0

Hello everyone I've been struggling for 3 days to find a solution to this does anyone can point me to the right direction? My problem is as follows:

What I have: 1. A listView populated through Objects of type "Sales" 2. A single row is constructed with a checkbox and 2 strings with name and email. 3. I'm extending from BaseAdapter

What I need: - When the user push a button called "next"(leads to a new activity) all the checked objects must remain selected an only those should be visible in the next activity.

Im not talking about passing primitive datatypes instead I'm talking about passing and maintaining a reference to all selected (checked) Objects in a list in another activity

What I have tried -I have created a static ArrayList and tryied to save all the selected objects in the Listener of the checkbox when the user checks a box I add this object to my static array but is not working it gives a NULL array when I try to read it back from another activity.

  • I tried to save it through SharedPreferences but it only saves the last index of the array.

My short and generic question would be: ¿Where do I look to save a selected object from a checkbox listener or how can I maintain only the ones selected in another activity?

this is my checkBoxListener not sure how can I save the selected object

chkBoxUser.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                if (chkBoxUser.isChecked())
                {
                     //saveData(obj, obj.saleId);

                    ArrayList<SaleModel> saleSelected = new ArrayList<>();
                    saleSelected.add(obj);

                    Toast.makeText(context, "Object added!: " + saleSelected.get(0), Toast.LENGTH_SHORT).show();

                }
                else
                {
                    saleSelected.remove(obj);
                    Toast.makeText(context, "UNChecked: " + obj.customerFirstName, Toast.LENGTH_SHORT).show();
                }

            }
        });
  • But that's for primitive data types mine is for objects and inside a checkbox listener to another activity. – Angel Pat Martin Nov 29 '18 at 15:56
  • post your code here. what have you tried till yet – nimi0112 Nov 29 '18 at 15:59
  • 1
    @AngelPatMartin it's the same for parcelable and serializable objects – Stanislav Bondar Nov 29 '18 at 16:02
  • in short, implement parcelable interface on your object. Pass it in a bundle (if its not too large). Then read that data using the Intent extras (Bundle) from the activity that is being launched. There are plenty of tutorials on this across the internet. – kandroidj Nov 29 '18 at 16:14

1 Answers1

1

Your data is simple. You dont need to pass the object. Instead, you can save per object as JSONObject. Every checked item, you save it to a JSONArray.

Now, call your next activity with an intent that has string parameter. The string parameter should contain the string representation of your JSONArray.

In your next activity, do the reverse by parsing the string paramater and put it back to a JSONArray object. Finally, recreate your initial objects.

If you need code, let me know.

user1506104
  • 6,554
  • 4
  • 71
  • 89