这个项目我已经在我的可绘制对象中完成了图像,但现在我想通过使用 Asynctask 从 JSON 获取图像 url 并显示它。我制作了提供如下 json 字符串的 php。
我想通过使用 JSON 中的 AsyncTask 来获取图像(url)的路径。我想使用来自 json 的数据而不是public mThumbId = {...};
{"count":"28","data":
[{"id":"1",
"first_name":"man",
"last_name":"woman",
"username":"man",
"password":"4f70432e636970de9929bcc6f1b72412",
"email":"man@gmail.com",
"url":"http://vulcan.wr.usgs.gov/Imgs/Jpg/MSH/Images/MSH64_aerial_view_st_helens_from_NE_09-64_med.jpg"},
{"id":"2",
"first_name":"first",
"last_name":"Last Name",
"username":"user",
"password":"1a1dc91c907325c69271ddf0c944bc72",
"email":"first@gmail.com",
"url":"http://www.danheller.com/images/California/Marin/Scenics/bird-view-big.jpg"},
{"id":"3",
"first_name":"first",
"last_name":"Last Name",
"username":"user",
"password":"1a1dc91c907325c69271ddf0c944bc72",
"email":"0",
"url":"http://www.hermes.net.au/bodhi/images/view/large/view_03.jpg"}]}
AndroidGridLayoutActivity
GridView gridView = (GridView) findViewById(R.id.grid_view);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
i.putExtra("id", position);
startActivity(i);
}
});
图像适配器
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.pic_1, R.drawable.pic_2,
R.drawable.pic_3, R.drawable.pic_4,
R.drawable.pic_5, R.drawable.pic_6,
R.drawable.pic_7, R.drawable.pic_8,
R.drawable.pic_9, R.drawable.pic_10,
R.drawable.pic_11, R.drawable.pic_12,
R.drawable.pic_13, R.drawable.pic_14,
R.drawable.pic_15
};
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return mThumbIds[position];
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
return imageView;
}
}
全图像活动
Intent i = getIntent();
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
imageView.setImageResource(imageAdapter.mThumbIds[position]);