calling BaseAdapter's notifyDataSetChanged() inside CustomAdapter not refreshing ListView

本小妞迷上赌 提交于 2019-12-13 03:00:45

问题


I have ArrayList<MyObject> data which is populated on the ListView using a CustomAdapter.

I've implemented View.OnClickListener in the CustomAdapter class and clicking a button deletes the particular row. After deleting the row, both from database and from the data object using data.remove(i) inside the Custom Adapter, I call notifyDataSetChanged().

Problem is that it does not refresh teh ListView.

I'm using a tabbed activity with fragment. So, when go to some distant tab and come back to this tab, it refreshes the ListView and the deleted item does not show anymore.

Why is the ListView not refreshing immediately when i call notifyDataSetChanged() but changing when i move to another fragment and back?

My CustomAdapter class is as follows:

public class CustomAdapter extends BaseAdapter implements View.OnClickListener {

    private LayoutInflater mInflater;
    Context context;
    private ArrayList<MyObject> data;

    public CustomAdapter(Context context, ArrayList<MyObject> data) {
        mInflater = LayoutInflater.from(context);
        this.data = data;
        this.context = context;
    }
    .....
    .....
    .....
    .....
    .....

    @Override
    public void onClick(View v) {
        DataBaseHelper db = new DataBaseHelper(context);
        int id = data.get((Integer) v.getTag()).id;
        //use getTag() to get the position, set position in tag before adding listener.
        switch (v.getId()){
            case R.id.txtName:
                Toast.makeText(context, "Show Details" + v.getTag(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.btnRemove:
                db.removePaper(id); // works fine
                data.remove(v.getTag()); // works fine
                notifyDataSetChanged(); // PROBLEM HERE: Does not refresh listview
                break;
            case R.id.btnFavorite:
                Toast.makeText(context, "Favorite" + v.getTag(), Toast.LENGTH_SHORT).show();
                break;

        }

    }
}

回答1:


I had a similar issue some time ago, I fixed it extending ArrayAdapter<MyObject> instead of BaseAdapter and overriding some methods:

public class CustomAdapter extends ArrayAdapter<MyObject> {

private List<MyObject> list = new ArrayList<MyObject>();

public CustomAdapter(Context context, int resource) {
    super(context, resource);
}

 @Override
 public void add(MyObject obj) {
     this.list.add(obj);
     super.add(obj);
 }

 @Override
 public void remove(MyObject obj) {
     int i = getPosition(obj);
     if(i >= 0)
         list.remove(i);
     super.remove(obj);
 }

 @Override
 public void clear() {
     this.list.clear();
     super.clear();
 }

 @Override
 public MyObject getItem(int position) {
     return this.list.get(position);
 }

 @Override
 public int getPosition(MyObject obj) {
     for(int i = 0; i < list.size(); i++) {
         if(list.get(i).equals(obj))
             return i;
     }
     return -1;
 }}

Then call these methods instead of calling directly methods of your List.



来源:https://stackoverflow.com/questions/35253269/calling-baseadapters-notifydatasetchanged-inside-customadapter-not-refreshing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!