5

I have created 3 arraylists and stored data in it. I need to pass one single arraydata to other page and so I have added my individual arrays to the main array. But when I added one array to other the data is not being aded to that array. Here is my code:

static ArrayList<ArrayList<String>> stringList1=new ArrayList<ArrayList<String>>();
static ArrayList<ArrayList<String>> stringList3; 
stringList1 = Mypage.stringList1;
 ArrayList<String> optionlist = new ArrayList<String>();
stringList3 = new ArrayList<ArrayList<String>>();stringList3 = dbAdapter.selectRecordsFromDBList(query2, null); 
 for (int i = 0; i < stringList3.size(); i++) {
 optionlist = stringList3.get(i);
 System.out.print("option list size");
 System.out.print(optionlist.size());
 stringList1.add(optionlist);
 System.out.println("total stringlist1"+stringList1.get(0));

I am getting the stringList1 array values from Mypage and accessing that in new page. In the new page I am trying to add the optionlist array to stringList1 by giving stringList1.add(optionlist) but the data is not adding. Where I went wrong? Please help me regarding this... Thanks in Advance

asked Dec 29, 2011 at 10:29
1

3 Answers 3

15

Use addAll() It appends all of the elements in the specified ArrayList to the end of this list, in the order that they are returned by the specified Collection's Iterator.

use stringList1.addAll(optionlist); instead of stringList1.add(optionlist);

answered Dec 29, 2011 at 10:31
0
4

After seeing your code I can guess that you have problem that elements of arrayList is not copy in another arrayList.If this is your issue then change your code like below

static ArrayList<ArrayList<String>> stringList1=new ArrayList<ArrayList<String>>();
static ArrayList<ArrayList<String>> stringList3; 
stringList1 = Mypage.stringList1;
ArrayList<String> optionlist;
stringList3 = new ArrayList<ArrayList<String>>();
stringList3 = dbAdapter.selectRecordsFromDBList(query2, null); 
 for (int i = 0; i < stringList3.size(); i++) {
 optionlist = new ArrayList<String>();
 optionlist = stringList3.get(i);
 System.out.print("option list size");
 System.out.print(optionlist.size());
 stringList1.add(optionlist);
 System.out.println("total stringlist1"+stringList1.get(0));
 }
answered Dec 29, 2011 at 10:54
0
2

The problem seems that you are initializing your

ArrayList<String> optionlist;

only once that is outside the loop. So, in that case only the first ArrayList gets stored. So, initialized the ArrayList<String> optionlist inside the loop.

ArrayList<String> optionlist;
for (int i = 0; i < stringList3.size(); i++) {
 optionlist = new ArrayList<String>();
 optionlist = stringList3.get(i);
 stringList1.add(optionlist);
 }
answered Dec 29, 2011 at 10:35
4
  • Hi thanks for the response..I took a list but even then the same problem..plz help me Commented Dec 29, 2011 at 10:44
  • I can't get you what you are trying to do actually, so please can you explain more. Commented Dec 29, 2011 at 10:47
  • I am getting values from stringlist1 from page1 to page2 using stringlist1 = page1.stringlist1. Now I need to add my optionlist array which is in page2 to stringList1 arraylist. But the data is not adding to stringlist1.What is the mistake I am doing? Commented Dec 29, 2011 at 10:52
  • hmmm the problem is you are only initializing your ArrayList only once outside the loop check my edited answer. Commented Dec 29, 2011 at 11:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.