开发者

Passing entered values into arrays

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 开发者_如何学JAVAguess there must be loops and I must be able to add as many notes as I want.


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"));


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";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