在自定义适配器上使用.getFilter()时出现问题(未正确过滤)(Android)

贵有恒,何必三更起、五更眠、最无益,只怕一日曝、十日寒。这篇文章主要讲述在自定义适配器上使用.getFilter()时出现问题(未正确过滤)(Android)相关的知识,希望能为你提供帮助。
首先(我将在稍后解释)这是我的代码:
在我的MainActivity中,我有一个listview,这就是我能够搜索它的方式。
主要内容:

adapter2 = new RelationShipAdapter(MainActivity.this, usersInfo, usersInfo1); url = getIntent().getStringExtra("userInfo"); url22 = getIntent().getStringExtra("userInfo22"); urlTwo = getIntent().getStringExtra("userInfoTwo"); sv=(SearchView) findViewById(R.id.search); Log.d("OkHttpNList", String.valueOf(usersInfo)); sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {@Override public boolean onQueryTextSubmit(String text) { // TODO Auto-generated method stub return false; }@Override public boolean onQueryTextChange(String text) { Log.d("OkHttpNList", "here.1"); adapter2.getFilter().filter(text); return false; } }); lvRelationShipAllUser = (ListView) findViewById(R.id.lvRelationShip); lvRelationShipAllUser.setAdapter(adapter2); setListViewHeightBasedOnChildren(lvRelationShipAllUser);

这是我的个人适配器的所有代码:
package com.tag.instagramdemo.example; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.util.Log; import android.widget.Filter; import android.widget.Filterable; import android.widget.ListView; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.tag.instagramdemo.R; import com.tag.instagramdemo.lazyload.ImageLoader; public class RelationShipAdapter extends BaseAdapterimplements Filterable { private InstagramApp mApp; private ListView lvRelationShipAllUser; public ArrayList< HashMap< String, String> > usersInfo; private ArrayList< HashMap< String, String> > usersInfo1; private List< String> originalData = https://www.songbingjia.com/android/null; private ArrayList< HashMap< String, String> > filteredData= null; private LayoutInflater mInflater; private ItemFilter mFilter = new ItemFilter(); private LayoutInflater inflater; private LayoutInflater inflater1; //lvRelationShipAllUser = (ListView) findViewById(R.id.lvRelationShip); private ImageLoader imageLoader; Context context; public RelationShipAdapter(Context context, ArrayList< HashMap< String, String> > usersInfo,ArrayList< HashMap< String, String> > usersInfo1) { inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater1 = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); Log.d("OkHttp","debug4"); this.usersInfo = usersInfo; this.usersInfo1 = usersInfo1; getFilter(); this.imageLoader = new ImageLoader(context); }@Override public View getView(int position, View convertView, ViewGroup parent) { View view = inflater.inflate(R.layout.relationship_inflater, null); View view1 = inflater1.inflate(R.layout.relationship_inflater_nothing, null); Log.d("usersInfo", "ImHere"); Holder holder = new Holder(); //holder.tvFullName1 = (TextView) view1.findViewById(R.id.tvFullName); holder.ivPhoto = (ImageView) view.findViewById(R.id.ivImage); holder.tvFullName = (TextView) view.findViewById(R.id.tvFullName); holder.tvFullName.setText(usersInfo.get(position).get( Relationship.TAG_USERNAME)); Log.d("tvFullNameTestIng",usersInfo.get(position).get( Relationship.TAG_USERNAME)); imageLoader.DisplayImage( usersInfo.get(position).get(Relationship.TAG_PROFILE_PICTURE), holder.ivPhoto); return view; }@Override public Filter getFilter() { Log.d("OkHttpNList", "here"); return mFilter; }private class Holder { private ImageView ivPhoto; private TextView tvFullName; private TextView tvFullName1; }@Override public int getCount() { // TODO Auto-generated method stub return usersInfo.size(); }@Override public Object getItem(int position) { // TODO Auto-generated method stub return position; }@Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } private class ItemFilter extends Filter { @Override protected FilterResults performFiltering(CharSequence constraint) { Log.d("OkHttpNList", "here1"); String filterString = constraint.toString().toLowerCase(); FilterResults results = new FilterResults(); ArrayList< HashMap< String, String> > list = usersInfo; int count = list.size(); final ArrayList< String> nlist = new ArrayList< > (count); String filterableString; Log.d("OkHttpNList", String.valueOf(list)); for (int i = 0; i < count; i++) { filterableString = list.get(i).get("username"); Log.d("OkHttpNList", String.valueOf(filterableString)); if (filterableString.toLowerCase().contains(filterString)) { nlist.add(String.valueOf(list.get(i))); } } Log.d("OkHttpNList", String.valueOf(nlist)); results.values = nlist; return results; }@SuppressWarnings("unchecked") @Override protected void publishResults(CharSequence constraint, FilterResults results) { usersInfo = (ArrayList< HashMap< String, String> > ) results.values; Log.d("OkHttpNList", String.valueOf(usersInfo)); notifyDataSetChanged(); }} }

当我使用我的SearchView“搜索”我的列表视图时,我可以看到
Log.d(“OkHttpNList”,String.valueOf(usersInfo));
usersInfo正在更改,所有内容都应按计划运行,但列表中没有任何更改。
+++++++++++++++++
此外,您可以从中看到
Log.d(“usersInfo”,“ImHere”):在getView中
视图getView仅在我单击searchview时更新,而不是在我编辑时更新,因此上面的Info无用。
+++++++++++++++++
我想我的问题是为什么我的文本更改时我的getView没有更新?
编辑:如果您需要更多信息,请通知我:)
提前谢谢。 (问题仍然没有解决!)
编辑:我认为问题是notifyDataSetChanged(); 是不是重置了getView!
答案这是因为适配器的主列表是HashMap< String, String> > 类型,但在performFilter中,您将值添加到类型为ArrayList< String> 的列表中。
所以为了避免冲突,请在你的performFilter中使用它: -
qazxsw poi然后将值添加到此List。
【在自定义适配器上使用.getFilter()时出现问题(未正确过滤)(Android)】更新过滤器中的代码如下: -
ArrayList< HashMap< String, String> > nlist = new ArrayList< > ;


    推荐阅读