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

时间:2018-02-28 05:33:20

标签: android listview filter searchview

开始(我稍后会解释这个问题)这是我的代码:

在我的MainActivity中,我有一个listview,这就是我能够搜索它的方式。

MainActivity:

        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 BaseAdapter  implements Filterable {
        private InstagramApp mApp;
        private ListView lvRelationShipAllUser;
        public ArrayList<HashMap<String, String>> usersInfo;
        private ArrayList<HashMap<String, String>> usersInfo1;
        private List<String> originalData = 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来搜索&#34;搜索&#34;我的列表视图,正如我从

中看到的那样
  

Log.d(&#34; OkHttpNList&#34;,String.valueOf(usersInfo));

用户信息正在被更改,一切都应按计划运行,但列表中没有任何变化。

+++++++++++++++++

此外,正如您可以从

中看到的那样
  

Log.d(&#34; usersInfo&#34;,&#34; ImHere&#34;):在getView中

视图getView仅在我点击搜索视图时更新,而不是在我编辑时更新,因此上面的信息无用。

+++++++++++++++++

我想我的问题是为什么我的文字发生变化时我的getView没有更新?

编辑: 如果您需要更多信息,请通知我:)

提前谢谢。 (问题仍然没有解决!)

编辑: 我认为问题是notifyDataSetChanged(); 没有重置getView!

1 个答案:

答案 0 :(得分:1)

这是因为您的适配器的主列表属于HashMap<String, String>>类型,但在您的performFilter中,您要将值添加到类型ArrayList<String>的列表中。

所以为了避免冲突在你的performFilter中使用它: -

ArrayList<HashMap<String, String>> nlist = new ArrayList<>;然后将值添加到此nList。

更新过滤器中的代码如下: -

ArrayList<HashMap<String, String>> nlist = new ArrayList<>;
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(list.get(i));
                }
            }