Reputation: 129
I have my 2 activities set up and linked.
What I'm trying to do:
Take a generated text view in my first activity, convert it to a string, pass it to my other activity, where is it displayed in a list view with other previous passed text views.
So in my first activity I currently have:
public void SaveMessage(View view) {
String saved = message.getText().toString();
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("message",message);
Toast.makeText(getApplicationContext(), R.string.addedfavs, Toast.LENGTH_SHORT).show();
}
However I don't really know where to go from this, I've looked at trying store the strings in arrays or then in array lists, but to no avail.
Help would be much appreciated on how to receive each string and store it in some sort of arraylist (as arrays are of fixed size) or any format that could consequently be displayed in a ListView.
Upvotes: 0
Views: 517
Reputation: 139
I could not properly understand your question, but according to my understanding, there are two solutions.
Solution 1: If you want to save previously passed texts, you can do that using a db. Whenever you pass a string from your first activity to second one, store that text in db and get all the strings from db and populate those in list. Or you can save it in arraylist and save the array list to SharedPreferences like:
public void addMessage(String message) {
if (currentMessages == null) {
currentMessages = new ArrayList<String>();
}
currentMessages.add(message);
//save the task list to preference
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
try {
editor.putString(MESSAGES, ObjectSerializer.serialize(currentMessages));
} catch (IOException e) {
e.printStackTrace();
}
editor.commit();
}
public ArrayList<String> getMessages() {
if (currentMessages == null) {
currentMessages= new ArrayList<String>();
}
// load tasks from preference
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
try {
currentMessages = (ArrayList<String>) ObjectSerializer.deserialize(prefs.getString(MESSAGES, ObjectSerializer.serialize(new ArrayList<String>())));
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
You can use this arraylist as a data source for your adapter.
Solution 2: If there are multiple messages that are passed via intent, then use this:
ArrayList<String> messages = new ArrayList<String>();
messages.add(message1);
messages.add(message2);
.
.
.
messages.add(messageN);
Then use this arraylist as a data source for your adapter.
Edited:
SHARED_PREFS_FILE can be a static final String like:
public static final String SHARED_PREFS_FILE = "sharedPrefsFile";
(Desired preferences file. If a preferences file by this name does not exist, it will be created when you retrieve an editor (SharedPreferences.edit()) and then commit changes (Editor.commit()).)
You can get ObjectSerializer class from here and add it to your project.
Upvotes: 1
Reputation: 27003
However I don't really know where to go from this,
First of all you must start the intent activity:
context.startActivity(intent);
Then, when necessary, in FirstActivity
or SecondActivity
, get the intent that started your activity with:
Intent intent = getIntent();
Now, you have also the extras with that intent. As you have the extra data as String
s, use intent.getStringExtra(String name)
method.
In your case:
String message = intent.getStringExtra("message");
I've looked at trying store the strings in arrays or then in array lists, but to no avail.
You have other extra methods like getStringArrayListExtra
to pass ArrayList<>
or getStringArrayExtra
to pass Arrays
Upvotes: 2