Listview update is not working after calling notifyDataSetChanged
Listview update is not working after calling notifyDataSetChanged. The user clicks on a build and I update the data but the screen never gets updated? can someone please tell me why maybe the load data is not working?
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);
}
Try
((ArrayAdapter)list1.getAdapter()).notifyDataSetChanged() ;
instead of casting to BaseAdapter.
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();
}
});
try this out may helps
I found the listener method may not run on the UI main thread. You should make sure when you use
adapter.notifyDataSetChanged()
in the UI main thread.So you can use hander to resolve this problem.
精彩评论