I have a very basic question. I am trying to read values of three EditText fields and save them as one item in an arraylist using an arrayadapter. My question is how can I group the three variables I read from the EditTexts and add them as a single item in the arraylist?
To add an object to the ArrayList, we call the add() method on the ArrayList, passing a pointer to the object we want to store. This code adds pointers to three String objects to the ArrayList... list. add( "Easy" ); // Add three strings to the ArrayList list.
We can use the Object class to declare our ArrayList using the syntax mentioned below. ArrayList<Object> list = new ArrayList<Object>(); The above list can hold values of any type. The code given below presents an example of the ArrayList with the Objects of multiple types.
class editTextString{
private String str1
private String str2
private String str3
public editTextString(String data1,String data2,String data3){
str1 = data1;
str2 = data2;
str3 = data3;
}
}
now add this class to ArrayList..
just like below,
ArrayList<editTextString> list = new ArrayList<editTextString>();
editTextString data = new editTextString("edit1","edit2","edit3")
list.add(data)
You can create a custom object that holds the strings from 3 edittexts
And the array list can be
public class CustomObj{
String str1;
String str2;
String str3;
public CustomObj(String s1,String s2,String s3){
this.str1 = s1;
this.str2 = s2;
this.str3 = s3;
}
}
ArrayList<CustomObj> customObjList = new ArrayList<CustomObj>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With