D.Blazer D.Blazer - 7 months ago 7
Java Question

Trying to find all objects out of and arraylist wit a field with the same value and display this in a list

Oke i have a arraylist with client objects, what i want to do now is search through the arraylist on one of the client fields, and get all the clients that share this value. to afterwords display these clients in a list.

Client:

public class Client implements Serializable{
private String name;
private String phone;
private String email;
private String url;
private Double turnover;
private String visitAddress;
private String visitCity;
private String visitZipcode;
private String visitCountry;
private String postalAddress;
private String postalCity;
private String postalZipcode;
private String postalCountry;
private List<Contact> contactList = new ArrayList<Contact>();


public Client(String Name, String Phone, String Email, String URL, Double Turnover,
String VisitAddress, String VisitCity, String VisitZipcode, String VisitCountry,
String PostalAddress, String PostalCity, String PostalZipcode, String PostalCountry, List contactList){
super();
this.name = Name;
this.phone = Phone;
this.email = Email;
this.url = URL;
this.turnover = Turnover;

this.visitAddress = VisitAddress;
this.visitCity = VisitCity;
this.visitZipcode = VisitZipcode;
this.visitCountry = VisitCountry;
this.postalAddress = PostalAddress;
this.postalCity = PostalCity;
this.postalZipcode = PostalZipcode;
this.postalCountry = PostalCountry;
this.contactList = contactList;
}


public String getName() {
return name;
}

public String getPhone() {
return phone;
}

public String getEmail() {
return email;
}

public String getUrl() {
return url;
}

public Double getTurnover() {

return turnover;
}

public String getVisitAddress() {
return visitAddress;
}

public String getVisitCity() {
return visitCity;
}

public String getVisitZipcode() {
return visitZipcode;
}

public String getVisitCountry() {
return visitCountry;
}

public String getPostalAddress() {
return postalAddress;
}

public String getPostalCity() {
return postalCity;
}

public String getPostalZipcode() {
return postalZipcode;
}

public String getPostalCountry() {
return postalCountry;
}

public List<Contact> getContactlist(){
return contactList;
}

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

public void setPhone(String phone) {
this.phone = phone;
}

public void setEmail(String email) {
this.email = email;
}

public void setUrl(String url) {
this.url = url;
}

public void setTurnover(Double turnover) {
this.turnover = turnover;
}

public void setVisitAddress(String visitAddress) {
this.visitAddress = visitAddress;
}

public void setVisitCity(String visitCity) {
this.visitCity = visitCity;
}

public void setVisitZipcode(String visitZipcode) {
this.visitZipcode = visitZipcode;
}

public void setVisitCountry(String visitCountry) {
this.visitCountry = visitCountry;
}

public void setPostalAddress(String postalAddress) {
this.postalAddress = postalAddress;
}

public void setPostalCity(String postalCity) {
this.postalCity = postalCity;
}

public void setPostalZipcode(String postalZipcode) {
this.postalZipcode = postalZipcode;
}

public void setPostalCountry(String postalCountry) {
this.postalCountry = postalCountry;
}

public void setContactList(List<Contact> contactList) {
this.contactList = contactList;
}

}


right now i search like this:

public class SearchClientFragment extends Fragment implements OnClickListener {

private ClientsArray clientsArray = new ClientsArray();
private Client currentClient;

private EditText searchName;

private EditText searchVisitAddress;
private EditText searchVisitCity;
private EditText searchVisitZipcode;
private EditText searchVisitCountry;

private EditText searchPostalAddress;
private EditText searchPostalCity;
private EditText searchPostalZipcode;
private EditText searchPostalCountry;

private EditText searchContactFirstname;
private EditText searchContactSurname;

public SearchClientFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_search_client,
container, false);

Button searchClientButton = (Button) rootView.findViewById(R.id.btn_submit_search_client);
searchClientButton.setOnClickListener(this);

Button clearSearchFields = (Button) rootView.findViewById(R.id.btn_clear_search_client);
clearSearchFields.setOnClickListener(this);

searchName = (EditText) rootView.findViewById(R.id.search_company_name);

