【发布时间】:2014-12-22 10:56:11
【问题描述】:
我想从 PHP MySql 数据库中检索所有图像并填充 GridView。我正在使用 JSON 解析。但是在我的网格视图中没有显示来自 PHP 的图像,也没有任何错误。那么为什么不在我的应用程序的 Gridview 中显示图像。代码有什么问题。感谢感谢。
这是我的适配器代码。
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class GridViewAdapter extends BaseAdapter
{
private Context context;
public ArrayList<HashMap<String, String>> mThumbIds;
public GridViewAdapter (Context context,ArrayList<HashMap<String, String>> data )
{
this.context= context;
mThumbIds= data;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position ;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(context);
imageView.setImageResource(mThumbIds.get(position).get("image"));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
return imageView;
}
}
这是活动代码。
package com.photo_app;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.GridView;
public class Photo_Gallery extends Activity
{
JSONObject jsonobject;
JSONArray jsonarray;
GridView gridview;
GridViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
JSONParser jsonParser = new JSONParser();
ProgressDialog pDialog;
private String URL_PHOTO_GALLERY = "http://192.168.1.102/timesofindia/admin/photo_gallery.php";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.photo_gallery);
new DownloadJSON().execute();
}
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(Photo_Gallery.this);
mProgressDialog.setTitle("Wait");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params)
{
arraylist = new ArrayList<HashMap<String, String>>();
jsonobject = JSONfunctions.getJSONfromURL(URL_PHOTO_GALLERY);
System.out.println("Json String = " + jsonobject.toString());
try
{
jsonarray = jsonobject.getJSONArray("photo_gallary");
for (int i = 0; i < jsonarray.length(); i++)
{
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
map.put("flag", jsonobject.getString("image"));
arraylist.add(map);
Log.e("arraylist","=");
}
}
catch (JSONException e)
{
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
gridview = (GridView) findViewById(R.id.photoGallery);
adapter = new GridViewAdapter();
gridview.setAdapter(adapter);
mProgressDialog.dismiss();
}
}
}
【问题讨论】:
-
安居,你的图片在哪里?在 mThumbIds 数组或 arraylist 数组中???
-
您是否为网格项设计了自定义行布局?
-
@BellaSwan 我认为问题在于获取图像及其数组,她采用了动态图像视图,所以不是问题。
-
@Pratik : - Arraylist 中的图像来自 Activity 中的 json 而不是 Adapter.Ho 中的 mThumbIds 来执行此操作。
-
@anjupatel 很简单,亲爱的。如果你不介意那么你能把你的代码发给我吗?让我用工作代码回复你。
标签: android phpmyadmin