[英]Filtering on a list
I have a list of Countries and I am using it as a search, so if I am searching for a specific country, once its found the country I want, I selected it from the filtered list. 我有一个国家列表,并且正在使用它进行搜索,因此,如果要搜索一个特定的国家,一旦找到了想要的国家,我就会从过滤列表中选择它。 The problem is, when I select the 112th item on the filtered list, if it is at the top of the filtered list, the result is actually the 1st item on the unfiltered list.
问题是,当我在过滤列表中选择第112个项目时,如果它在过滤列表的顶部,则结果实际上是未过滤列表中的第一个项目。
This will be an easier way of explaining it, I've uploaded a video to it which can be found here Search Example 这将是一种更轻松的解释方式,我已将视频上传到该视频,可以在此处找到搜索示例
my Adapter for the Search is here: 我的搜索适配器在这里:
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;
}
Sorry I forgot where I am actually selecting the country 对不起,我忘了我实际上在选择国家
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);
}
Thanks 谢谢
Once for all the time: If you are using AdapterView.setOnItemClickListener
, the right way to get clicked item is such implementation: 一直这样:如果您使用
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)
returns Adapter.getItem(position)
from your adapter used in AdapterView
(with setAdapter
) parent.getItemAtPosition(position)
从AdapterView
使用的适配器(带有setAdapter
)返回Adapter.getItem(position)
。
so for example: 因此,例如:
if you are using ArrayAdapter<T>
you should cast parent.getItemAtPosition(position)
to T
and use it ... 如果使用
ArrayAdapter<T>
,则应将parent.getItemAtPosition(position)
为T
并使用它。
for ArrayAdapter<POJO>
use: 对于
ArrayAdapter<POJO>
使用:
POJO item = (POJO)parent.getItemAtPosition(position);
if you are using CursorAdapter
如果您使用的是
CursorAdapter
Cursor c = (Cursor)parent.getItemAtPosition(position);
if you are using SimpleAdapter
如果您使用的是
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)
当然,这取决于您的Adapter实现...因此,您应该记住Adapter应该使用
getItem(position)
返回正确的对象
As it is stated in the documentation it apply to: ListView
, GridView
, Spinner
, Gallery
and other subclasses of AdapterView
如文档中所述,它适用于:
ListView
, GridView
, Spinner
, Gallery
和AdapterView
其他子类
So in your case the right way is obviously: 因此,对于您而言,正确的方法显然是:
Countries country = (Countries)parent.getItemAtPosition(position);
do this way 这样做
add one method in adapter
class to get current list 在
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) {
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.