Android的notifyDataSetChanged不工作 [英] Android notifyDataSetChanged not working
问题描述
我有填充用2 TextViews从一个TreeMap的数据一个ListView的适配器。
当用户添加或从ListView中删除数据,查看应刷新。
I have an adapter which fills a ListView with 2 TextViews with data from a TreeMap. When the user adds or deletes Data from the ListView, the View should be refreshed.
因此,这里是我的适配器:
public class MyAdapter extends BaseAdapter {
private final ArrayList mData;
public MyAdapter(Map<String, String> map) {
mData = new ArrayList();
mData.addAll(map.entrySet());
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Map.Entry<String, String> getItem(int position) {
return (Map.Entry) mData.get(position);
}
@Override
public long getItemId(int position) {
// TODO implement you own logic with ID
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View result;
if (convertView == null) {
result = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_adapter_item, parent, false);
} else {
result = convertView;
}
Map.Entry<String, String> item = getItem(position);
// TODO replace findViewById by ViewHolder
((TextView) result.findViewById(android.R.id.text1)).setText(item.getKey());
((TextView) result.findViewById(android.R.id.text2)).setText(item.getValue());
return result;
}}
由于我想更新从对话框,并在另一个问题,我阅读, notifyDataSetChanged()
需要从UIThread我把我的<$ C称为视图$ C> notifyDataSetChanged()到 Runnable接口。这里是:
Because I want to update the view from a dialog and on another question I read, that notifyDataSetChanged()
needs to be called from the UIThread I put my notifyDataSetChanged()
into a Runnable. here is it:
Runnable run = new Runnable(){
public void run(){
Log.v("in the Runnable: ", String.valueOf(colorHashMap));
adapter.notifyDataSetChanged();
}
};
和这一点了Runnable被称为是如何在对话框:
And this it how the Runnable gets called in the Dialog:
DefaultColorActivity.this.runOnUiThread(run);
不过,我不管我怎么努力还是做,只需在列表中不会得到更新。我需要关闭并重新打开活动来获得新的列表。
But I no matter what I try or do, the List just won't get updated. I need to close and reopen the activity to get the new List.
推荐答案
在创建一个方法你的适配器
这样的:
create a method in your adapter
like:
public void updateList(){
notifyDataSetChanged()
}
的当你想刷新列表调用此方法的
这篇关于Android的notifyDataSetChanged不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!