Skip to content Skip to sidebar Skip to footer

How To Clear My Listview In Android

I have included my ListAdapter in my EMPLOYEE class.And list1 contains the values of Empname, Eno,Salary fetched from webservices.Now after displaying the 5 records in the employee

Solution 1:

Without seeing more code, it's difficult to be sure, but I'll hazard a guess...

When you leave an activity and come back, the framework tries to restore you to where you were using the savedInstanceState bundle. It uses this to re-create where you last were in that activity. It sounds like you have set up the list in the onCreate method and haven't checked for a savedInstanceState bundle, so when you come back to the activity the framework is restoring your list and then proceeds into your code and re-creates the list (in this case adding the same data again).

Try wrapping your list creation code in an if that checks for the existence of the savedInstanceState bundle.

Like this:

@OverridepublicvoidonCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   if (savedInstanceState == null) {
       // do your list setup here
   }
 }

Using that, if you come back to the activity and the framework saved your state, it will simply restore it and not run through your list creation code.

I know this doesn't answer the actual question, but it should solve the root issue (duplicating list data on return to activity).

Post a Comment for "How To Clear My Listview In Android"