searchVisitAddress = (EditText) rootView.findViewById(R.id.search_visit_address);
searchVisitCity = (EditText) rootView.findViewById(R.id.search_visit_city);
searchVisitZipcode = (EditText) rootView.findViewById(R.id.search_visit_zipcode);
searchVisitCountry = (EditText) rootView.findViewById(R.id.search_visit_country);

searchPostalAddress = (EditText) rootView.findViewById(R.id.search_postal_address);
searchPostalCity = (EditText) rootView.findViewById(R.id.search_postal_city);
searchPostalZipcode = (EditText) rootView.findViewById(R.id.search_postal_zipcode);
searchPostalCountry = (EditText) rootView.findViewById(R.id.search_postal_country);

searchContactFirstname = (EditText) rootView.findViewById(R.id.search_contact_fname);
searchContactSurname = (EditText) rootView.findViewById(R.id.search_contact_sname);


return rootView;
}

@Override
public void onClick(View v) {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
switch (v.getId()){
case R.id.btn_clear_search_client:
searchName.setText("");

searchVisitAddress.setText("");
searchVisitCity.setText("");
searchVisitZipcode.setText("");
searchVisitCountry.setText("");

searchPostalAddress.setText("");
searchPostalCity.setText("");
searchPostalZipcode.setText("");
searchPostalCountry.setText("");

searchContactFirstname.setText("");
searchContactSurname.setText("");
break;

case R.id.btn_submit_search_client:
for(Client client : clientsArray.ClientsArray){
client.getContactlist();
if(client.getName().trim().equals(searchName.getText().toString().trim())) {
currentClient = client;
}else if (client.getVisitAddress().trim().equals(searchVisitAddress.getText().toString().trim())){
currentClient = client;
}else if (client.getVisitCity().trim().equals(searchVisitCity.getText().toString().trim())){
currentClient = client;
}else if (client.getVisitZipcode().trim().equals(searchVisitZipcode.getText().toString().trim())){
currentClient = client;
}else if (client.getVisitCountry().trim().equals(searchVisitCountry.getText().toString().trim())){
currentClient = client;
} else if (client.getPostalAddress().trim().equals(searchPostalAddress.getText().toString().trim())){
currentClient = client;
}else if (client.getPostalCity().trim().equals(searchPostalCity.getText().toString().trim())){
currentClient = client;
}else if (client.getPostalZipcode().trim().equals(searchPostalZipcode.getText().toString().trim())){
currentClient = client;
}else if (client.getPostalCountry().trim().equals(searchPostalCountry.getText().toString().trim())){
currentClient = client;
}for(Contact contact : client.getContactlist()){
if(contact.getFirstname().trim().equals(searchContactFirstname.getText().toString().trim())){
currentClient = client;
}else if(contact.getSurname().trim().equals(searchContactSurname.getText().toString().trim())){
currentClient = client;
}
}
if(currentClient != null){
ViewClientFragment viewClientFragment = new ViewClientFragment();
viewClientFragment.setCurrentClient(currentClient);
transaction.replace(R.id.group, viewClientFragment, "viewcfrag");
transaction.addToBackStack(null);
transaction.commit();
}else{
Toast.makeText(this.getActivity(), "Client Not Found", Toast.LENGTH_LONG).show();
}

}
break;
}

}
}


this works great for when there is only 1 client with that field value, and brings me to a view fragment for that client. but i now want to get all the clients if i fill in a value that multiple have in the same field. for example when i search on visitaddress Groningen, i want to get all clients that have groningen as visitation address.

do i have to add these to another array to display these in a listview?
or can i do it in another way

My solution: edited search fragment

public class SearchClientFragment extends Fragment implements OnClickListener {

private Client currentClient;

private EditText searchName;

private EditText searchVisitAddress;
private EditText searchVisitCity;
private EditText searchVisitZipcode;
private EditText searchVisitCountry;

private EditText searchPostalAddress;
private EditText searchPostalCity;
private EditText searchPostalZipcode;
private EditText searchPostalCountry;

private EditText searchContactFirstname;
private EditText searchContactSurname;

public SearchClientFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_search_client,
container, false);

Button searchClientButton = (Button) rootView.findViewById(R.id.btn_submit_search_client);
searchClientButton.setOnClickListener(this);

