android listview只是偶尔更新
我的android程序中有一个listview,它从ArrayList适配器获取信息。 我有三个方法调用listview.invalidateViews() 其中两种方法工作正常,第三种方法似乎冻结了listview。退出活动并在屏幕上旋转时,信息会正确保存。但是如果不执行这些操作,listview不会更新 有什么想法吗 更新: 这些实例有效:android listview只是偶尔更新,android,android-layout,Android,Android Layout,我的android程序中有一个listview,它从ArrayList适配器获取信息。 我有三个方法调用listview.invalidateViews() 其中两种方法工作正常,第三种方法似乎冻结了listview。退出活动并在屏幕上旋转时,信息会正确保存。但是如果不执行这些操作,listview不会更新 有什么想法吗 更新: 这些实例有效: public void onItemClick(AdapterView<?> a, View v, int index, long id)
public void onItemClick(AdapterView<?> a, View v, int index, long id) {
al.remove(index);
adapter.notifyDataSetChanged();
}
public void addToList(View view) {
EditText et = (EditText) findViewById(R.id.ListText1);
if (et.getText().toString().equals("")) {
//do nothing
}
else {
al.add(et.getText().toString());
adapter.notifyDataSetChanged();
et.setText(null);
}
}
public-void-onItemClick(AdapterView a、View v、int-index、long-id){
al.移除(索引);
adapter.notifyDataSetChanged();
}
公共无效添加列表(视图){
EditText et=(EditText)findViewById(R.id.ListText1);
if(et.getText().toString().equals(“”){
//无所事事
}
否则{
al.add(et.getText().toString());
adapter.notifyDataSetChanged();
et.setText(空);
}
}
此方法不起作用:
public void resetList(View view) {
al = new ArrayList<String>();
adapter.notifyDataSetChanged();
}
public void resetList(视图){
al=新的ArrayList();
adapter.notifyDataSetChanged();
}
您使用的是不同的invalidateViews()
,如果您想更改listview子视图的视图
,则可以使用invalidateViews()
,但是如果要更改listview的adapte
r的数据/内容,则需要使用notifyDataSchanged()
方法
本文讨论了两者的区别
ListView.invalidateViews()
适配器。notifyDataSetChanged()
用你的方法
public void resetList(View view) {
al = new ArrayList<String>();
adapter.notifyDataSetChanged();
}
public void resetList(视图){
al=新的ArrayList();
adapter.notifyDataSetChanged();
}
创建
ArrayList()的新对象代码>不会重置列表视图的数据。只是因为在适配器上传递的al
ArrayList现在与al=new ArrayList()不同代码>您现在需要做的是访问当前的arraylist,然后使用al.clear()
方法清除其内容。您只是在更改列表视图的视图还是内容?如果要更改列表视图的数据,请使用notifydatasetchanged方法。我正在更新列表的内容。adapter.notifydatasetchanged()似乎执行了相同的操作。您可能希望读取我已将listview.invalidateViews()的所有实例更改为adapter.notifydatasetchanged()。同样的问题也发生了是的,notifydatasetchanged()的两个实例正常工作,第三个实例给了我同样的问题。
on the other hand, is to tell the observer of the adapter that the contents of what is being adapted have changed. Notifying the dataset changed will cause the listview to invoke your adapters methods again to adjust scrollbars, regenerate item views, etc...
public void resetList(View view) {
al = new ArrayList<String>();
adapter.notifyDataSetChanged();
}