[英]Searchview in Android working but displaying incorrect result when clicked on.
所以我有一个带有searchview的自定义适配器。 每当我搜索特定的人时,我的搜索视图就会显示正确的结果。 但是,当我单击searchview中的项目时,它将返回原始列表的结果。
因此,例如,我有项目ab,cd,ef,并且我搜索ef。 searchview返回1项ef。 但是,如果单击它,我将获得ab的数据。
我该如何解决? 还是我需要重写searchview的onclick方法?
谢谢。 (我将发布我的代码(我将删除不相关的代码),我的自定义适配器中包含searchview代码。另一个是包含我的searchview和listview列表的片段。
My Custom Adapater.
public class oncallAdapter extends ArrayAdapter<OnCallContact> implements SectionIndexer
{
private ArrayList<OnCallContact> contactList;
private Context context;
//For our searchview
private MyFilter filter;
private ArrayList<OnCallContact> mcontactListFilter;
//OUr adapter constructor
//WE have an arraylist which represents the bulletin objects
public oncallAdapter(ArrayList<OnCallContact> ONCALLLIST, Context ctx)
{
super(ctx, R.layout.list_item_oncall, ONCALLLIST);
this.contactList = ONCALLLIST;
this.context= ctx;
this.mcontactListFilter = ONCALLLIST;
}
//This is what sets up the textviews or one item in the listview.
//We are overiding the orignial method
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
//if null then inflate the view which is the row for the bulletins
if(convertView == null)
{
LayoutInflater inflator = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.list_item_oncall,parent,false);
}
TextView mgroup = (TextView) convertView.findViewById(R.id.oncall_group);
TextView mname = (TextView) convertView.findViewById(R.id.oncall_name);
TextView mCircleText = (TextView) convertView.findViewById(R.id.circleText);
OnCallContact b = contactList.get(position);
//Setting the textviews.
mgroup.setText(b.getMgroup());
mname.setText(b.getMname());
mCircleText.setText(b.getMgroup().substring(0,1).toUpperCase());
return convertView;
}
@Override
public android.widget.Filter getFilter()
{
if(filter == null)
{
filter = new MyFilter();
}
return filter;
}
private class MyFilter extends android.widget.Filter
{
@Override
protected android.widget.Filter.FilterResults performFiltering(CharSequence constraint)
{
FilterResults results = new FilterResults();
if(constraint!= null && constraint.length()>0)
{
//Arraylist where we will add the items (oncallConcacts) that have the letter
ArrayList<OnCallContact> filterList = new ArrayList<>();
for(int i=0; i < mcontactListFilter.size();i++)
{
//CHecking if the letter is in the name
if(mcontactListFilter.get(i).getMname().toUpperCase().contains(constraint.toString().toUpperCase()))
{
OnCallContact contact = new OnCallContact();
contact.setMname(mcontactListFilter.get(i).getMname());
contact.setMgroup(mcontactListFilter.get(i).getMgroup());
contact.setMtitle(mcontactListFilter.get(i).getMtitle());
contact.setMbusinessPhone(mcontactListFilter.get(i).getMbusinessPhone());
contact.setMemail(mcontactListFilter.get(i).getMemail());
contact.setMpager(mcontactListFilter.get(i).getMpager());
contact.setmManagerName(mcontactListFilter.get(i).getmManagerName());
contact.setmManagerBusinessPhone(mcontactListFilter.get(i).getmManagerBusinessPhone());
contact.setmManagerPager(mcontactListFilter.get(i).getmManagerPager());
contact.setmManagerEmail(mcontactListFilter.get(i).getmManagerEmail());
filterList.add(contact);
}
}
results.count = filterList.size();
results.values = filterList;
Log.v("HEre is the filtersize " , "" +filterList.size());
}
else
{
results.count = mcontactListFilter.size();
results.values = mcontactListFilter;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
{
contactList = (ArrayList<OnCallContact>) results.values;
notifyDataSetChanged();
}
}
}
我的片段课
public class OnCallFragment extends android.support.v4.app.Fragment implements SearchView.OnQueryTextListener{
oncallAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_on_call, container, false);
//Parsing the xml file and getting the data we need which is an arraylist of oncallContacts
OnCallXMLParser b = new OnCallXMLParser();
final ArrayList<OnCallContact> list = b.parse(getContext());
//HEre the custom adapter is making the listviews
adapter = new oncallAdapter(list, getContext());
final ListView listView = (ListView) rootView.findViewById(R.id.onCallListView);
SearchView search = (SearchView)rootView.findViewById(R.id.onCall_searchView);
search.setOnQueryTextListener(this);
//displaying the listview by setting the adapter.
listView.setAdapter(adapter);
listView.setFastScrollEnabled(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//The contact we are passing through. (Item that was clicked on)
OnCallContact temp = list.get(position);
// Intent i = new Intent(getActivity(), DetailActivity.class).putExtra("extra", ForecastAdapter.getItem(position));
Intent i = new Intent(getActivity(), OnCallDetail.class).putExtra("ContactInformation", temp);
startActivity(i);
}
});
// Inflate the layout for this fragment
return rootView;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
}
谢谢!!
这很可能是因为结果ef在所有过滤结果中的位置以及结果ef在所有可用项目中的位置都不同。 您需要使用另一个变量,例如自定义标识符,而不是使用position。 有关更多信息,请参考此内容: 从列表视图中选择了错误的项目,并且列表视图 自定义过滤器在过滤时给出了错误的选择
对于遇到此问题的任何人,请确保您覆盖getcount,getitem。 GetItemId应该保持为返回0;否则,返回0。 其他2种方法必须覆盖它们。 希望对任何可能遇到此问题的人有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.