Sachin Solanki
Sachin Solanki

Reputation: 79

How to filter a listview?

I have a list view and i want to show records by filter suppose i have an array list called expenseList. in expenseList i am able to filter single item like this

 if (!dealerexpautoa.getText().toString().equals("")){
                        ArrayList<ExpenseStatusModel.Expense> expensetypeList= new ArrayList<>();

                        for (int i = 0; i < expenseList.size(); i++)
                        {
                            if (expenseList.get(i).getDealerName().equalsIgnoreCase(dealerexpautoa.getText().toString())) {

                                expensetypeList.add(expenseList.get(i));
                            }
                        }
                        adapterExpenseView.filterList(expensetypeList);

                    }

here getting the perfect output but when i need to perform same task with two string then it is not getting me the right out put

i have an EditText and Spinner i want to compare both and print in list view

else if(!spn.getSelectedItem().toString().equals("Select Type")
                            && !dealerexpautoa.getText().toString().equals("")){
                        ArrayList<ExpenseStatusModel.Expense> expensetypeList= new ArrayList<>();

                        for (int i = 0; i < expenseList.size(); i++)
                        {
                            if (expenseList.get(i).getDealerName().equalsIgnoreCase(dealerexpautoa.getText().toString()) &&
                                    expenseList.get(i).getExpenseType().equalsIgnoreCase(spn.getSelectedItem().toString())) {

                                expensetypeList.add(expenseList.get(i));
                            }
                        }
                        adapterExpenseView.filterList(expensetypeList);

                    }

Upvotes: 0

Views: 78

Answers (2)

Kevin Kurien
Kevin Kurien

Reputation: 842

you forgot to write if in your else statement

else if(!spn.getSelectedItem().toString().equals("Select Type")
                            && !dealerexpautoa.getText().toString().equals("")){
                        ArrayList<ExpenseStatusModel.Expense> expensetypeList= new ArrayList<>();

                        for (int i = 0; i < expenseList.size(); i++)
                        {
                            if (expenseList.get(i).getDealerName().equalsIgnoreCase(dealerexpautoa.getText().toString()) &&
                                    expenseList.get(i).getExpenseType().equalsIgnoreCase(spn.getSelectedItem().toString())) {

                                expensetypeList.add(expenseList.get(i));
                            }
                        }
                        adapterExpenseView.filterList(expensetypeList);

                    }

Upvotes: 1

navylover
navylover

Reputation: 13549

The condition expression in if branch is:

!dealerexpautoa.getText().toString().equals("")  

and the condition expression in else branch is

!spn.getSelectedItem().toString().equals("Select Type")
                            && !dealerexpautoa.getText().toString().equals("")

this two expressions are not mutually exclusive, only dealerexpautoa text is equal empty string, it could enter into else branch, but in else branch,the condition needs dealerexpautoa text not equal empty string, so this is contradictory.

Upvotes: 1

Related Questions