I have a question about populating arrays. In my Android app in one activity I enter the title and the description of my note and I want to add these titles and descriptions to the arrays in another activity respectively. Now it is done in a dummy way, statically. I want to do this dynamically. So, I guess there must be loops and I must be able to add as many notes as I want.
Asked
Active
Viewed 494 times
2 Answers
3
Use Intent mechanism:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("title", title);
intent.putExtra("description", desc);
In your SecondActivity:
Intent intent = getIntent();
array[i++] = new MyElement(intent.getExtra("title"), intent.getExtra("description"));

Vladimir Ivanov
- 42,730
- 18
- 77
- 103
-
Tell me more about the array thing, cause in my first activity I have exactly what you wrote. But in second... So, I have to write sth like `code`String[] array; (String s: array) { array[i++] = new MyElement(intent..getExtras().getString("Title"), intent.getExtras().getString("Body");`code` ? What is MyElement and where should I put this code at all??? – lomza Apr 01 '11 at 07:35
-
MyElement here is just a class of your element in the list. If it just a String then you don't need to have a separate class. – Vladimir Ivanov Apr 01 '11 at 07:47
-
Thanks for answering, but it's not helpful enough =( – lomza Apr 01 '11 at 08:03
1
So you want to pass an entire array to the next activity, is that right? Rather than pass the individual strings, you can pass the entire array with putStringArrayListExtra()
. Check here for an example: pass arraylist from one activity to other
Edit: Ok, then. Just extract the relevant strings from the intent, and add it to your existing array:
String newTitle = getIntent().getStringExtra("title");
mTitles.add(newTitle);
Edit2: I see you're using regular arrays, rather than lists. You can't resize arrays, so you need to allocate a new one, one string longer, and copy all the old items across. Something like this:
String[] newTitles = new String[mTitles.length + 1];
for (int i=0;i<mTitles.length;i++) {
newTitles[i]= mTitles[i];
}
mTitles = mNewTitles;
// add the new item
mTitles[mTitles.length-1] = "the string you got from the intent";

Community
- 1
- 1
-
No,no,no! I want to pass values entered in the first activity into arrays in the second activity. – lomza Apr 01 '11 at 08:31
-
Without first putting them in an array? Why? Can you provide a use case for the two activities? – Apr 01 '11 at 08:40
-
Well...I want to put them in an array in my second activity. The code of this activity is here: http://codeviewer.org/view/code:190a . Line 108 and so on... Now I'm putting the values of a title and a body in a dummy way. – lomza Apr 01 '11 at 08:50
-
check my edit. It'd be much cleaner to use an ArrayList. Also, bear in mind that your arrays are going to get destroyed and recreated each time your activity gets created, so you need to store that data somewhere outside the activity's lifecycle. – Apr 01 '11 at 09:04
-
@danH, thank you so much!!! Yes, I'm aware of destruction... In ideal, I would use SQLite DB, but as for now, arrays will be enough. – lomza Apr 01 '11 at 09:15
-
@danH Hmm... copying an array looks really trivial. In case I have used an ArrayList, I would write private ArrayList
mTitlesList = new ArrayList – lomza Apr 01 '11 at 09:32(); mTitlesList.add(title); , right? But then I encounter another problem - I can't add the title in the same place I declared my array and how can I check whether I have data to pass or not yet? -
Ah, I won't write this... Too much modifications with indexes and so on. Thank you for your help anyway! – lomza Apr 01 '11 at 09:36