user9243462
user9243462

Reputation:

Issue when using .getFilter() on a custom adapter (Not Filtering Correctly) (Android)

To begin (I will explain the Issue later) this is my code:

In my MainActivity, I have a listview and this is how I am able to search it.

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);

This is all the code for my personal adapter:

 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();
            }

        }
    }

When I use my SearchView to "Search" my listview, as I can see from

Log.d("OkHttpNList", String.valueOf(usersInfo));

usersInfo is being changed and everything should be working according to plan but nothing changes in the list.

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

Additionally, as you can see from

Log.d("usersInfo", "ImHere"): in the getView

The view getView only updates when I click on the searchview and not when I edit it so the Info above is useless.

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

I guess my question is why is my getView not updating when my text is changed?

EDIT: Please inform me if you need any more informtion to help me :)

Thanks ahead of time. (PROBLEM IS STILL NO SOLVED!)

EDIT: I believe that the issue is the notifyDataSetChanged(); Is not reseting the getView!

Upvotes: 0

Views: 364

Answers (1)

Santanu Sur
Santanu Sur

Reputation: 11477

This is because the main list of your adapter is of type HashMap<String, String>> but in your performFilter you are adding the values to a list of type ArrayList<String>.

So to avoid the conflict use this in your performFilter:-

ArrayList<HashMap<String, String>> nlist = new ArrayList<>; and then add the values to this nList.

UPDATE Your code in the filter be like this :-

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));
                }
            }

Upvotes: 1

Related Questions