Question

i need to reload adapter when buton was clicked. But how can i perform it? My code is below.

public class CustomGridAdapter extends BaseAdapter {

private Context context;
//private final String[] gridValues;
ArrayList<Bitmap> arrayList;
ImageButton i1;

//Constructor to initialize values
public CustomGridAdapter(Context context, ArrayList<Bitmap> ob) {
    arrayList = ob;
    this.context = context;
    // this.gridValues     = gridValues;
}

@Override
public int getCount() {
    return arrayList.size();
}

@Override
public Object getItem(int position) {

    return null;
}

@Override
public long getItemId(int position) {

    return 0;
}

public View getView( int position,  View convertView,  ViewGroup parent) {
    final int pos = position;
    // LayoutInflator to call external grid_item.xml file

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View gridView;

    if (convertView == null) {

        gridView = new View(context);

        // get layout from grid_item.xml ( Defined Below )

        gridView = inflater.inflate(R.layout.grid_item, null);
        ImageView imageView = (ImageView) gridView
                .findViewById(R.id.grid_item_image);

        imageView.setImageBitmap(arrayList.get(position));
        ImageButton imageButton = (ImageButton) gridView
                .findViewById(R.id.imageButton);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.d("myLogs", "Yes, onclick "+pos);
                arrayList.remove(pos);
                 !!!THERE I NEED TO RELOAD IT
            }
        });
    } else {

        gridView = (View) convertView;
    }

    return gridView;
}

}

Was it helpful?

Solution

First of all, you have to set the event and use it in your activity... you will have to modify your adapter and add to it an OnClickListener attr:

private Context context;
private OnClickListener listener;
ArrayList<Bitmap> arrayList;
ImageButton i1;

//Constructor to initialize values
public CustomGridAdapter(Context context, ArrayList<Bitmap> ob, OnClickListener listener) {
    arrayList = ob;
    this.context = context;
    this.listener = listener;
}

And then to set your setOnClickListener, you have to do the following:

imageView.setImageBitmap(arrayList.get(position));
ImageButton imageButton = (ImageButton) gridView
            .findViewById(R.id.imageButton);
imageButton.setOnClickListener(listener);

Then, in your java, you need to let your activity implements OnClickListener, and then override the onClickListener method, where you can use the following method to reload your list:

@Override
public void onClick(View v) {   
        adapter = new CustomGridAdapter(MyActivity.this, myArrayListWithMyData, MyActivity.this);

        listView.setAdapter(adapter);

        listView.invalidate();
}

To set the adapter, you can pass your context (this, for example) as argument for the OnClickListener:

adapter = new CustomGridAdapter(this, myArrayListWithMyData, this);

Hope it helps!

OTHER TIPS

I believe you can use NotifyDataSetChanged(); and that should reload the data.

The problem with your code is that your list is reusing the old views and when the view gets reused, listener is not re-registered for current view as you are registering listeners only when a new view is created (not reused). Use the below code :

...    
if (convertView == null) {
                gridView = new View(context);
                // get layout from grid_item.xml ( Defined Below )
                gridView = inflater.inflate(R.layout.grid_item, null);
            } else {
                gridView = (View) convertView;
            }
            ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_item_image);
            imageView.setImageBitmap(arrayList.get(position));
            ImageButton imageButton = (ImageButton) gridView.findViewById(R.id.imageButton);
            imageButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d("myLogs", "Yes, onclick " + pos);
                    arrayList.remove(pos);
                    notifyDataSetChanged();
                }
            });
    ...

For more details on using and optimizing ListView, refer to this Blog.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top