Android:在ArrayList中保存值

Android:在ArrayList中保存值,android,arraylist,save,sharedpreferences,Android,Arraylist,Save,Sharedpreferences,我想使用SharedReference在ArrayList中保存值。然后,我想从另一个类调用ArrayList中的值,但它不保存。我该怎么做?有共享的参考资料吗?要保存文件吗?还是创建Sqlite?谢谢你的帮助 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main

我想使用SharedReferenceArrayList中保存值。然后,我想从另一个类调用ArrayList中的值,但它不保存。我该怎么做?有共享的参考资料吗?要保存文件吗?还是创建Sqlite?谢谢你的帮助

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

    final Button saveButton = (Button)findViewById(R.id.button);
    final Button delButton = (Button)findViewById(R.id.delButton);
    final ListView listView = (ListView)findViewById(R.id.listView);
    final EditText editText = (EditText)findViewById(R.id.editText);
    final ArrayList<String> arrayList = new ArrayList<String>();

    SharedPreferences sharedPref = this.getPreferences(Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = this.getPreferences(Activity.MODE_PRIVATE).edit();
    editor.putString("numbers", arrayList.toString());
    editor.commit();

    String arrayString = sharedPref.getString("numbers", null);

    final ArrayAdapter<String> arrayAdapter;
    arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,arrayList);
    listView.setAdapter(arrayAdapter);

    saveButton.setOnClickListener(new View.OnClickListener()
    {

        public void onClick(View v)
        {
            String str = editText.getText().toString();
            Integer cout = listView.getCount()+ 1;
            String str1 = cout.toString().concat("."+str);
            arrayList.add(listView.getCount(), str1);

            arrayAdapter.notifyDataSetChanged();
            Toast.makeText(getApplicationContext(), "Data Saved", Toast.LENGTH_SHORT).show();
            editText.setText(" ");
        }
    });

    delButton.setOnClickListener(new View.OnClickListener()
    {

        public void onClick(View v)
        {
            arrayList.remove(arrayList.size()-1);
            arrayAdapter.notifyDataSetChanged();
        }
    });

}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
最终按钮saveButton=(按钮)findViewById(R.id.Button);
最终按钮delButton=(按钮)findViewById(R.id.delButton);
最终ListView ListView=(ListView)findViewById(R.id.ListView);
final EditText EditText=(EditText)findViewById(R.id.EditText);
最终ArrayList ArrayList=新ArrayList();
SharedPreferences sharedPref=this.getPreferences(Activity.MODE\u PRIVATE);
SharedReferences.Editor编辑器=this.getPreferences(Activity.MODE_PRIVATE).edit();
putString(“numbers”,arrayList.toString());
commit();
String arrayString=sharedPref.getString(“数字”,null);
最终阵列适配器阵列适配器;
arrayAdapter=新的arrayAdapter(这是android.R.layout.simple\u list\u item\u 1,arrayList);
setAdapter(arrayAdapter);
saveButton.setOnClickListener(新视图.OnClickListener()
{
公共void onClick(视图v)
{
String str=editText.getText().toString();
整数cout=listView.getCount()+1;
字符串str1=cout.toString().concat(“.”+str);
add(listView.getCount(),str1);
arrayAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(),“数据已保存”,Toast.LENGTH\u SHORT.show();
editText.setText(“”);
}
});
delButton.setOnClickListener(新视图.OnClickListener()
{
公共void onClick(视图v)
{
arrayList.remove(arrayList.size()-1);
arrayAdapter.notifyDataSetChanged();
}
});
}

我想你不能这样做
编辑器.putString(“numbers”,arrayList.toString())
因为object.toString()没有将ArrayList转换为字符串,所以它只会生成一些奇怪的东西,比如“@Object3223rw”,请看这篇文章,而不是保存列表:

editor.putString("List", TextUtils.join(",", myList));
获取列表:

String serialized = sharedPref.getString("List", null);
myList = Arrays.asList(TextUtils.split(serialized, ","));

列表
数组列表
有效的观察,因为
asList
返回
List
。如果您有使用set的重复字符串,托法西奥的答案就不起作用。

我的理解是,使用SharedReference仅用于保存简单的键值对(bool、float、int、long和string),而不是作为数据存储。如果需要保存更复杂的数据对象(包括列表和数组),则需要序列化/反序列化并保存在磁盘上


这是一个错误。不兼容的类型。必需:java.util.ArrayList Found:java.util.listmake this
List ArrayList=new ArrayList()如果我这样做,它就不能添加和删除值,因为它给出了错误:“变量'arrayList'是通过内部类访问的,需要声明为final”make
arrayList
实例字段。