ArrayAdapter.NotifyDataSetChanged() 不起作用? [英] ArrayAdapter.NotifyDataSetChanged() is not working?

查看:20
本文介绍了ArrayAdapter.NotifyDataSetChanged() 不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可由用户更新的列表,但 notifyDataSetChanged() 不适用于与 listView 关联的适配器.我在一个片段中使用它.数据库会更新,但适配器不会.以下是我的实现:

I have a list that can be updated by the user, but notifyDataSetChanged() is not working on the adapter associated with the listView. I am using it in a fragment. Datbase gets updated, however the adpater doesn't. Following is my implementation:

我的主要片段:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    myArrayList = new ArrayList<myList>();
    myArrayList = Database.getSharedObject(getActivity()).getMyList();
    if (myArrayList != null && myArrayList.size() > 0) {
        adapter = new myListAdapter(getActivity(), 0, myArrayList);
    }

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.mainfragment, container, false);
    myListView = (ListView) v.findViewById(R.id.my_list_view);
    myListView.setOnItemClickListener(this);
    if (adapter != null) {
        myListView.setAdapter(adapter);
    }

    return v;
}

在一些导航过程列表更新后,在我的 onResume() 方法中,我从数据库中获取更新后的列表并调用不更新我的视图的 notifyDatasetChanged() 方法,但是来自 DB 的对应数组被更新为一个.

After some navigations process List gets updated and in my onResume() method I get the updated List from database and call notifyDatasetChanged() method that does not update my view, however corresponding array from DB is updated one.

@Override
public void onResume() {
    super.onResume();
    myArrayList = Database.getSharedObject(getActivity()).getMyList();
    adapter.setItems (myArrayList);
    adapter.notifyDataSetChanged();

}

MyListAdapter

public class MyListAdapter extends ArrayAdapter<Message> {

    private ArrayList<Message> myList;

    /* Context */
    private Context context;

    public void setItems (ArrayList<Message> myList) {
        this.myList = myList;
    }

    public MyListAdapter (Context context, int textViewResourceId, ArrayList<Message> myList) {
        super(context, textViewResourceId, myList);
        this.context = context;
        this.myList = myList;
    }

    @Override
    public View getView (int position, View v, ViewGroup parent) {
        // Keeps reference to avoid future findViewById()
        MyViewHolder viewHolder;

        if (v == null) {
            LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = li.inflate(R.layout.list_row, parent, false);

            viewHolder = new MyViewHolder();
            viewHolder.txtImage = (ImageView) v.findViewById(R.id.message_icon);
            viewHolder.txtMessage = (TextView) v.findViewById(R.id.message);
            viewHolder.txtDate = (TextView) v.findViewById(R.id.date);

            v.setTag(viewHolder);
        } else {
            viewHolder = (MyViewHolder) v.getTag();
        }

        Message myMessage = new Message();
        myMessage = myList.get(position);
        if (myMessage != null) {
            viewHolder.txtMessage.setText(myMessage.getText());
            viewHolder.txtDate.setText(myMessage.getDate());
        }
        return v;
    }

    static class MyViewHolder {
        ImageView txtImage;
        TextView txtMessage;
        TextView txtDate;
    }
}

有没有想过出了什么问题?为什么列表视图没有更新?任何帮助表示赞赏.

Any thought on what is going wrong? Why is not listview getting updated? Any helps is appreciated.

推荐答案

notifyDataSetChanged() 对你不起作用.原因

  1. 您的适配器丢失了对您的列表的引用.
  2. 总是为适配器创建和添加新列表.这样做:在全局声明的同时初始化 ArrayList.

  1. Your adapter loses reference to your list.
  2. Always creating and adding a new list to the Adapter. Do like this: initialize the ArrayList while declaring globally.

ArrayListmyArrayList=new ArrayList();//作为全局变量

直接将 List 添加到适配器,无需检查 null 和 empty 条件.直接将适配器设置到列表中(不检查任何条件)

Add List to the adapter directly with out checking null and empty condition. Set the adapter to the list directly(don't check for any condition)

myListView.setAdapter(adapter);

每次都将数据添加到 myArrayList(如果您的数据是全新的,那么您可以在实际将数据添加到列表之前调用 adapter.clear() 和 myArrayList.clear())但不要设置适配器即如果新数据填充在 myArrayList 中而不仅仅是 adapter.notifyDataSetChanged()

Add data to the myArrayList Every time(if your data is completely new than you can call adapter.clear() and myArrayList.clear() before actually adding data to the list) but don't set the adapter i.e If the new data is populated in the myArrayList than just adapter.notifyDataSetChanged()

adapter = new myListAdapter(getActivity(), 0, myArrayList);

  • 删除这一行

  • Remove this line

    adapter.setItems (myArrayList);

    这篇关于ArrayAdapter.NotifyDataSetChanged() 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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