Button clearSearchFields = (Button) rootView.findViewById(R.id.btn_clear_search_client);
clearSearchFields.setOnClickListener(this);

searchName = (EditText) rootView.findViewById(R.id.search_company_name);

searchVisitAddress = (EditText) rootView.findViewById(R.id.search_visit_address);
searchVisitCity = (EditText) rootView.findViewById(R.id.search_visit_city);
searchVisitZipcode = (EditText) rootView.findViewById(R.id.search_visit_zipcode);
searchVisitCountry = (EditText) rootView.findViewById(R.id.search_visit_country);

searchPostalAddress = (EditText) rootView.findViewById(R.id.search_postal_address);
searchPostalCity = (EditText) rootView.findViewById(R.id.search_postal_city);
searchPostalZipcode = (EditText) rootView.findViewById(R.id.search_postal_zipcode);
searchPostalCountry = (EditText) rootView.findViewById(R.id.search_postal_country);

searchContactFirstname = (EditText) rootView.findViewById(R.id.search_contact_fname);
searchContactSurname = (EditText) rootView.findViewById(R.id.search_contact_sname);

ClientsSearchArray.ClientsSearchArray.clear();

return rootView;
}

@Override
public void onClick(View v) {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
switch (v.getId()){
case R.id.btn_clear_search_client:
searchName.setText("");

searchVisitAddress.setText("");
searchVisitCity.setText("");
searchVisitZipcode.setText("");
searchVisitCountry.setText("");

searchPostalAddress.setText("");
searchPostalCity.setText("");
searchPostalZipcode.setText("");
searchPostalCountry.setText("");

searchContactFirstname.setText("");
searchContactSurname.setText("");
break;

case R.id.btn_submit_search_client:
for(Client client : ClientsArray.ClientsArray){
client.getContactlist();
if(client.getName().trim().equals(searchName.getText().toString().trim())) {
currentClient = client;
ClientsSearchArray.ClientsSearchArray.add(currentClient);
}else if (client.getVisitAddress().trim().equals(searchVisitAddress.getText().toString().trim())){
currentClient = client;
ClientsSearchArray.ClientsSearchArray.add(currentClient);
}else if (client.getVisitCity().trim().equals(searchVisitCity.getText().toString().trim())){
currentClient = client;
ClientsSearchArray.ClientsSearchArray.add(currentClient);
}else if (client.getVisitZipcode().trim().equals(searchVisitZipcode.getText().toString().trim())){
currentClient = client;
ClientsSearchArray.ClientsSearchArray.add(currentClient);
}else if (client.getVisitCountry().trim().equals(searchVisitCountry.getText().toString().trim())){
currentClient = client;
ClientsSearchArray.ClientsSearchArray.add(currentClient);
} else if (client.getPostalAddress().trim().equals(searchPostalAddress.getText().toString().trim())){
currentClient = client;
ClientsSearchArray.ClientsSearchArray.add(currentClient);
}else if (client.getPostalCity().trim().equals(searchPostalCity.getText().toString().trim())){
currentClient = client;
ClientsSearchArray.ClientsSearchArray.add(currentClient);
}else if (client.getPostalZipcode().trim().equals(searchPostalZipcode.getText().toString().trim())){
currentClient = client;
ClientsSearchArray.ClientsSearchArray.add(currentClient);
}else if (client.getPostalCountry().trim().equals(searchPostalCountry.getText().toString().trim())){
currentClient = client;
ClientsSearchArray.ClientsSearchArray.add(currentClient);
}for(Contact contact : client.getContactlist()){
if(contact.getFirstname().trim().equals(searchContactFirstname.getText().toString().trim())){
currentClient = client;
ClientsSearchArray.ClientsSearchArray.add(currentClient);
}else if(contact.getSurname().trim().equals(searchContactSurname.getText().toString().trim())){
currentClient = client;
ClientsSearchArray.ClientsSearchArray.add(currentClient);
}
}
}
break;
}
if(ClientsSearchArray.ClientsSearchArray.size() > 1){
SearchedClientsListFragment searchedClientsListFragment = new SearchedClientsListFragment();
transaction.replace(R.id.group, searchedClientsListFragment, "searchedcfrag");
transaction.addToBackStack(null);
transaction.commit();
}else if (currentClient != null){
ViewClientFragment viewClientFragment = new ViewClientFragment();
viewClientFragment.setCurrentClient(currentClient);
transaction.replace(R.id.group, viewClientFragment, "viewcfrag");
transaction.addToBackStack(null);
transaction.commit();
}else{
Toast.makeText(this.getActivity(), "Client Not Found", Toast.LENGTH_LONG).show();
}

}
}

