如何删除列表视图项(当列表由SharedPreferences填充时

时间:2014-01-10 15:43:44

标签: android android-listview sharedpreferences

我有一个由SharedPreferences成功填充的列表视图。

public class FavouritesActivity extends Activity {

ArrayAdapter<String> adapter;
List<String> List;
private ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_favourites);

    lv = (ListView) findViewById(R.id.My_Favourites);

    SharedPreferences preferences = this.getSharedPreferences("MyPreferences",
                Context.MODE_PRIVATE);
    ArrayList<String> List = new ArrayList<String>();
    Map<String, ?> prefsMap = preferences.getAll();
    for (Map.Entry<String, ?> entry: prefsMap.entrySet()) {
        List.add(entry.getValue().toString());      
    } 
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, List);

    lv.setAdapter(arrayAdapter);

这很好用,但现在我要删除列表中的项目。

 lv.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                removeItemFromList(position);
                return true;
            }
        });
}

protected void removeItemFromList(int position) {
        final int deletePosition = position;
        AlertDialog.Builder alert = new AlertDialog.Builder(
                FavouritesActivity.this);

        alert.setTitle("Delete");
        alert.setMessage("Are you sure you want delete this item?");
        alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                    List.remove(deletePosition);
                    adapter.notifyDataSetChanged();
                    adapter.notifyDataSetInvalidated();
            }
        });
        alert.setNegativeButton("CANCEL",new DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        alert.show();
    }

不幸的是这部分不起作用。 AlertView显示,但选择确认会崩溃应用程序。

我感觉它与阵列适配器有关,但我很难发现它。

有人能指出我正确的方向吗?

CRASH LOG:

01-10 10:35:45.950: E/AndroidRuntime(1370): FATAL EXCEPTION: main
01-10 10:35:45.950: E/AndroidRuntime(1370): Process: com.LifeSchematics.msg, PID: 1370
01-10 10:35:45.950: E/AndroidRuntime(1370): java.lang.NullPointerException
01-10 10:35:45.950: E/AndroidRuntime(1370):     at com.LifeSchematics.msg.FavouritesActivity$2.onClick(FavouritesActivity.java:78)
01-10 10:35:45.950: E/AndroidRuntime(1370):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
01-10 10:35:45.950: E/AndroidRuntime(1370):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-10 10:35:45.950: E/AndroidRuntime(1370):     at android.os.Looper.loop(Looper.java:136)
01-10 10:35:45.950: E/AndroidRuntime(1370):     at android.app.ActivityThread.main(ActivityThread.java:5017)
01-10 10:35:45.950: E/AndroidRuntime(1370):     at java.lang.reflect.Method.invokeNative(Native Method)
01-10 10:35:45.950: E/AndroidRuntime(1370):     at java.lang.reflect.Method.invoke(Method.java:515)
01-10 10:35:45.950: E/AndroidRuntime(1370):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-10 10:35:45.950: E/AndroidRuntime(1370):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-10 10:35:45.950: E/AndroidRuntime(1370):     at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:1)

试试这个..

我猜你得到 NPE ,因为你没有初始化List<String> List;ArrayAdapter<String> adapter;

只需更换它..

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);

答案 1 :(得分:0)

您的arraylist未初始化!

ArrayAdapter<String> adapter;
List<String> List;

到此:

ArrayAdapter<String> adapter = new ArrayList<String>();
List<String> List = new List<String>();

另外,根据我从您的代码中读取的内容,您只使用此数组列表来删除内容。但是,由于您使用另一个填充列表,因此此适配器中没有添加任何内容。

答案 2 :(得分:0)

试试这个:

           @Override
            public void onClick(DialogInterface dialog, int which) {
                // TO DO Auto-generated method stub

                    List.remove(position);
                    adapter.notifyDataSetChanged();
                    adapter.notifyDataSetInvalidated();


            }
        });