Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

arraylist with custom objects

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?

like image 519
androidisnotforme Avatar asked Sep 30 '11 08:09

androidisnotforme


People also ask

Can you add objects to 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.

How do you add different data types in ArrayList?

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.


2 Answers

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)
like image 130
ngesh Avatar answered Oct 13 '22 00:10

ngesh


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>();
like image 21
blessanm86 Avatar answered Oct 12 '22 22:10

blessanm86