Gridview get image from JSON using AsyncTask

Posted by kongkea on Stack Overflow See other posts from Stack Overflow or by kongkea
Published on 2012-10-08T09:35:51Z Indexed on 2012/10/08 9:36 UTC
Read the original article Hit count: 534

Filed under:
|
|

This project I've done with image in my drawable but now I want to get image url from JSON by using Asynctask and display it. and I make php that provide a json string like below.

I want to get path of image(url) by using AsyncTask from JSON. I want to use data from json instead of public mThumbId = {...};

 {"count":"28","data":
    [{"id":"1",
        "first_name":"man",
        "last_name":"woman",
        "username":"man",
        "password":"4f70432e636970de9929bcc6f1b72412",
        "email":"[email protected]",
        "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":"[email protected]",
            "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);
        }
    });

ImageAdapter

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;
}

}

FullImageActivity

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]);

© Stack Overflow or respective owner

Related posts about android

Related posts about JSON