Ahmed Mohamed
Ahmed Mohamed

Reputation: 31

i implemented all thing in searchview but i cant know what is the error

when i click search no result come i tried to search by name but no result i used filterable but i dont no the reason only recyclerview all name appear in activity not the name that i selected i tried everything and hope find solution for that

 RecyclerView.Adapter <ClientAdapter.ClientHolder> implements Filterable{ 

 ArrayList <name> mylist;
 Context context;
 ArrayList<name> arraylist;;
 public ClientAdapter (ArrayList<name>mylist,Context context){
    this.mylist=mylist;
    this.context=context;
}
@Override
public ClientHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view= LayoutInflater.from(context).inflate(R.layout.clientexp,parent,false);
    ClientHolder holder=new ClientHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(ClientHolder holder, int position) {

    holder.img.setImageBitmap(convertToBitmap(mylist.get(position).getImage()));
    holder.Name.setText(mylist.get(position).getName().toString());
    final int idd=mylist.get(position).getId();
    final String id=Integer.toString(idd);
    holder.layout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Bundle bundle=new Bundle();
            Intent intent=new Intent(context,DeletePerson.class);
            bundle.putString("id",id);
            intent.putExtras(bundle);
            view.getContext().startActivity(intent);


        }
    });

}


@Override
public int getItemCount() {
    return mylist.size();
}

@Override
public Filter getFilter() {
    return new Filter() {
        @Override//charsequance حروف مسلسله
        protected FilterResults performFiltering(CharSequence charSequence) {
            String charString=charSequence.toString();
            if(charString.isEmpty())
            {
                arraylist=mylist;
            }else {
                ArrayList<name>filteredlist=new ArrayList<>();
                for(name filtername:mylist)
                {
                    if(filtername.getName().toLowerCase().contains(charString.toLowerCase()))
                    {
                        filteredlist.add(filtername);
                    }
                }
                arraylist=filteredlist;
            }
            FilterResults filterResults=new FilterResults();
            filterResults.values=arraylist;
            filterResults.count =arraylist.size();
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            arraylist=(ArrayList<name>)filterResults.values;
            notifyDataSetChanged();

        }
    };
}

public class ClientHolder extends RecyclerView.ViewHolder {
    TextView Name;
    LinearLayout layout;
    ImageView img;
    public ClientHolder(View view) {
        super(view);
        Name=(TextView)view.findViewById(R.id.name);
        layout=(LinearLayout)view.findViewById(R.id.linear);
        img=(ImageView)view.findViewById(R.id.imgid);
    }
}
private Bitmap convertToBitmap(byte[] b){

    return BitmapFactory.decodeByteArray(b, 0, b.length);

}


}

@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.search, menu);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView   searchView = (SearchView) menu.findItem(R.id.action_search)
            .getActionView();
    searchView.setSearchableInfo(searchManager
            .getSearchableInfo(getComponentName()));
    searchView.setMaxWidth(Integer.MAX_VALUE);

    // listening to search query text change
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            // filter recycler view when query submitted
            adapter.getFilter().filter(query);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String query) {
            // filter recycler view when text is changed
            adapter.getFilter().filter(query);
            return false;
        }
    });
    return true;
}

@Override
public boolean onOptionsIte`enter code here`mSelected(MenuItem item) {



    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_search) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}}

Upvotes: 0

Views: 46

Answers (1)

Bishoy Kamel
Bishoy Kamel

Reputation: 2355

you are using mylist which is the original unfiltered list. and you are filtering arraylist which the adapter has nothing to do with it. it does not know about it. your item are filtered but you are using the wrong list.

ArrayList <name> mylist;
ArrayList<name> arraylist;

change your construcot to.

public ClientAdapter (ArrayList<name>mylist,Context context){
this.mylist=mylist;
this.arraylist=mlist;  //use this as your acual list--> inside onBindViewHolder and getCount()
this.context=context;
 }

Note:

rename the two lists (originalist and filteredList) for convenience.

Upvotes: 0

Related Questions