I have a ListView. I want to dynamically change the contents of the listview.I used "adapter.notifyDataSetChanged();" to change the contents. I have also tried setting the adapter of the listview to null but still I get the same results. It is refreshing but not clearing the listview, instead it is appending it with the previous results. What i want is the previous contents should be deleted and the new results should be displayed.
I am not able to understand where is my mistake and what i am missing.
I would be Thankful if someone could please help me out with this.
Below is the code that generates the listview
public void ListDrawer() {
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("myRegisteredList");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String registered_name = jsonChildNode.optString("seminar");
String org = jsonChildNode.optString("organizer");
String sem_id = jsonChildNode.optString("id");
String r_sem = jsonChildNode.optString("seminar_id");
registeredlist = new HashMap<String, String>();
registeredlist.put("registeredList", registered_name);
registeredlist.put("seminar_id", r_sem);
registeredlist.put("sem_id", sem_id);
registeredlist.put("organizer", org);
registeredList.add(registeredlist);
}
} catch (JSONException e) {
//Toast.makeText(this, e.getMessage().toString() + " 1", Toast.LENGTH_LONG).show();
Toast.makeText(ViewReservedSeminarList.this, "Sorry...You don't have any reserved seminar/s.", Toast.LENGTH_LONG).show();
}
try{
simpleAdapter = new SimpleAdapter(this, registeredList,
android.R.layout.simple_list_item_2,
new String[] { "registeredList" ,"organizer", "seminar_id" }, new int[] { android.R.id.text1 , android.R.id.text2 , android.R.id.title });
listView.setAdapter(simpleAdapter);
listView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
// TODO Auto-generated method stub
@SuppressWarnings("unchecked")
HashMap<String, String> selected_row = (HashMap<String, String>) simpleAdapter.getItem(i);
s_id = selected_row.get("seminar_id");
Intent intent = new Intent(ViewReservedSeminarList.this, ReservedSeminar.class);
startActivity(intent);
//Toast.makeText(ViewReservedSeminarList.this, "Selected Item : "+s_id, Toast.LENGTH_LONG).show();
}
});
}catch(Exception e){
//Toast.makeText(this, e.getMessage().toString() + " 2", Toast.LENGTH_LONG).show();
Toast.makeText(ViewReservedSeminarList.this, "There is no data being fetched", Toast.LENGTH_LONG).show();
}
}
Below is the snippet I placed on the onCreate method to refresh the listview every 10 seconds. I am calling the ListDrawer method under the accessWebService method.
final Handler handler = new Handler();
handler.postDelayed( new Runnable(){
@Override
public void run(){
accessWebService();
handler.postDelayed(this, 10 * 1000);
Log.i("TAG", "notify");
}
}, 10 * 1000
);
Try this please:
Clear the arraylist/HashMap
Add new items
adapter.notifyDataSetChanged();
set adapter.
If you use an ArrayAdapter instead of a SimpleAdapter you can call
ArrayAdapter.clear();
ArrayAdapter.notifyDataSetChanged();
To remove all the elements from the adapter.
Try This
final Handler handler = new Handler();
handler.postDelayed( new Runnable(){
@Override
public void run(){
//this line i have added in your code
registeredList.clear();
accessWebService();
handler.postDelayed(this, 10 * 1000);
Log.i("TAG", "notify");
}
}, 10 * 1000
);
remove adapter.notifyDataSetChanged(); hope it works
来源:https://stackoverflow.com/questions/22954679/clear-android-listview