我的列表视图中只保存了1个项目

时间:2014-05-22 13:01:17

标签: sharedpreferences

我试图使用共享偏好设置保存列表视图中的项目,并且列表中只保存了1个项目,虽然我在这里输入了许多我用来做的小代码帽

公共类MainActivity扩展了Activity {

ListView listview;
String values[] = { };
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ActionBar bar = getActionBar();
    bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#000000")));
    listview = (ListView) findViewById(R.id.listView1);

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
    listview.setAdapter(adapter);
    LoadPreferences();
    }

private void LoadPreferences() {
    SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(this);
    String dataSet = data.getString("LISTS", "None available");
    adapter.add(dataSet);
    adapter.notifyDataSetChanged();

}

String string = "tasks";
public boolean onOptionsItemSelected(MenuItem item)
{
    switch(item.getItemId())
    {
    case R.id.clear_list: adapter.add(string);
                          adapter.notifyDataSetChanged();
                          SavePreferences("LISTS", string);
    }
    return false;

}

private void SavePreferences(String key, String value) {
    SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = data.edit();
    editor.putString(key, value);
    editor.commit();

}

0 个答案:

没有答案