调用notifyDataSetChanged后,Listview更新无法正常工作。用户点击构建并更新数据但屏幕永远不会更新?有人可以告诉我为什么负载数据可能无法正常工作?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list1 = (ListView) findViewById(R.id.ListView01);
list1.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, array));
final EditText EditMessage = (EditText) findViewById(R.id.editTextWebSite);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String website = EditMessage.getText().toString();
//String returnString = loaddata(website);
Toast.makeText(v.getContext(),
"Updating Information",
Toast.LENGTH_LONG).show();
setListData();
BaseAdapter la = (BaseAdapter)list1.getAdapter();
((BaseAdapter) la).notifyDataSetChanged() ;
Toast.makeText(v.getContext(),
"Updated",
Toast.LENGTH_LONG).show();
}
});
private void setListData()
{
String array2[] = { "Iphone", "Tutorials", "Gallery", "Android", "item 1", "item 2", "item3", "item 4" };
System.arraycopy(array, 0, array2, 0, array.length);
}
答案 0 :(得分:1)
尝试
((ArrayAdapter)list1.getAdapter()).notifyDataSetChanged() ;
而不是强制转换为BaseAdapter。
答案 1 :(得分:1)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ArrayAdapter mArrayAdapter=new ArrayAdapter(this, android.R.layout.simple_list_item_1, array);
list1 = (ListView) findViewById(R.id.ListView01);
list1.setAdapter(mArrayAdapter);
final EditText EditMessage = (EditText) findViewById(R.id.editTextWebSite);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String website = EditMessage.getText().toString();
//String returnString = loaddata(website);
Toast.makeText(v.getContext(),
"Updating Information",
Toast.LENGTH_LONG).show();
setListData();
mArrayAdapter.notifyDataSetChanged() ;
Toast.makeText(v.getContext(),
"Updated",
Toast.LENGTH_LONG).show();
}
});
试试这个可能有帮助
答案 2 :(得分:1)
我发现侦听器方法可能无法在UI主线程上运行。您应该确保何时使用
adapter.notifyDataSetChanged()
UI主线程中的。您可以使用 hander 来解决此问题。