0

How can I dynamically add grid items in grid view? Currently, I have an adapter containing my images. I want to get my images from an URL and dynamically add them to my grid view. I am using following code to download images from url

                try {
            URL myFileUrl =null;  
         myFileUrl= new URL(imageUrl);

          HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
    conn.setDoInput(true);
    conn.connect();
    InputStream is = conn.getInputStream();

    bmImg = BitmapFactory.decodeStream(is);
    //images[i].setImageBitmap(bmImg);
      }       catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }

and the following is mu base image adpater class

      public class ImageAdapter extends BaseAdapter{
    Context mContext;
    public static final int ACTIVITY_CREATE = 10;
    public ImageAdapter(Context c){
        mContext = c;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 9;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View v;
        if(convertView==null){
            LayoutInflater li = getLayoutInflater();
            v = li.inflate(R.layout.icon, null);
            TextView tv = (TextView)v.findViewById(R.id.icon_text);
            tv.setText("Profile "+position);
            ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
            iv.setImageResource(R.drawable.ondamoveicon);

        }
        else
        {
            v = convertView;
        }
        return v;
    }
    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
}

Now my problem is that i want to display the images that are download through the url in the adapter. how we will pass these images to adapter class.

NOW I am getting the last image on all the places in the gridview

Can anyone help me over this Thanks

ekjyot
  • 2,247
  • 7
  • 39
  • 63

2 Answers2

3
  1. Increase the count of your adapter class.

  2. You have to use some resource array , which will store images. after you download a new image , increase the count by 1, and update the resource array with new image.

  3. call adapter.notifyDataSetChanged().

This will refresh the gridView.

Yashwanth Kumar
  • 28,931
  • 15
  • 65
  • 69
0

you can try LazyList and in main XML you can convert listview to gridview.

Sandroid
  • 328
  • 1
  • 10
  • 19