Skip to content Skip to sidebar Skip to footer

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 hereList = newArrayList<String>();         
    Map<String, ?> prefsMap = preferences.getAll();
    for (Map.Entry<String, ?> entry: prefsMap.entrySet()) {
        List.add(entry.getValue().toString());      
    } 

    // correction here
    arrayAdapter = newArrayAdapter<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 = newArrayList<String>();
List<String> List = newList<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
            publicvoidonClick(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"