Reputation: 215
I have created a ListView which is populated with an XMLParser and the user is allowed to select multiple values from the List using checkboxes and then can go to the next activity by pressing a button. On the Activity 2 I want to retrieve all of those values which were selected.
To get all of the selected values I use: String selected += "\n- " +c.getName();
where c is final ConfigOptions c = options.get(position);
Also I declared it outside of onCreate method like this
public static String selected = null;
But I have same issues
The problem is that on Activity 2 I get all the selected values but one of the value is null
EX: null \n -Servotronic \n -Park distance control \n - Speed limit info
Also, if I try to reopen Activity 1 and select new items again, on the second activity the old activities which were selected before are there too. Even if I set the value of the string to null, I still get all of them there.
So how can I concatenate the values of this string to not get anymore the null value in the next activity and also to be able to reset the value so I can make the selection again ?
Upvotes: 0
Views: 879
Reputation: 38605
When the user wants to go to Activity 2, make a String array to hold the number of selected items. Add it as an extra in your intent to start Activity 2.
int count = // get number of selections
String[] options = new String[count];
for (int i = 0; i < count; i++) {
options[i] = // ith selection
}
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("options", options);
startActivity(intent);
In Activity 2's onCreate:
String[] options = getIntent.getStringArrayExtra("options");
if (options != null) {
// process the strings here.
}
Upvotes: 1
Reputation: 5919
You can use Android Bundle
to accomplish the task and in a better way. Create a ArrayList of the items to pass, add the ArrayList to the Bundle
and send the Bundle
in the Intent
call.
Upvotes: 1