Answer

fixed my own problem a while ago, forgot to paste it here.

case R.id.btn_submit_search_client:

            ClientsSearchArray.ClientsSearchArray.clear();
            Client currentClient = null;

            if (searchName.getText().toString().trim().equals("")
                    && searchVisitAddress.getText().toString().trim().equals("")
                    && searchVisitCity.getText().toString().trim().equals("")
                    && searchVisitZipcode.getText().toString().trim().equals("")
                    && searchVisitCountry.getText().toString().trim().equals("")
                    && searchPostalAddress.getText().toString().trim().equals("")
                    && searchPostalCity.getText().toString().trim().equals("")
                    && searchPostalZipcode.getText().toString().trim().equals("")
                    && searchPostalCountry.getText().toString().trim().equals("")
                    && searchContactSurname.getText().toString().trim().equals("")
                    && searchContactFirstname.getText().toString().trim().equals("")
                    ) {
                Toast.makeText(this.getActivity(), "Please give search paramaters", Toast.LENGTH_LONG).show();
            } else {
                for (Client client : ClientsArray.ClientsArray) {
                    if (client.getName().trim().contains(searchName.getText().toString().trim())
                            && client.getVisitAddress().trim().contains(searchVisitAddress.getText().toString().trim())
                            && client.getVisitCity().trim().contains(searchVisitCity.getText().toString().trim())
                            && client.getVisitZipcode().trim().contains(searchVisitZipcode.getText().toString().trim())
                            && client.getVisitCountry().trim().contains(searchVisitCountry.getText().toString().trim())
                            && client.getPostalAddress().trim().contains(searchPostalAddress.getText().toString().trim())
                            && client.getPostalCity().trim().contains(searchPostalCity.getText().toString().trim())
                            && client.getPostalZipcode().trim().contains(searchPostalZipcode.getText().toString().trim())
                            && client.getPostalCountry().trim().contains(searchPostalCountry.getText().toString().trim())) {

                        if (!searchContactSurname.getText().toString().trim().equals("")
                                || !searchContactFirstname.getText().toString().trim().equals("")) {
                            for (Contact contact : client.getContactList()) {
                                if (contact.getFirstname().trim().contains(searchContactFirstname.getText().toString().trim())
                                        && contact.getSurname().trim().contains(searchContactSurname.getText().toString().trim())) {

                                    if(client.getContactList().size() != 0){
                                        currentClient = client;
                                        if (!ClientsSearchArray.ClientsSearchArray.contains(currentClient)) {
                                            ClientsSearchArray.ClientsSearchArray.add(currentClient);
                                        }
                                    }
                                }
                            }
                        }else{
                            currentClient = client;
                            if (!ClientsSearchArray.ClientsSearchArray.contains(currentClient)) {
                                ClientsSearchArray.ClientsSearchArray.add(currentClient);
                            }
                        }
                    }
                }

                if (ClientsSearchArray.ClientsSearchArray.size() > 1) {
                    SearchedClientsListFragment searchedClientsListFragment = new SearchedClientsListFragment();
                    transaction.replace(R.id.group, searchedClientsListFragment, "searchedcfrag");
                    transaction.addToBackStack(null);
                    transaction.commit();
                } else if (currentClient != null) {
                    ViewClientFragment viewClientFragment = new ViewClientFragment();
                    viewClientFragment.setCurrentClient(currentClient);
                    transaction.replace(R.id.group, viewClientFragment, "viewcfrag");
                    transaction.addToBackStack(null);
                    transaction.commit();
                } else {
                    Toast.makeText(this.getActivity(), "Client Not Found", Toast.LENGTH_LONG).show();
                }


            }
            break;
    }

}