ArrayList in ArrayList<String> - android

ArrayList<ArrayList<String>> mainlist = new ArrayList<ArrayList<String>>();
ArrayList<String> childlist = new ArrayList<String>();
for (int i = 0; i < 3 i++){
String chlidtext = chlidlist + String.valueof(i);
String maintext = mainlist +String.Valueof(i);
childlist.add(chlidtext);
mainlist.add(maintext);
}
return mainlist;
i dont have an idea how to get 3rd element in every chlidlist ?

You can access 3rd element of childList by:
mainList.get(index).get(2); // Here 1st get() will get the 1st element of mainList which conatins 1st child element and 2nd get() will get the 3rd element of childlist.

Related

How to fix listview value single string have multiple value for android

String sessionId = getIntent().getStringExtra("numbers");
final String[] values = new String[]{sessionId};
ArrayList list = new ArrayList<>(Arrays.asList(values));
final ArrayList arrayList = new ArrayList();
for (int i = 0; i < values.length; i++)
{
Log.d(TAG, "listValue -" + values[i]);
arrayList.add(values[i]);
}
listView = (ListView)findViewById(R.id.textView2);
listView.setAdapter(new
ArrayAdapter(ListDisplayActivity.this,R.layout.list_display,R.id.text, arrayList));
How to fix listview value single string have multiple values for android?
You can pass values like
intent.putStringArrayListExtra("sessionIds",yourSessionIdList);
and get like
ArrayList<String> sessionIds = getIntent().getStringArrayListExtra("sessionIds");
Check the below link: You may need to use custom adapter to display multiple values in each row of listview:
Listview with custom adapter
And I also recommend you to use recyclerview instead of listview

get value from Dynamically add multiple spinner in android?

How to get value to dynamically add multiple spinner where spinner id is same.i used 'String spin = parent.getSelectedItem().toString();' but i get all time last spinner value.Plz help me?
you can create array list of spinner
ArrayList<Spinner> listSpinner = new ArrayList<>();
listSpinner.add(spinner1);
listSpinner.add(spinner2);
listSpinner.add(spinner3);
for(int i=0;i<listSpinner.size();i++){
String p1 = listSpinner.get(i).getSelectedItem();
}
get different value for all spinner out side for loop and store it in arraylist.
ArrayList<Spinner> listSpinner = new ArrayList<>();
ArrayList<ArrayList<String>> s‌​tatus_list = new ArrayList<>();
status_data.add("" + snapshot.getValue());// getting data
s‌​tatus_list.add(status_data);
for (int i = 0; i < 10; i++) {
View v = LayoutInflater.from(AssetCodingDetails.this).inflate(R.layou‌​t.custom_asset_codin‌​g_label, null);
Spinner spinner_status = (Spinner) v.findViewById(R.id.spinner_status);
ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), R.layout.spinner_item, s‌​tatus_list.get(i));
spinner_status.setAdapter(adapter);
listSpinner.add(spinner_status);
}
for(int i=0;i<listSpinner.size();i++){
String p1 = listSpinner.get(i).getSelectedItem().toString();
}

Multiple Strings in a Single TextView

I have two List<String> LIST A and LIST B.Each List<String> have 10 lines. I need to display these Strings in TextView. But it should be like line by line.
Example:
LIST A - line 1 ( display first line of LIST A)
LIST B - line 1 ( display first line of LIST B)
LIST A - line 2 ( display 2nd line of LIST A)
LIST B - line 2 ( display 2nd line of LIST B)
LIST A - line 3 ( display 3rd line of LIST A)
LIST B - line 3 ( display 3rd line of LIST B)
etc....etc....
Please suggest me a solution.
Just iterate your lists and use textview.append("yourline"); as per your convenient.
int listSize=10;
List<String> A = new ArrayList<String>(listSize),B = new ArrayList<String>(listSize);
for(int i=0;i<listSize;i++){
textview.append(A.get(i)+"\n"+B.get(i)+"\n");
}
I merged the both list and displayed the items in textview by using HTML and it worked
Just try with this example below. This is the answer with the output you want.
public class test extends AppCompatActivity {
public ArrayList<String> alldata;
TextView test;
String str;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
test = (TextView) findViewById(R.id.test);
List<String> stockList = new ArrayList<String>();
stockList.add("stock1");
stockList.add("stock2");
List<String> stockList2 = new ArrayList<String>();
stockList.add("stock3");
stockList.add("stock4");
stockList.addAll(stockList2);
String[] stockArr = new String[stockList.size()];
stockArr = stockList.toArray(stockArr);
alldata = new ArrayList<String>();
for (int i = 0; i < stockArr.length; i++) {
str = stockArr[i].substring(0, stockArr[i].length()) + "<br>";
alldata.add(str);
str = "";
}
StringBuilder PostItems = new StringBuilder();
for (int i = 0; i < alldata.size(); i++) {
PostItems.append(alldata.get(i));
}
String StrigItem = String.valueOf(PostItems);
test.setText(Html.fromHtml(StrigItem));
}
}

