Android 如何保存添加到存储在SharedReference中的arraylist的数据

Android 如何保存添加到存储在SharedReference中的arraylist的数据,android,arraylist,sharedpreferences,Android,Arraylist,Sharedpreferences,单击按钮时,我使用SharedReferences将一些数据从Activity1传递到Activity2。然后我将这些数据添加到Activity2中的ArrayList //Basically I have ListView with images. //When user ckick on any image, Activity1 opens and it contains this image and it's name Activity1 { button.setOnClic

单击
按钮时,我使用SharedReferences将一些数据从
Activity1
传递到
Activity2
。然后我将这些数据添加到
Activity2
中的
ArrayList

//Basically I have ListView with images. 
//When user ckick on any image, Activity1 opens and it contains this image and it's name

Activity1 {

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           String imageName = intent.getStringExtra("name");   //imageName has different vavues, depends on which image user clicked in previous activity.
           int imageId = intent.getIntExtra("image", 0);   //imageId has different vavues, depends on which image user clicked in previous activity.
           String str = Integer.toString(imageId);   //convert imageId to use it as a key in SharedPreferences
           SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("MyPref", Context.MODE_PRIVATE);
           SharedPreferences.Editor editor = sharedPref.edit();

           editor.putString("key", str);  //save current key, 
           editor.putInt(str, receivedImage);
           editor.putString(str + "a", receivedName);
           editor.apply();
    }
}

Activity2{

   onCreate{
      SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("MyPref", Context.MODE_PRIVATE);
      String key1 = sharedPref.getString("key", null);
      String imageName = sharedPref.getString(key1 + "a", null);
      int imageId = sharedPref.getInt(key1, 0);
      mDrinkArrayList.add(new Drink(imageName, imageId));

      listAdapter = new CustomAdapter(this, mDrinkArrayList);
      listAdapter.notifyDataSetChanged();
      ListView listDrinks = findViewById(R.id.list_drinks);
      listDrinks.setAdapter(listAdapter);
   }
}
问题是
mArrayList
没有保存以前添加的
数据。例如,我单击3个不同图像的
按钮
,我希望
婚姻列表
将有3个
数据
对象。但实际上它总是只有最后一个
数据
对象。
我理解为什么会发生这种情况——SharedReference覆盖了旧数据。我看到很多建议,比如我必须使用不同的键,但我做到了(将ImageResourceId作为键)。我想办法解决这个问题。我现在可以理解的方式是:

1) 以某种方式让SharedReferences存储以前的数据(我听说过使用SQL数据库,但我并没有太多的图像可以使用数据库)

2) 在接收到新数据后覆盖
mdrinkarray列表
,因此当Activity2再次启动时,它基本上拥有旧数据

3) 在Activity1中创建
mdrinkarray列表
,单击按钮时添加新数据,并使用SharedReference将
mdrinkarray列表
传递给Activity2

有人能建议我在这种情况下该怎么办吗?提前感谢。

您可以使用此方法

要保存列表,请创建一个对象并将数据保存在活动1中

TinyDB tinydb = new TinyDB(context);

tinydb.putList("key", yourIdList));
并从活动2获取您的数据

TinyDB tinydb = new TinyDB(context);

tinydb.getList("key");

我认为将整个arraylist传递给另一个活动是一个更好的解决方案,而不是在目的地传输数据和转换。 您可以使用可序列化数据添加意图,并在第二个活动中回调它。
如果您需要有关它的更多信息,请告诉我。

您需要
序列化
您的
Bean
类,如下所示:-

public class YOUR_BEAN_CLASS implements Serializable{


}
然后在你的活动中1你只需通过,你的
Bean
Pojo
如下

  startActivity(new Intent(mContext,
  SECOND_ACTIVITY.class).putExtra("YOUR_BEAN", (Serializable) 
  new ArrayList<YOUR_BEAN_CLASS>);
startActivity(新意图)(mContext,
putExtra(“您的_BEAN”,(可序列化)
新ArrayList);
在第二节课中,你会得到如下“你的数组列表”

ArrayList<YOUR_BEAN_CLASS> categoryList = (ArrayList<YOUR_BEAN_CLASS>)getIntent().getSerializableExtra("category");
ArrayList categoryList=(ArrayList)getIntent().getSerializableExtra(“类别”);

我已经尝试使用TinyDB。但它现在没有putList()方法。它只有putListObject()。我应该使用它吗?如果要使用它,还必须自定义该方法。例如,您正在创建一个类似;
list myList=new Arraylist()的列表
转到tinyDB类并自定义方法
putListObject()
,这意味着用MyModel替换对象我将尝试您的建议我可以使用Serializable with SharedReferences吗?尝试下面的解决方案,并在出现问题时通知我