0

I followed this code on SearchView In ListView having a custom Adapter which works fine.I tried to implement it on my project but seems to be not working. provided that data is populated from Firebase database server.

I have removed the code for firebase which is not cause of the error. I don't get any error but it is not working. Here is what I have tried :

ShowFriendsActivity.java

    public class ShowFriendsActivity extends AppCompatActivity implements SearchView.OnQueryTextListener{


    private ListView mMessageListView;
    private ShowFriendsAdapter showFriendsAdapter;
    SearchView searchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_online_users);


        searchView = (SearchView) findViewById(R.id.myownsearchview);
       SettingFirebaseAndListView();

    }


    private void SettingFirebaseAndListView() {

        mMessageListView = (ListView) findViewById(R.id.show_friends_listview);

        final List<AuthenticatedUser> authenticatedUserList = new ArrayList<>();
        showFriendsAdapter = new ShowFriendsAdapter(this, R.layout.show_friends_item_layout, authenticatedUserList);
        mMessageListView.setAdapter(showFriendsAdapter);

        mMessageListView.setTextFilterEnabled(true);
        setupSearchView();

    }

    private void setupSearchView() {
        searchView.setIconifiedByDefault(false);
        searchView.setOnQueryTextListener(this);
        searchView.setSubmitButtonEnabled(true);
        searchView.setQueryHint("Search Friend");
    }

    @Override
    public boolean onQueryTextSubmit(String newText) {
        return true;
    }

    @Override
    public boolean onQueryTextChange(String s) {

        mMessageListView.setFilterText(s);

        return false;
    }
    }

ShowFriendsAdapter.java CustomAdapter which extends ArrayAdapter and implements Filterable Interface

    public class ShowFriendsAdapter extends ArrayAdapter<AuthenticatedUser>  implements Filterable{

    Cursor cr;
    SQLiteDatabase db;
    Context context;
    List<AuthenticatedUser> listofAuthenticatedUsers;
    List<AuthenticatedUser> listofAuthenticatedUsers2;


    public ShowFriendsAdapter(Context context, int resource,List<AuthenticatedUser> objects) {


        super(context, resource, objects);
        this.context = context;
        listofAuthenticatedUsers = objects;
    }


    public Filter getFilter(){
        return  new Filter() {


            @Override
            protected FilterResults performFiltering(CharSequence constraint) {


                final FilterResults oReturn = new FilterResults();
                final ArrayList<AuthenticatedUser> results = new ArrayList<AuthenticatedUser>();
                if(listofAuthenticatedUsers2==null)
                    listofAuthenticatedUsers2=listofAuthenticatedUsers;
                if(constraint!=null){
                    if (listofAuthenticatedUsers2 != null && listofAuthenticatedUsers2.size() > 0){
                        for (final AuthenticatedUser g : listofAuthenticatedUsers2) {
                            if (g.getName().toLowerCase()
                                    .contains(constraint.toString()))
                                results.add(g);
                        }

                    }
                    oReturn.values = results;

                }

                return oReturn;
            }

            @Override
            @SuppressWarnings("unchecked")
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {

                listofAuthenticatedUsers = (ArrayList<AuthenticatedUser>) filterResults.values;
                notifyDataSetChanged();

            }
        };
    }


    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }



    @NonNull
    @Override
    public View getView(int position,View convertView,ViewGroup parent) {


        if (convertView == null) {
            convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.show_friends_item_layout, parent, false);
        }


        TextView userName = (TextView) convertView.findViewById(R.id.friend_name_textview);
        TextView userEmail = (TextView) convertView.findViewById(R.id.friend_email_id_textview);
        final ImageView userImage = (ImageView) convertView.findViewById(R.id.friend_name_image);


        assert currentItem != null;
        userName.setText(currentItem.getName());
        userEmail.setText(currentItem.getEmail());




        // If true , the app crashes
        userImage.setClickable(true);
        userImage.setEnabled(true);

        userImage.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View view) {



            }
        });

        return convertView;
    }


    }

AuthenticatedUser.java

    public class AuthenticatedUser {
    private String Name;
    private String Email;
    private String imageUrl;




    // constructor

    public AuthenticatedUser(){}

    public AuthenticatedUser(String name, String email, String imageUrl) {



        Name = name;
        Email = email;
        this.imageUrl = imageUrl;
    }

    public String getName() {


        return Name;
    }

    public void setName(String name) {


        Name = name;
    }

    public String getEmail() {


        return Email;
    }

    public void setEmail(String email) {


        Email = email;
    }

    public String getImageUrl() {


        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {


        this.imageUrl = imageUrl;
    }
    }

The row layout show_friends_item_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/friend_name_image"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="8dp"
            android:onClick="ChangeTheDisplayImage"
            android:src="@drawable/contacts"
            app:civ_border_color="#006064"
            app:civ_border_width="2dp"
            app:srcCompat="@drawable/contacts"/>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/friend_name_textview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Name"
                android:textColor="@color/black"
                android:textSize="@dimen/authen_user_size"/>

            <TextView
                android:id="@+id/friend_email_id_textview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Email ID"
                android:textColor="@color/black"
                android:textSize="8sp"/>

        </LinearLayout>


    </LinearLayout>




    </LinearLayout>

And my the main layout of my activity

show_online_users.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.google.firebase.udacity.friendlychat.friends.ShowFriendsActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        style="@style/HeaderBar"
        app:theme="@style/Theme.AppCompat.DayNight.DarkActionBar"
        app:popupTheme="@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar"
        android:elevation="4dp"
        />
    <SearchView
        android:id="@+id/myownsearchview"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@+id/my_toolbar"
        android:layout_marginTop="4dp"
        android:tooltipText="Search"/>

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefresh_Activity_show_Friends"
        android:layout_marginTop="110dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <ListView
        android:layout_marginTop="120dp"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/show_friends_listview"/>
    </android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
Niamatullah Bakhshi
  • 1,445
  • 16
  • 27
  • use [this](https://gist.githubusercontent.com/pskink/2dd4d17a93caf02ff696533e82f952b0/raw/f70974450c84978a0c5bc43e20bed5ec42c20333/MatchableArrayAdapter.java) generic `Filterable` adapter as a base class, instead of `ArrayAdapter` - override `matches()` method to implement filtering – pskink Feb 06 '18 at 21:47
  • @pskink Could you elaborate more please ? I created this class and copied everything, I also replaced ArrayAdapter with MatchableArrayAdapter. what to do next ? – Niamatullah Bakhshi Feb 06 '18 at 23:24
  • you dont have to copy anything: just create `class ShowFriendsAdapter extends MatchableArrayAdapter { ...` and override two methods: `onBind` and `matches()` (and of course one constructor), thats all – pskink Feb 07 '18 at 06:30
  • I think you should edit my code as per my requirements if you know what you are trying to say, Thanks. – Niamatullah Bakhshi Feb 07 '18 at 07:49
  • there isn't any problem in overriding the methods, I suggest you to edit my own code so that the confusion is avoided. – Niamatullah Bakhshi Feb 07 '18 at 07:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/164676/discussion-between-niamatullah-bakhshi-and-pskink). – Niamatullah Bakhshi Feb 07 '18 at 07:53

0 Answers0