Java 我无法在Android片段中使用textview填充gridview
我无法使用片段填充gridview内部的imageview文本视图 它显示了在我的项目中填充gridview的空白intead,请任何人查看下面的代码,并让我知道我需要更改什么 图像和文本的填充是从mysql数据库中动态生成的Java 我无法在Android片段中使用textview填充gridview,java,android,gridview,android-fragments,android-fragmentactivity,Java,Android,Gridview,Android Fragments,Android Fragmentactivity,我无法使用片段填充gridview内部的imageview文本视图 它显示了在我的项目中填充gridview的空白intead,请任何人查看下面的代码,并让我知道我需要更改什么 图像和文本的填充是从mysql数据库中动态生成的 public class StoreHomeFragment extends Fragment { final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<Hash
public class StoreHomeFragment extends Fragment {
final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.store_home, container, false);
final GridView gridView1 = (GridView)rootView.findViewById(R.id.store_home_gridview);
gridView1.setAdapter(new ImageAdapter(rootView.getContext(), MyArrList));
return rootView;
}
公共类StoreHomeFragment扩展了片段{
final ArrayList MyArrList=新的ArrayList();
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.store\u home,container,false);
final GridView GridView 1=(GridView)rootView.findviewbyd(R.id.store\u home\u GridView);
setAdapter(新的ImageAdapter(rootView.getContext(),MyArrList));
返回rootView;
}
//活动已创建
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String url = "http://192.168.1.132/Android/App/good.php"; //url where i am using select query and retrieving it from database
try {
JSONArray data = new JSONArray(getJSONUrl(url));
HashMap<String, String> map;
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);//retrieving from db
map = new HashMap<String, String>();
map.put("name", c.getString("name"));
map.put("artist", c.getString("artist"));
map.put("price", c.getString("price"));
map.put("image", c.getString("image"));
MyArrList.add(map);
}
//gridView1.setAdapter(new ImageAdapter(this,MyArrList));
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
@覆盖
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
字符串url=”http://192.168.1.132/Android/App/good.php“;//我正在使用select查询并从数据库中检索它的url
试一试{
JSONArray数据=新的JSONArray(getJSONUrl(url));
HashMap图;
对于(int i=0;i
//我用过imageAdapter
class ImageAdapter extends BaseAdapter
{
private Context context;
public ImageView imageView;
private ArrayList<HashMap<String, String>> MyArr = new ArrayList<HashMap<String, String>>();
public ImageAdapter(Context c,ArrayList<HashMap<String, String>> list)
{
context = c;
MyArr = list;
}
public int getCount() {
return MyArr.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.store_home_gridview_row, null);
}
TextView tv_title = (TextView) convertView.findViewById(R.id.textview_name);
tv_title.setText("Title:"+MyArr.get(position).get("name"));
TextView tv_artist = (TextView) convertView.findViewById(R.id.textview_artist);
tv_artist.setText("Artist:"+MyArr.get(position).get("artist"));
TextView tv_duration = (TextView) convertView.findViewById(R.id.textview_price);
tv_duration.setText("Price:"+MyArr.get(position).get("price"));
String abc = (MyArr.get(position).get("image"));
String abcd = "http://192.168.1.132/images/products/"+abc;
imageView = (ImageView) convertView.findViewById(R.id.imageView1);
try
{
URL url3 = null;
try {
url3 = new URL(abcd);
} catch (Exception e) {
e.printStackTrace();
}
Bitmap bmp = BitmapFactory.decodeStream(url3.openConnection().getInputStream()); //image is populated
imageView.setImageBitmap(bmp);
}
catch(Exception par)
{
imageView.setImageResource(android.R.drawable.ic_menu_report_image);
}
return convertView;
}
}
/*** Get JSON Code from URL ***/
public String getJSONUrl(String url) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try
{
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
System.out.println ( "status Code : " + statusCode );
if (statusCode == 200)
{
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null)
{
str.append(line);
}
}
else
{
Log.e("Log", "Failed to download file..");
}
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println ( "str : " + str.toString() );
return str.toString();
}
}
class ImageAdapter扩展了BaseAdapter
{
私人语境;
公共影像视图;
private ArrayList MyArr=new ArrayList();
公共ImageAdapter(上下文c、ArrayList列表)
{
上下文=c;
MyArr=列表;
}
public int getCount(){
返回MyArr.size();
}
公共对象getItem(int位置){
返回位置;
}
公共长getItemId(int位置){
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutFlater充气器=(LayoutFlater)上下文
.getSystemService(上下文布局\充气机\服务);
if(convertView==null){
convertView=充气机。充气(R.layout.store\u home\u gridview\u row,空);
}
TextView tv_title=(TextView)convertView.findViewById(R.id.TextView_name);
tv_title.setText(“title:+MyArr.get(position.get)(“name”));
TextView电视艺术家=(TextView)convertView.findViewById(R.id.TextView艺术家);
tv_artist.setText(“艺术家:”+MyArr.get(position.get(“艺术家”)));
TextView tv_duration=(TextView)convertView.findViewById(R.id.TextView_price);
tv_duration.setText(“价格:”+MyArr.get(position.get(“价格”)));
字符串abc=(MyArr.get(position.get(“image”));
字符串abcd=”http://192.168.1.132/images/products/“+abc;
imageView=(imageView)convertView.findViewById(R.id.imageView1);
尝试
{
URL url3=null;
试一试{
url3=新URL(abcd);
}捕获(例外e){
e、 printStackTrace();
}
位图bmp=BitmapFactory.decodeStream(url3.openConnection().getInputStream());//图像已填充
设置图像位图(bmp);
}
捕获(异常par)
{
setImageResource(android.R.drawable.ic\u菜单\u报告\u图像);
}
返回视图;
}
}
/***从URL获取JSON代码***/
公共字符串getJSONUrl(字符串url){
StringBuilder str=新的StringBuilder();
HttpClient=new DefaultHttpClient();
HttpGet HttpGet=新的HttpGet(url);
尝试
{
HttpResponse response=client.execute(httpGet);
StatusLine StatusLine=response.getStatusLine();
int statusCode=statusLine.getStatusCode();
System.out.println(“状态代码:“+statusCode”);
如果(状态代码==200)
{
HttpEntity=response.getEntity();
InputStream内容=entity.getContent();
BufferedReader=新的BufferedReader(新的InputStreamReader(内容));
弦线;
而((line=reader.readLine())!=null)
{
str.append(行);
}
}
其他的
{
Log.e(“Log”,“未能下载文件…”);
}
}
捕获(客户端协议例外e)
{
e、 printStackTrace();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
System.out.println(“str:+str.toString());
return str.toString();
}
}
遵循以下步骤,希望这对您有所帮助
1) 从onCreateView()方法中删除这些行
2) 按如下所示修改ActivityCreated()
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
String url = "http://192.168.1.132/Android/App/good.php";
try {
JSONArray data = new JSONArray(getJSONUrl(url));
HashMap<String, String> map;
for (int i = 0; i < data.length(); i++) {
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, String>();
map.put("name", c.getString("name"));
map.put("artist", c.getString("artist"));
map.put("price", c.getString("price"));
map.put("image", c.getString("image"));
MyArrList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
final GridView gridView1 = (GridView) rootView.findViewById(R.id.store_home_gridview);
gridView1.setAdapter(new ImageAdapter(getActivity(), MyArrList));
}
}.execute();
}
@覆盖
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
新建异步任务(){
@凌驾
受保护的Void doInBackground(Void…参数){
字符串url=”http://192.168.1.132/Android/App/good.php";
试一试{
JSONArray数据=新的JSONArray(getJSONUrl(url));
HashMap图;
对于(int i=0;i@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
String url = "http://192.168.1.132/Android/App/good.php";
try {
JSONArray data = new JSONArray(getJSONUrl(url));
HashMap<String, String> map;
for (int i = 0; i < data.length(); i++) {
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, String>();
map.put("name", c.getString("name"));
map.put("artist", c.getString("artist"));
map.put("price", c.getString("price"));
map.put("image", c.getString("image"));
MyArrList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
final GridView gridView1 = (GridView) rootView.findViewById(R.id.store_home_gridview);
gridView1.setAdapter(new ImageAdapter(getActivity(), MyArrList));
}
}.execute();
}