1

Hi I have developed bus booking app in Android. Here there is a fucntionality called bus filteration. We can filter the bus based on user selection. So in bus list it contains AC and NON Ac. How do i filter based on AC/ NON AC.

for (int j = 0; j < busListResponses.size(); j++) 
{
    if (busListResponses.get(j).getCOACHDESC().toLowerCase().contains((strvalue.toString().toLowerCase()))) {
        {
            filterResponse.add(busListResponses.get(j));
        }
    }
}

So if i select NON AC it retrieves correct result. But if i select AC, it returns all bus contains both AC and NON Ac

Rahul Khurana
  • 8,577
  • 7
  • 33
  • 60
user2210356
  • 89
  • 2
  • 2
  • 9
  • You can again use `for` loop and use the `if` condition to check whether it matches user selection. and append it to the new list – Rahul Khurana Aug 13 '19 at 06:31
  • 2
    Because you are using a contains and if u select AC then AC also present in non AC string so that's why it is showing both types – Harsh kurra Aug 13 '19 at 06:33
  • @Harshkurra is right. When you are always using a filter make sure to use `contentEquals` instead of `contains` for matching values perfectly.. – sanjeev Aug 13 '19 at 06:36
  • busListResponses.get(j).getCOACHDESC(). this will return AC Semi Sleeper 2+2. So if i give equals it will not retrieve any result. – user2210356 Aug 13 '19 at 06:53

2 Answers2

1
for (int j = 0; j < busListResponses.size(); j++) 
{
    if (busListResponses.get(j).getCOACHDESC().toLowerCase().equals((strvalue.toString().toLowerCase()))) {
        {
            filterResponse.add(busListResponses.get(j));
        }
    }
}

or without converting into lowercase

for (int j = 0; j < busListResponses.size(); j++) 
{
    if (busListResponses.get(j).getCOACHDESC().equalsIgnoreCase((strvalue.toString()))) {
        {
            filterResponse.add(busListResponses.get(j));
        }
    }
}
Harsh kurra
  • 1,094
  • 1
  • 9
  • 19
  • But the response from operator is like AC 2+2 semi sleeper. So if i give equals it will not fetch any result. – user2210356 Aug 13 '19 at 06:51
  • Then check for contains 'NON' string instead of AC or you can also use regular expression for pattern-based string comparison – Harsh kurra Aug 13 '19 at 08:05
0

In your Bus model class add a boolean method isAirConditioned(), so when your looping through the list simply do it like this:

public class Bus{

...

  public boolean isAirConditioned(){
  String coachDesc = getCoachDesc();
  if(coachDesc.contains("NON AC")
    return false;
  return true;
  }
}

And modify your for loop as

for (int j = 0; j < busListResponses.size(); j++) {
     if (busListResponses.get(j).isAirConditioned()) {
         filterResponse.add(busListResponses.get(j));
         }
      }

Edit: Add this condition before your for loop

String strvalue; 
if((!strvalue.contains("AC") || strvalue.contains("NON AC"))|| (strvalue.contains("AC") && !strvalue.contains("NON AC" )))
for (int j = 0; j < busListResponses.size(); j++) {
     if (busListResponses.get(j).getCOACHDESC().contains(strvalue) {
         filterResponse.add(busListResponses.get(j));
         }
      }

If you encounter same problem with sleeper and semi sleeper, use similar logic.

Edit2: this should be your conditional statment before the for loop to include all the parameters

if(((strvalue.contains("Semi Sleeper") || strvalue.contains("NON AC")||
 strvalue.contains("Seater")) || (strvalue.contains("AC") && 
!strvalue.contains("NON AC" )) || (strvalue.contains("Sleeper") && 
!strvalue.contains("Semi Sleeper")))
Shubham
  • 175
  • 3
  • 12
  • But it will contain other fields also like seater, sleeper and semi sleeper – user2210356 Aug 13 '19 at 06:59
  • Is your filter array {ac, non ac} or {ac, non ac, seater, sleeper, semi sleeper} ? Please be specific. If it is former, this will work. For more general solutions check out this thread https://stackoverflow.com/questions/122105/what-is-the-best-way-to-filter-a-java-collection – Shubham Aug 13 '19 at 07:25
  • @user2210356 I've edited my answer, please see if it solves your problem. – Shubham Aug 13 '19 at 07:52
  • my filter contains value like ac, non ac , seater, sleeper and semi sleeper. But i am not sure how to retrieve only particular record based on selection. – user2210356 Aug 13 '19 at 11:06
  • Please give me some example to proceed to further – user2210356 Aug 13 '19 at 11:16
  • Did you read my edit? The problem is in the logic that .contains("AC") will also give those strings containing "NON AC" because NON AC contains AC too. Similarly with sleeper and semi sleeper. So you retrieve them all separately by putting a conditional check before your for loop. If strvalue is semi sleeper, non ac or seater your method will work fine. When you retrieve sleeper or ac you'd have to minus those cases which give semi sleeper or non ac because you have already filtered them earlier. I will make one more edit to show the final conditional statement. – Shubham Aug 13 '19 at 11:30
  • I understood the problem but i dont know how to retrieve the string based on selection. And also if((!strvalue.contains("AC") || strvalue.contains("NON AC"))|| (strvalue.contains("AC") && !strvalue.contains("NON AC" ))) is not working. Can you explain this loop – user2210356 Aug 13 '19 at 11:36
  • I've added another edit, let me know if you face the same problem. – Shubham Aug 13 '19 at 11:39
  • is this the condition is correct? Because it is retrieve Non Ac bus also when i select AC – user2210356 Aug 13 '19 at 11:46