Filtering On A List
Solution 1:
Once for all the time:
If you are using AdapterView.setOnItemClickListener
, the right way to get clicked item is such implementation:
adapterView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object item = parent.getItemAtPosition(position);
}
});
parent.getItemAtPosition(position)
returns Adapter.getItem(position)
from your adapter used in AdapterView
(with setAdapter
)
so for example:
if you are using
ArrayAdapter<T>
you should castparent.getItemAtPosition(position)
toT
and use it ...for
ArrayAdapter<POJO>
use:POJO item = (POJO)parent.getItemAtPosition(position);
if you are using
CursorAdapter
Cursor c = (Cursor)parent.getItemAtPosition(position);
if you are using
SimpleAdapter
Map<String, ?> item = ( Map<String, ?>)parent.getItemAtPosition(position);
of course it depends on your Adapter implementation ... so you should remeber that Adapter should return the right object with getItem(position)
As it is stated in the documentation it apply to: ListView
, GridView
, Spinner
, Gallery
and other subclasses of AdapterView
So in your case the right way is obviously:
Countries country = (Countries)parent.getItemAtPosition(position);
Solution 2:
do this way
add one method in adapter
class to get current list
public ArrayList<Countries> GetCurrentListData() {
return countryArrayList;
}
CountryAdapter dcAdapter = new CountryAdapter("whatever your perameter");
listview.setAdapter(dcAdapter);
lstDoctorsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
try {
Countries countries = dcAdapter
.GetCurrentListData().get(position);
// get data from countries model class
} catch (Exception e) {
}
}
});
Post a Comment for "Filtering On A List"