Unable to fetch data from Hashmap properly

I have an Arraylist of HashMap. Each HashMap element contains two columns: column name and corresponding value. This HashMap will be added into a ListView with 3 TextView.
I populate the ArrayList as follows, and then assign that to an adapter in order to display it:
ArrayList<HashMap<String, String>> list1 = new ArrayList<HashMap<String, String>>();
HashMap<String, String> addList1;
for (int i = 0; i < count; i++) {
addList1 = new HashMap<String, String>();
addList1.put(COLUMN1, symbol[i]);
addList1.put(COLUMN2, current[i]);
addList1.put(COLUMN3, change[i]);
list1.add(addList1);
RecentAdapter adapter1 = new RecentAdapter(CompanyView.this,
CompanyView.this, list1);
listrecent.setAdapter(adapter1);
}
.
Now on listItemClick, the fetched data is of the different form at different time.
For eg. My list contains following data:
ABC 123 1
PQR 456 4
XYZ 789 7
i.e. When I log the fetched string after clicking 1st list item, I get one of the several outputs:
{1=ABC ,2=123 ,3=1}
{First=ABC ,Second=123 ,Third=1}
{1=123 ,0=ABC ,2=1}
and even
{27=123 ,28=1 ,26=ABC}
Initially I used:
int pos1 = item.indexOf("1=");
int pos2 = item.indexOf("2=");
int pos3 = item.indexOf("3=");
String symbol = item.substring(pos1 + 2,pos1 - 2).trim();
String current = item.substring(pos2 + 2, pos3 - 2).trim();
String change = item.substring(pos3 + 2, item.length() - 1).trim();
Then for the 4th case, I have to use:
int pos1 = item.indexOf("26=");
int pos2 = item.indexOf("27=");
int pos3 = item.indexOf("28=");
String symbol = item.substring(pos1 + 3, item.length() - 1).trim();
String current = item.substring(pos2 + 3, pos3 - 3).trim();
String change = item.substring(pos3 + 3, pos1 - 3).trim();
So that I get ABC in symbol and so on.
But, by this approach, application loses it's reliability completely.
I also tried
while (myVeryOwnIterator.hasNext()) {
key = (String) myVeryOwnIterator.next();
value[ind] = (String) addList1.get(key);
}
But it's not giving proper value. Instead it returns random symbol for eg. ABC or PQR or XYZ.
Am I doing anything wrong?
Thanks in advance!
The HashMap's put function does not insert value in specific order. So the best way is to put the keyset of the HashMap in a ArrayList and use the ArrayList index in retrieving the value
ArrayList<HashMap<String, String>> list1 = new ArrayList<HashMap<String, String>>();
HashMap<String, String> addList1;
ArrayList<String> listKeySet;
for (int i = 0; i < count; i++) {
addList1 = new HashMap<String, String>();
addList1.put(COLUMN1, symbol[i]);
addList1.put(COLUMN2, current[i]);
addList1.put(COLUMN3, change[i]);
listKeySet.add(COLUMN1);
listKeySet.add(COLUMN2);
listKeySet.add(COLUMN3);
list1.add(addList1);
RecentAdapter adapter1 = new RecentAdapter(CompanyView.this,
CompanyView.this, list1);
listrecent.setAdapter(adapter1);
}
And when retrieving use
addList1.get(listKeySet.get(position));
Here, the arraylist listKeySet is just used to preserve the order in which the HashMap keys are inserted. When you put data in HashMap insert the key into the ArrayList.
I don't think using HashMap for this purpose is a good idea. I would implement Class incapsulating your data like
class myData {
public String Column1;
public String Column2;
public String Column3;
// better idea would be making these fields private and using
// getters/setters, but just for the sake of example these fields
// are left public
public myData(String col1, String col2, String col3){
Column1 = col1;
Column2 = col2;
Column3 = col3;
}
}
and use it like
ArrayList<myData> list1 = new ArrayList<myData>();
for (int i = 0; i < count; i++) {
list1.add(new myData(symbol[i], current[i], change[i]));
}
//no need to create new adapter on each iteration, btw
RecentAdapter adapter1 = new RecentAdapter(CompanyView.this,
CompanyView.this, list1);
listrecent.setAdapter(adapter1);
You will need to make changes in your adapter to use myData instead of HashMap<String,String>, of course.

How to add one arraylist to other arraylist in android?

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

Categories

Resources