在CustomAdapter中调用BaseAdapter的notifyDataSetChanged()不会刷新ListView [英] calling BaseAdapter's notifyDataSetChanged() inside CustomAdapter not refreshing ListView

查看:75
本文介绍了在CustomAdapter中调用BaseAdapter的notifyDataSetChanged()不会刷新ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ArrayList<MyObject> data,它使用CustomAdapter填充在ListView上.

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

我已经在CustomAdapter类中实现了View.OnClickListener,单击按钮将删除特定行.在数据库中以及使用自定义适配器中的data.remove(i)data对象中删除该行之后,我将调用notifyDataSetChanged().

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().

问题在于它不刷新ListView.

我正在使用带有片段的选项卡式活动.因此,当转到某个较远的选项卡并返回到该选项卡时,它将刷新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.

为什么当我调用notifyDataSetChanged()ListView没有立即刷新,但是当我移至另一个片段并返回时却发生了变化?

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

我的CustomAdapter类如下:

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;

        }

    }
}

推荐答案

我前段时间有一个类似的问题,我修复了它扩展了ArrayAdapter<MyObject>而不是BaseAdapter并覆盖了一些方法的问题:

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.

这篇关于在CustomAdapter中调用BaseAdapter的notifyDataSetChanged()不会刷新ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