Implement Searchview On String Type Listview Android
I have a list of items and i want to start different activity on the basis of item, when i click it opens the correct activity but when i try to search list items from search view
Solution 1:
you are starting your activity on the basis of position
but the position
will be changed when you do the search because list will shrink and positions
will change so to get data associated with the specified position in the list use getItemAtPosition
so changes conditions on the basis of data
if (parent.getItemAtPosition(position).equals("item1")) {
Intent myIntent = new Intent(view.getContext(), activity1.class);
startActivityForResult(myIntent,0);
}
elseif (parent.getItemAtPosition(position).equals("item2")) { // use any item value here you want
Intent myIntent = new Intent(view.getContext(), aactivity4.class);
startActivityForResult(myIntent,0);
}
Note : you can use switch
as well instead of long if
or else-if
ladder
e.g You have three string
item 1position0
item 2position1
item 3position2
after searching item 2 you have two values in your list close to your search
item 2position0
item 3position1
so position will change so don't use it instead use the data
Code
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
publicvoidonItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = null;
// global string to class
selectedValue = String.valueOf(parent.getItemAtPosition(position));
if (selectedValue.equals("item1")) {
// ^^^ use any item value here you want
Intent myIntent = new Intent(view.getContext(), activity1.class);
startActivityForResult(myIntent,0);
}
elseif (selectedValue.equals("item2")) {
Intent myIntent = new Intent(view.getContext(), aactivity4.class);
startActivityForResult(myIntent,0);
}
}
});
Post a Comment for "Implement Searchview On String Type Listview Android"