2016-09-16 5 views
1

Unten ist die Liste Klasse.speichern arraylist aus bestehenden arraylist in android

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

Ich füge die Daten der Liste hinzu.

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")); 

Ich brauche eine separate Liste, die in Delhi lebt, wie man diese Logik erreicht. Ich habe versucht, wie diese

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

Aber in meinem Custom keine Daten hinzugefügt. Kann mir jemand helfen.

+0

versuchen, das ABC-Klasse-Objekt in Ihrer Custom setzen. ** customList.add (abcs.get (i)); ** –

+0

warten .. Ich werde überprüfen .. – Balaji

+0

Diese Bedingung 'if (abcs.get (i) .getName(). Ist gleich (" Delhi ")) {'sollte' sein if (abcs.get (i) .getState(). equals ("Delhi")) {' – Piyush

Antwort

2

Diese Bedingung

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

sollte

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

sein, weil you'r Zustandsnamen in zweitem Parameter zu speichern und in "name" variable dort nicht Wert "Delhi"

+0

vielen Dank..meine Fehler. – Balaji

2

Wieder-Code ändern, ist,

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

Ich denke, Sie können ein benutzerdefiniertes Modell zum Speichern von Informationen über nur Benutzer von Delhi erstellen.

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

Jetzt können Sie Ihren Code hinzufügen und für Delhi Benutzer überprüfen:

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

zurückholen: customList.get(position).getAbc();