0

首先,我在我的应用程序中使用共享首选项将数据从一个活动发送到另一个活动,当在第一个活动中单击列表视图时,但是当我单击另一个列表时遇到问题,另一个列表中的详细信息与第一个相同.
当我尝试使用意图时,它仍然是一样的。我用过:

settings.edit().clear().commit();

settings.edit().remove().commit();

但它不起作用。有什么不对吗?这是我的第一个活动:

lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
//              SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
//              settings.edit().clear().commit();
//              settings.edit().remove("nama").commit();
//              settings.edit().remove("alamat").commit();
//              settings.edit().remove("ket").commit();
//              settings.edit().remove("img_id").commit();
//              settings.edit().remove("telp").commit();
//              settings.edit().remove("begdate").commit();
//              settings.edit().remove("enddate").commit();

                Intent detail= new Intent (TerbaruSimasCard.this, TerbaruDetail.class);
                detail.putExtra("nama", nama);
                detail.putExtra("alamat",alamat);
                detail.putExtra("ket", ket);
                detail.putExtra("telp",telp);
                detail.putExtra("begdate", begdate);
                detail.putExtra("enddate",enddate);
                detail.putExtra("img_id", img_id);

//              SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
//              settings.edit().clear().commit();
//              startActivity (detail);
//              Intent detail= new Intent (TerbaruSimasCard.this, TerbaruDetail.class);
                startActivity (detail);

//              SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
//              SharedPreferences.Editor editor = settings.edit();
//              editor.putString("nama", nama);
//              editor.putString("alamat",alamat);
//              editor.putString("ket", ket);
//              editor.putString("telp",telp);
//              editor.putString("begdate", begdate);
//              editor.putString("enddate",enddate);
//              editor.putString("img_id", img_id);
//              editor.commit();
//              System.out.println(nama+"nama");

            }

这是我的第二个活动:

Intent detail= getIntent();
        nama=detail.getStringExtra("nama");
        alamat= detail.getStringExtra("alamat");
        ket= detail.getStringExtra("ket");
        img_id= detail.getStringExtra("img_id");
        telp= detail.getStringExtra("telp");
        begdate= detail.getStringExtra("begdate");
        enddate= detail.getStringExtra("enddate");

//      SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
//      settings.edit().clear().commit();
//      settings.edit().remove("nama").commit();
//      settings.edit().remove("alamat").commit();
//      settings.edit().remove("ket").commit();
//      settings.edit().remove("img_id").commit();
//      settings.edit().remove("telp").commit();
//      settings.edit().remove("begdate").commit();
//      settings.edit().remove("enddate").commit();
//      nama= settings.getString("nama", null);
//      alamat = settings.getString("alamat", null);
//      ket= settings.getString("ket", null);
//      img_id = settings.getString("img_id", null);
//      telp= settings.getString("telp", null);
//      begdate = settings.getString("begdate", null);
//      enddate = settings.getString("enddate", null);

我已经强制停止,清除了我的应用程序的缓存并将其卸载,但它仍然无法正常工作。我的方法有问题吗?

4

2 回答 2

1

看看这段代码:

设置.edit()

每次使用此代码时,都会有一个新的编辑器。所以你应该这样做:

Editor editor = setting.edit();
editor.remove("nama");
......
editor.remove("enddate");
editor.commit();

祝你好运。enter code here

于 2013-08-26T10:04:43.657 回答
1

如果您想SharePreference通过清除editor

    editor.clear();
    editor.commit();
于 2013-08-26T09:52:22.763 回答