我有一个国家列表,并且正在使用它进行搜索,因此,如果要搜索一个特定的国家,一旦找到了想要的国家,我就会从过滤列表中选择它。问题是,当我在过滤列表中选择第112个项目时,如果它在过滤列表的顶部,则结果实际上是未过滤列表中的第一个项目。
这将是一种更简单的解释方式,我已将视频上传到该视频,可在此处找到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);
}
谢谢
最佳答案
一直一次:
如果您使用的是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);
当然,这取决于您的适配器实现...因此,请记住,适配器应使用
getItem(position)
返回正确的对象如文档中所述,它适用于:
ListView
,GridView
,Spinner
,Gallery
和AdapterView
的其他子类因此,在您的情况下,正确的方法显然是:
Countries country = (Countries)parent.getItemAtPosition(position);