Balaji
Balaji

Reputation: 117

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.

Upvotes: 1

Views: 146

Answers (3)

Kaveesh Kanwal
Kaveesh Kanwal

Reputation: 1773

I think you can create a custom model for storing info about only delhi users.

public class CustomList{
private ArrayList<Abc> abc;
public CustomList(ArrayList<Abc> abc){
this.abc = abc;
}
public void setAbc(ArrayList<Abc> abc){
this.abc = abc;
}

public ArrayList<Abc> getAbc(){
return abc;
}
}

Now, you can add your code and check for Delhi users:

ArrayLis<CustomList> customList = new ArrayList<CustomList>();
for (int i = 0; i < abcs.size(); i++) {
      if(abcs.get(i).getState().equals("Delhi")){
            customList.add(new CustomList(abcs));
      }
    }

retrieve by: customList.get(position).getAbc();

Upvotes: 1

Jack
Jack

Reputation: 193

Again change code to

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

Upvotes: 3

Piyush
Piyush

Reputation: 18923

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"

Upvotes: 2

Related Questions