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

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

问题描述

我有一个可以由用户进行更新的列表,但 notifyDataSetChanged()不工作与的ListView <相关的适配器/ code>。我用它在一个片段。 Datbase得到更新,然而adpater没有。以下是我的实现:

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:

我的主要Fargment:

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;
}

}

这是怎么回事不对任何想法?为什么不是列表视图得到更新?任何帮助是AP preciated。

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.

的ArrayList&LT; myList中&GT; myArrayList =新的ArrayList&LT; myList中&GT;(); //作为一个全局变量

ArrayList<myList> myArrayList=new ArrayList<myList>(); // as a global variable

通过自查空和空状态添加列表到适配器直接。直接将适配器列表(不检查任何条件)

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(适配器);

将数据添加到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天全站免登陆