我有一个国家/地区列表,我正在使用它作为搜索,所以如果我要搜索特定国家/地区,一旦找到我想要的国家/地区,我就会从过滤后的列表中选择它。问题是,当我在筛选列表中选择第112个项目时,如果它位于筛选列表的顶部,则结果实际上是未筛选列表中的第1个项目。
这是一种更简单的解释方式,我已经上传了一个视频,可以在Search Example
找到我的搜索适配器在这里:
public Context mContext;
public ArrayList<Countries> countryArrayList;
public ArrayList<Countries> original;
public CountryAdapter(Context context, ArrayList<Countries> countryArrayList) {
mContext = context;
this.countryArrayList = countryArrayList;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
@Override
public int getCount() {
return countryArrayList.size();
}
@Override
public Object getItem(int position) {
return countryArrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
CountryHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.search_results_row_list, parent, false);
holder = new CountryHolder();
holder.countryName = (TextView) convertView.findViewById(R.id.country_text);
holder.countryFlag = (ImageView) convertView.findViewById(R.id.country_flag);
convertView.setTag(holder);
} else {
holder = (CountryHolder) convertView.getTag();
}
holder.countryName.setText(countryArrayList.get(position).getCountryName());
// holder.countryFlag.setImageResource(countryArrayList.get(position).getImgId());
return convertView;
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults oReturn = new FilterResults();
final ArrayList<Countries> results = new ArrayList<>();
if (original == null)
original = countryArrayList;
if (constraint != null) {
if (original != null && original.size() > 0) {
for (final Countries g : original) {
if (g.getCountryName()
.contains(constraint.toString()))
results.add(g);
}
}
oReturn.values = results;
}
return oReturn;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
countryArrayList = (ArrayList<Countries>) results.values;
notifyDataSetChanged();
}
};
}
public class CountryHolder {
TextView countryName;
ImageView countryFlag;
}
抱歉,我忘记了实际选择国家的地方
private void setSearchResultsList() {
final CountryAdapter countryAdapter = new CountryAdapter(getActivity(), controller.getCountriesArrayList());
countryAdapter.getFilter().filter(searchPhrase);
countryAdapter.notifyDataSetChanged();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedCountry = controller.getCountriesArrayList().get(+position).getCountryName();
mISearch.searchResult(selectedCountry);
}
});
listView.setAdapter(countryAdapter);
}
由于
答案 0 :(得分:2)
一直以来:
如果您使用AdapterView.setOnItemClickListener
,获得点击项目的正确方法就是这样的实施:
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)
从Adapter.getItem(position)
中使用的适配器返回AdapterView
(setAdapter
)
所以例如:
如果您使用的是ArrayAdapter<T>
,则应将parent.getItemAtPosition(position)
投放到T
并使用它......
ArrayAdapter<POJO>
使用:
POJO item = (POJO)parent.getItemAtPosition(position);
如果您使用CursorAdapter
Cursor c = (Cursor)parent.getItemAtPosition(position);
如果您使用SimpleAdapter
Map<String, ?> item = ( Map<String, ?>)parent.getItemAtPosition(position);
当然这取决于您的Adapter实现...所以您应该记住,Adapter应该使用getItem(position)
返回正确的对象
正如文档中所述,它适用于:ListView
,GridView
,Spinner
,Gallery
以及AdapterView
的其他子类
所以在你的情况下,正确的方法显然是:
Countries country = (Countries)parent.getItemAtPosition(position);
答案 1 :(得分:0)
这样做
在adapter
类中添加一个方法以获取当前列表
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) {
}
}
});