How to add a value to arraylist in android? - android

I am adding a value to my arraylist but it is showing error. Before adding the value I am displaying it first. The value is displaying but it is not adding to the arraylist. Please help me regarding this...
My Code:
static ArrayList<String> allfirstids;
ArrayList<String> list = List.get(i);
UserBO user = new UserBO();
user.firstid = Integer.parseInt(list.get(0));
user.secondid = Integer.parseInt(list.get(1));
System.out.print("Hello this is first id");
System.out.print(list.get(0));
allfirstids.add(list.get(0));
System.out.println("first ids"+allfirstids);
Thanks in advance...

before adding, please initialize arraylist.
allfirstids = new ArrayList<String>();

You forgot to initialize your static ArrayList allfirstids, Just initialize it, before using..
static ArrayList<String> allfirstids = new ArrayList<String>();
ArrayList<String> list = List.get(i);
UserBO user = new UserBO();
user.firstid = Integer.parseInt(list.get(0));
user.secondid = Integer.parseInt(list.get(1));
System.out.print("Hello this is first id");
System.out.print(list.get(0));
allfirstids.add(list.get(0));
System.out.println("first ids"+allfirstids);

Related

Split strings on string array

i have String Array like this:
String[] q1={"AAA-BBB","AAA-CCC","AAA-DDD"}
and i want result like this
temp={"BBB","CCC","DDD"}
i tried below code but the result is wrong
for(int i=0;i<q1.length;i++){
ArrayList<String> temp=new ArrayList<>(Arrays.asList(q1[i].split("AAA-")));
}
Try like this:
ArrayList<String> temp=new ArrayList<>();
for(int i=0;i<q1.length;i++){
String[] array = q1[i].split("-");
temp.add(array[1]);
}
You could use substring:
ArrayList<String> temp = new ArrayList<>();
for(int i=0; i<q1.length; i++){
temp.add(q[i].substring(q[i].indexOf('-') + 1, q[i].length()))
}
you find error Because you use split
Splits this string around matches of the given regular expression.
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
q1[i].split("AAA-")
in this line you got 2 result splited 0 = "" AND 1 = "BBB"
so you need to pick the sec result
you have multi Solution
like https://stackoverflow.com/a/50234408/6998825 said
String[] array = q1[i].split("-");
temp.add(array[1]);
//change this q1[i].split("AAA-") to
q1[0].substring(4)
if your AAA- is not going to change
Have you tried creating the ArrayList outside of the loop? As previously you were creating a new ArrayList for every element in your string array
ArrayList<String> temp = new ArrayList<>();
for(int i=0;i<q1.length;i++){
temp.add(q1[i].substring(4);
}
Assuming that "AAA-" is not going to change.

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

Android button click next display of listview of data in JSON

hi can you help me how to display the next 10 data in json by click the next button. i have 50 data and i want to display first 10. Then when I click the next button, 11-20 will display in listview. Ill post my code below and i dont have any idea how to do it. Also when i click previous button it will go back to previous listview which is 1-10. Thanks!
doctordata = new ArrayList<Map<String, String>>();
try {
jsonObject = new JSONObject(d);
jsonArray = jsonObject.optJSONArray("Doctors");
int arraylength = jsonArray.length();
for (int i = 0; i < arraylength; i++) {
Map<String, String> doctormap = new HashMap<String, String>();
JSONObject jsonChildNode = jsonArray.getJSONObject(i);
doctor = jsonChildNode.optString("Name").toString();
specialty = jsonChildNode.optString("Specialty").toString();
doctormap.put("name", doctor);
doctormap.put("specialty", specialty);
doctordata.add(doctormap);
}
String[] from = {"name", "specialty"};
int[] views = {R.id.doctorlist_name, R.id.doctorlist_specialty,};
final SimpleAdapter myadapter = new SimpleAdapter(MainActivity.this, doctordata, R.layout.doctor_list, from, views);
list.setAdapter(myadapter);
} catch (JSONException e) {
e.printStackTrace();
}
Define a class called Doctors, with fields String name and String Specialty, and add the Doctors to a list that you can iterate or convert to Array.
class Doctors {
private final String specialty;
private final String name;
public Doctors (){
specialty= "Spe1";
name = "name";
}
}
public String convertToJson(){
Gson gson = new Gson();
return gson.toJson(this);
}
Ok, there are several ways to do what do you want to achieve. I will explain you how I would do it:
Firts, in the doctorData arraylist you have all the items (50 items) that you need to show.
Create a partialDoctorData arraylist and assing to it only the first 10 items from doctorData, ok? and add this new arraylist to the SimpleAdaper.
So you will need to do instead of your code:
final SimpleAdapter myadapter = new SimpleAdapter(MainActivity.this, **partialDoctorData**, R.layout.doctor_list, from, views);
list.setAdapter(myadapter);
So when the user click in the next button, you can clean the partialDoctorData content, add from the 11-20 items from the original doctorData arrayList and and and directly call to the
myadapter.notifyDataSetChanged();
(you don't have to repeat the step to create a new SimpleAdapter, only changing the values of the arraylist and calling to this method, the content of the list is going to be updated with the content of the partialDoctorData)
Try ;)
Try this one:
Android ListView with Load More Button
You can use pagination when 10 items will be loaded after that you will call agin api to get next 10 items

Adding ArrayList to ArrayListArrayList

Suppose we have an ArrayListArrayList(String)
[[a,b,c,d] , [e,f,g,h]]
and an ArrayList(String)
[1,2,3]
How can we add ArrayList to ArrayListArrayList in position 1 at the end in order to get
[[a,b,c,d] , [e,f,g,h,1,2,3]]
Thanks
//pseudocode
List<ArrayList<String>> arraylistarraylist = [[a, b, c, d], [e, f, g, h]];
List<String> list = Arrays.asList(1,2,3);
arraylistarraylist.get(1).addAll(list);
If we name your ArrayList<ArrayList> m and if we name your ArrayList a, then you could use
m.get(1).addAll(a)
Description for this method provided by Oracle here: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
I think you need a add(int, E) method
List<ArrayList<String>> dataHolder = new ArrayList<ArrayList<String>>();
List<String> firstData = new ArrayList<String>();
firstData.add("a");
firstData.add("b");
firstData.add("c");
firstData.add("d");
List<String> secondData = new ArrayList<String>();
secondData.add("e");
secondData.add("f");
secondData.add("g");
secondData.add("h");
secondData.add("1");
secondData.add("2");
secondData.add("3");
dataHolder.add(secondData);
dataHolder.add(0, firstData); // insert your List<String> to 0 index position in dataHolder

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