Android notifyDataSetChanged 不工作
Android notifyDataSetChanged not working
我有一个适配器,它用来自 TreeMap 的数据填充 ListView 和 2 个 TextView。
当用户在 ListView 中添加或删除数据时,应刷新视图。
这是我的适配器:
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;
}}
因为我想从对话框更新视图,并且在我阅读的另一个问题上,需要从 UIThread 调用 notifyDataSetChanged()
我将我的 notifyDataSetChanged()
放入 可运行。在这里:
Runnable run = new Runnable(){
public void run(){
Log.v("in the Runnable: ", String.valueOf(colorHashMap));
adapter.notifyDataSetChanged();
}
};
这就是在对话框中调用 Runnable 的方式:
DefaultColorActivity.this.runOnUiThread(run);
但无论我尝试或做什么,列表都不会更新。我需要关闭并重新打开 activity 以获取新列表。
从您的适配器调用 notifyDataSetChanged() 方法 class(在 getView() 方法中),它在删除项目后扩展 BaseAdapter。
首先,您必须在地图(适配器的数据源)中进行更改。并使用 Handler class(Android OS Thread) 到 运行 这样的线程
Handler handler=new Handler();
handler.post(run);
在您的 adapter
中创建一个方法,例如:
public void updateList(){
notifyDataSetChanged()
}
并在需要刷新列表时调用此方法
您不能创建列表对象。
只需创建参考并将列表分配给您的列表
在你的适配器中删除这条语句class
mData = new ArrayList();
我有一个适配器,它用来自 TreeMap 的数据填充 ListView 和 2 个 TextView。 当用户在 ListView 中添加或删除数据时,应刷新视图。
这是我的适配器:
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;
}}
因为我想从对话框更新视图,并且在我阅读的另一个问题上,需要从 UIThread 调用 notifyDataSetChanged()
我将我的 notifyDataSetChanged()
放入 可运行。在这里:
Runnable run = new Runnable(){
public void run(){
Log.v("in the Runnable: ", String.valueOf(colorHashMap));
adapter.notifyDataSetChanged();
}
};
这就是在对话框中调用 Runnable 的方式:
DefaultColorActivity.this.runOnUiThread(run);
但无论我尝试或做什么,列表都不会更新。我需要关闭并重新打开 activity 以获取新列表。
从您的适配器调用 notifyDataSetChanged() 方法 class(在 getView() 方法中),它在删除项目后扩展 BaseAdapter。
首先,您必须在地图(适配器的数据源)中进行更改。并使用 Handler class(Android OS Thread) 到 运行 这样的线程
Handler handler=new Handler();
handler.post(run);
在您的 adapter
中创建一个方法,例如:
public void updateList(){
notifyDataSetChanged()
}
并在需要刷新列表时调用此方法
您不能创建列表对象。
只需创建参考并将列表分配给您的列表
在你的适配器中删除这条语句class
mData = new ArrayList();