How To Delete List View Items (when List Is Populated By SharedPreferences
I have a list view successfully being populated by SharedPreferences. public class FavouritesActivity extends Activity { ArrayAdapter adapter; List Li
Solution 1:
Try this..
I guess you getting NPE because your not initialize both List<String> List;
and ArrayAdapter<String> adapter;
Just replace it..
SharedPreferences preferences = this.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
//correction here
List = new ArrayList<String>();
Map<String, ?> prefsMap = preferences.getAll();
for (Map.Entry<String, ?> entry: prefsMap.entrySet()) {
List.add(entry.getValue().toString());
}
// correction here
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, List);
lv.setAdapter(arrayAdapter);
Solution 2:
Your arraylist is not initialized!
ArrayAdapter<String> adapter;
List<String> List;
to this:
ArrayAdapter<String> adapter = new ArrayList<String>();
List<String> List = new List<String>();
Also, from what i can read from your code your are using this array list only for remove things. But no things are added in this adapter since you are using another one to populate your list.
Solution 3:
Try This:
@Override
public void onClick(DialogInterface dialog, int which) {
// TO DO Auto-generated method stub
List.remove(position);
adapter.notifyDataSetChanged();
adapter.notifyDataSetInvalidated();
}
});
Post a Comment for "How To Delete List View Items (when List Is Populated By SharedPreferences"