-1

I use SharedPreferences to pass some data from Activity1 to Activity2 when button clicked. Then I add these data to ArrayList in Activity2.

//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);
   }
}

The problem is that mArrayList doesn't save Data added before. For example I click on button of 3 different images and I expect that mArrayList will have 3 Data objects. But actually it always has only last Data object. I understand why it happens - SharedPreferences overrides old data. I saw many advices like I have to use different keys but I did it (put imageResourseId as a key). I thought about ways to solve this problem. The ways that I can understand now are:

1) somehow let SharedPreferences to store previous data (I heard about using SQL database, but I don't have so many images to use database).

2) override mDrinkArrayList after receiving new data, so it basically have old data when Activity2 starts again.

3) create mDrinkArrayList in Activity1, add new Data when button clicked, and pass mDrinkArrayList to Activity2 using SharedPreference.

Could someone suggest what shoud I do in this situation? Thanks in advance.

Dzianis
  • 63
  • 7

3 Answers3

0

You can use TinyDB for this approach.

For saving list create an object and save data in your activity1

TinyDB tinydb = new TinyDB(context);

tinydb.putList("key", yourIdList));

And get your data from activity2

TinyDB tinydb = new TinyDB(context);

tinydb.getList("key");
Murat Güç
  • 377
  • 4
  • 9
  • I've already tried to use TinyDB. But it doesn't have putList() method now. It only has putListObject(). Should I use it? – Dzianis Dec 26 '18 at 07:39
  • 1
    If you are going to use it you also have to customize the method. For example you are creating a list like; `List myList = new Arraylist<>()` and go to your tinyDB class and customize the method `putListObject()`, which means replace **Objects** with **MyModel** – Murat Güç Dec 26 '18 at 07:43
  • I'll try your suggestion – Dzianis Dec 26 '18 at 07:46
0

I think it's a better solution to pass whole arraylist to another activity instead of transfer data and convert in destination. You can use Serializable data to add Intent and callback it in second activity. If you need more information about it, let me know.

siyavash
  • 168
  • 2
  • 14
0

You need to Serialized your Bean class like below :-

public class YOUR_BEAN_CLASS implements Serializable{


}

And than after in your Activity1 u need to just pass , yours Bean OR Pojo like below

  startActivity(new Intent(mContext,
  SECOND_ACTIVITY.class).putExtra("YOUR_BEAN", (Serializable) 
  new ArrayList<YOUR_BEAN_CLASS>);

And than in the second class Activity2 u get the "YOUR_ARRAYLIST" as follow

ArrayList<YOUR_BEAN_CLASS> categoryList = (ArrayList<YOUR_BEAN_CLASS>)getIntent().getSerializableExtra("category");
Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103