0

I'm using a listview in my android application in which the listitems(in my case it is bitmap images) are loaded dynamically. Actually i'm creating the bitmap images and then it is loaded one by one into the list. what i want is to show all the list items with some default image and update them correspondingly when the bitmap image is created. My code is given below,

public class BitmapDemoActivity extends Activity {

HorizontalListView listview;
Vector<Bitmap> thumbImg;
BitmapCreator creator;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.listviewdemo);

    creator=new BitmapCreator();
    thumbImg= new Vector<Bitmap>(97);

    listview = (HorizontalListView)findViewById(R.id.listview);
    listview.setAdapter(new BitmapAdapter());

    new AsyncBitmapCreate().execute();

}

private class AsyncBitmapCreate extends AsyncTask<Void, Bitmap, Void>{

    //Bitmap[] temp=new Bitmap[44];
    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

        for(int i=0;i<97;i++){

            publishProgress(creator.generateBitmap(i+1));
        }

        return null;
    }

    @Override
    protected void onProgressUpdate(Bitmap... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);

        new BitmapAdapter().add(values[0]);
        new BitmapAdapter().notifyDataSetChanged();
    }
}

class BitmapAdapter extends BaseAdapter{

    public void add(Bitmap bitmap)
    {
        Log.w("My adapter","add");
        thumbImg.add(bitmap);
    }

    @Override
    public int getCount() {
        return thumbImg.capacity();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
        View retval = inflater.inflate(R.layout.listitem, null);
        ImageView img = (ImageView) retval.findViewById(R.id.tImage);

            img.setImageBitmap(thumbImg.get(position));

        return retval;
    }

};

}

Here i'm using a vector in which after creating each bitmap, it is inserted into that vector. I'm using an asynctask to create the bitmap. After each bitmap is created i'm calling notifydatasetchanged() method to update the listview. But now in the output whenever each bitmap image is created it is adding one item in the listview with that image. But my requirement is to show all the 97 items in my list with some default image and whenever bitmap is created update the corresponding listitem.

can anyone help me?? Thanks in advance....

user456
  • 61
  • 1
  • 7
  • see this http://stackoverflow.com/questions/541966/how-do-i-do-a-lazy-load-of-images-in-listview – njzk2 Dec 11 '12 at 08:41

1 Answers1

1

The simplest would be to include the default image as the src for the ImageView with id tImage in your layout listitem.xml. And in your getView method, replace the default image if the Bitmap for that position is available.

    ImageView img = (ImageView) retval.findViewById(R.id.tImage);
    Bitmap bmp = null;
    if(position < thumbImg.size()){
       thumbImg.get(position);
    }
    if(null != bmp){
        img.setImageBitmap(bmp);
    }
Rajesh
  • 15,724
  • 7
  • 46
  • 95
  • :while doing this i'm getting the arrayindexoutofbound exception...is there anything to do with the getcount method() ? – user456 Dec 11 '12 at 08:02
  • Please check the updated answer. Since you are populating the vector in an AsyncTask, the size of the Vector may vary. Also, hardcoding of magic numbers like 97 is discouraged. – Rajesh Dec 11 '12 at 08:18