Android:清除存储在sharedPreferences中的列表数组

时间:2014-06-10 21:37:40

标签: java android

我目前正在listArray中存储sharedPreferences,然后我在listArray中显示此ListView,如下所示

public class Savelistarray extends ListActivity {

    private static final String SHARED_PREFS_NAME = "MY_SHARED_PREF";

    public static ArrayList<String> mylist = new ArrayList<String>();
    ArrayAdapter<String> adapter;

    public void advancenext (View view) {
        Intent intent = new Intent(SaveScores.this, Splash.class);
        SaveScores.this.startActivity(intent);
    }

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

        setupActionBar();

        ListView listview = (ListView) findViewById(android.R.id.list);

        mylist = (ArrayList<String>) getArray();

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mylist);

        mylist.add(String.valueOf(PreviousActivity.value));
        adapter.notifyDataSetChanged();

        listview.setAdapter(adapter);        

    }

    public boolean saveArray() {

        SharedPreferences sp = this.getSharedPreferences(SHARED_PREFS_NAME, Activity.MODE_PRIVATE);
        SharedPreferences.Editor mEdit1 = sp.edit();
        Set<String> set = new HashSet<String>();
        set.addAll(mylist);        
        mEdit1.putStringSet("list", set);

        return mEdit1.commit();     

    }

    public List<String> getArray() {
        SharedPreferences sp = this.getSharedPreferences(SHARED_PREFS_NAME, Activity.MODE_PRIVATE);
        Set<String> set = sp.getStringSet("list", new HashSet<String>());
        return new ArrayList<String>(set);
    }

    public void onStop() {
        saveArray();
        super.onStop();
        SaveScreen.datevalue = null;
    }

}

现在,我的目标是通过点击按钮listArray,让用户可以选择从其他活动重置此ListViewreset 。这就是我到目前为止所做的事情

public class NextActivity extends Activity {

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

        final Button reset = (Button)findViewById(R.id.reset);

        reset.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Savelistarray.mylist.clear();

                //New Code
                SharedPreferences.Editor mEdit1.remove("mylist");
                SharedPreferences sp.getStringSet("list", new HashSet<String>());
            }

        });

    }

}

但是,这不会清除listArrayListView。有谁知道如何处理这个问题?

编辑在更新//New Code中,我收到错误The method remove(String) is undefined for the type new View.OnClickListener(){}The method getStringSet(String, HashSet<String>) is undefined for the type new View.OnClickListener(){}

2 个答案:

答案 0 :(得分:0)

你可以试试这个 - 在Next活动中使用sharedpreference将变量设置为1。覆盖savelistarray活动中的onResume()函数并从sharedpreferences获取变量,现在清除列表并将0存储在变量中。

答案 1 :(得分:0)

我还没有机会测试这个,这就是为什么我现在想把它留作评论,但你似乎遇到了一些麻烦,所以我想我至少会给你一个开始。 / p>

public class NextActivity extends Activity implements OnClickListener{

    private SharedPreferences sp;
    private SharedPreferences.Editor mEdit1;
    private Set<String> set;

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

        Button reset = (Button)findViewById(R.id.reset);
        reset.setOnClickListener(this);

        sp = this.getSharedPreferences(SHARED_PREFS_NAME,  Activity.MODE_PRIVATE);
        mEdit1 = sp.edit();
        set = new HashSet<String>();
    }

    @Override
    public void onClick(View v){
        mEdit1.remove("mylist").commit();

        // add this if you want to check and make sure that you
        // have removed your key value, if it has worked, set will
        // be set to a new, empty Set String, if not, it set will
        // be the old key value
        set = sp.getStringSet("list", new HashSet<String>());
    }

}

在onClick方法中,您也可以尝试

mEdit1.putStringSet("list", new HashSet<String>()).commit();

而不是

mEdit1.remove("list").commit();

我想如果两者都有效,那真的取决于你。

测试它,看它是否有效。如果确实如此,我会就实际情况给出更详细的答案。如果没有,请告诉我什么不起作用,我应该能够提供帮助。