Reputation: 99
I have an ArrayList
in android which contains several names. Suppose user typed R, so I want to calculate how many names are there in the ArrayList
which starts with R. for example if arraylist contains Ashok, Bimal, Ram, Raju, sunita then it should return 2 as there are 2 names starts with R
Upvotes: 3
Views: 24502
Reputation: 414
Try below code....
enter code here
List<String> list = new ArrayList<String>();
list.add("How are you");
list.add("How you doing");
list.add("Joe");
list.add("Mike");
Collection<String> filtered = Collections2.filter(list,
Predicates.containsPattern("How"));
print(filtered);
Upvotes: -2
Reputation: 1525
In your onCreate method
int countToShow=counterMethod(character);
TextView showCount=(TextView)findViewById(idOfYourTextView);
showCount.setText(String.valueOf(count));
Make another method in your activity as follow:-
public int counterMethod(String character){
int count=0;
for(int i=0; i<arrayListName.size();i++)
{
String name=arrayListName.get[i];
if(name.startswith(character))
count++;
}
return count;
}
Upvotes: 0
Reputation: 4701
ArrayList<String> list = new ArrayList<String>();
list.add("Ashok");
list.add("Bimal");
list.add("Ram");
list.add("Raju");
list.add("sunita");
int count = 0;
for(String s: list){
if(s.startsWith("R")){
count++;
}
}
System.out.println(count);
Upvotes: 0
Reputation: 3633
int count = 0;
ArrayList<String> _list;//ur arraylist with names
for (String names : _list) {
if (names.startsWith("R")) {
count++;
}
}
System.out.println(count);
Upvotes: 7
Reputation: 2308
Iterate through the ArrayList and increment an integer variable when your check returns true.
Upvotes: 4