Nikhil Nikhil - 2 months ago 11
Java Question

storing arraylist from existing arraylist in android

Below is the List Class.

public class Abc {

String pincode, state, name, phNo;

public Abc(String pincode, String state, String name, String phNo) {
this.pincode = pincode;
this.state = state;
this.name = name;
this.phNo = phNo;
}

public String getPincode() {
return pincode;
}

public void setPincode(String pincode) {
this.pincode = pincode;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPhNo() {
return phNo;
}

public void setPhNo(String phNo) {
this.phNo = phNo;
}
}


I am adding the data to the list.

List<Abc> abcs = new ArrayList<>();
List<Abc> customList = new ArrayList<>();
abcs.add(new Abc("600025", "Delhi", "gambhir", "565632552"));
abcs.add(new Abc("600026", "Punjab", "yuvi", "565632553"));
abcs.add(new Abc("600027", "Punjab", "bhajji", "565632554"));
abcs.add(new Abc("600028", "Delhi", "kohli", "565632555"));
abcs.add(new Abc("600029", "Karnataka", "dravid", "565632556"));
abcs.add(new Abc("600030", "Delhi", "rohit", "565632557"));
abcs.add(new Abc("600031", "Kerla", "sreeshanth", "565632558"));


I need a separate list who lives in Delhi how to achieve this logic.
I tried like this

for (int i = 0; i < abcs.size(); i++) {
if(abcs.get(i).getName().equals("Delhi")){
customList.addAll(i,abcs);
}
}


But in my customList no data is added. Can anyone help me.

Answer

This condition

if(abcs.get(i).getName().equals("Delhi")){

should be

if(abcs.get(i).getState().equals("Delhi")){

because you'r storing state name in second parameter and in "name" variable there is not value for "Delhi"