1

Well, I have a RecyclerView, whose Items contains name and subtext. I created a class MyObject in order to fill them. With my Adapter, I am filling the RecyclerView. I want to search for a String name in my RecyclerView and that the application showed all the Items whose names matching the search (including name and subtext). But I don't know how I can do this. It is really hard to explain for me, so you will probably understand more from my code. I can provide more information or code if you need. (Sorry for some Russian text, but it doesn't really matter anyway).

MainActivity.java code:

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, SearchView.OnQueryTextListener {
    DatabaseHelper myDB;
    MyAdapter mAdapter;
    RecyclerView mRecyclerView;
    int year,month,day;
    int count;
    ArrayList<MyObject> myObjects;


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

        myDB = new DatabaseHelper(this);
        Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
        Cursor cursor = myDB.forCount(year,month,day);
        count = cursor.getCount();
        if (count != 0) {
            alertWhenDelete(count);}
        myDB.deleteIstekshiySrokGodnosy(year,month,day);


        mRecyclerView = findViewById(R.id.recyclerView);

        myObjects = new ArrayList<>();


        Cursor data = myDB.getListContents();
        if(data.getCount() == 0){
            Toast.makeText(this, "Ваш список товаров пуст!",Toast.LENGTH_LONG).show();
        }else{
            while(data.moveToNext()){

                myObjects.add(new MyObject(data.getString(1), "Дата окончания срока годности: "+data.getInt(4)+"."+data.getInt(3)+"."+data.getInt(2)));


                mAdapter = new MyAdapter();
                mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
                mRecyclerView.setAdapter(mAdapter);

                mAdapter.updateData(myObjects);

            }
        }
    ...
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        MenuItem menuItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) menuItem.getActionView();
        searchView.setOnQueryTextListener(this);
        return true;
    }
    ...
    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {

        String userInput = newText.toLowerCase();
        ArrayList<MyObject> newList = new ArrayList<>();
        for (String name : myObjects){
            if(name.toLowerCase().contains(userInput)) {
                newList.add(name);
            }
        }

        mAdapter.updateData(newList);
        return true;
    }
}

MyAdapter.java code:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> implements View.OnClickListener {
private ArrayList<MyObject> mItems = new ArrayList<>();
public String nametext;
DatabaseHelper myDB;

int idactivity;
String nameactivity;
int yearactivity;
int monthactivity;
int dayactivity;
int priceactivity;





@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
    MyViewHolder vh=new MyViewHolder(view);
    view.setOnClickListener(this);

    return vh;
}


@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    holder.bindData(getItem(position));
}

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

public void updateData(ArrayList<MyObject> items) {
    mItems = new ArrayList<>(items);
    notifyDataSetChanged();
}

private MyObject getItem(int position) {
    if (position >= 0 && position < mItems.size())
        return mItems.get(position);

    return null;
}

@Override
public void onClick(View v) {

    idactivity = 0;
    nameactivity = null;
    yearactivity = 0;
    monthactivity = 0;
    dayactivity = 0;
    priceactivity = 0;


    myDB = new DatabaseHelper(v.getContext());

    TextView clickedName=(TextView)v.findViewById(R.id.title);
     nametext = clickedName.getText().toString();
    Toast.makeText(v.getContext(),nametext,Toast.LENGTH_LONG).show();
    Cursor c = myDB.showDataonSelect(nametext);



    if(c.moveToFirst()) {

        idactivity = c.getInt(0) ;
        nameactivity = c.getString(1) ;
        yearactivity = c.getInt(2) ;
        monthactivity = c.getInt(3) ;
        dayactivity = c.getInt(4);
        priceactivity = c.getInt(5);


    }

    Log.d("TAG",
            "ID = " + idactivity +
                    ", name = " + nameactivity +
                    ", year = " + yearactivity +
                    ", month = " + monthactivity +
                    ", day = " + dayactivity +
                    ", price = " + priceactivity);

    Intent myIntent = new Intent(v.getContext(), Main3Activity.class);
    Bundle extras = new Bundle();
    extras.putString("Name", nameactivity);
    extras.putInt("Year", yearactivity);
    extras.putInt("Month", monthactivity);
    extras.putInt("Day", dayactivity);
    extras.putInt("Price", priceactivity);
    myIntent.putExtras(extras);

    v.getContext().startActivity(myIntent);


}



class MyViewHolder extends RecyclerView.ViewHolder {
    TextView title;
    TextView subTitle;

    MyViewHolder(View itemView) {
        super(itemView);
        title = itemView.findViewById(R.id.title);
        subTitle = itemView.findViewById(R.id.sub_title);
    }

    void bindData(MyObject myObject) {
        if (myObject == null)
            return;

        title.setText(myObject.title);
        subTitle.setText(myObject.subTitle);
    }
  }
}

MyObject.java code:

public class MyObject {
public String title;
public String subTitle;

public MyObject(String title, String subTitle) {
    this.title = title;
    this.subTitle = subTitle;
  }
}

When I am searching for Item's name in Search, I want to see that application shows me Items with names that are matching the search and their subtext.

Riajur Rahman
  • 1,976
  • 19
  • 28
Amir
  • 83
  • 11

1 Answers1

2

Use Filterable

Take another List to keep the actual list, initialize it with myObjects and set the values in constructor/ when you update the list data.

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> 
implements View.OnClickListener, Filterable {
 private  List<MyObject> myObjects;
 private List<MyObject> reservedList;
//....
//remove mItems from your class replace it with myObjects
public MyAdapter(List<MyObject> myObjects){
       this.myObjects = myObjects;
       this.reservedList =  myObjects;
   }


    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
                String charString = charSequence.toString().toLowerCase();
                if (!charString.isEmpty()) {
                    List<MyObject> filteredList = new ArrayList<>();
                    for (MyObject row : reservedList) {
                        if (row.title.toLowerCase().contains(charString) || row.subTitle.toLowerCase().contains(charString)) {
                            filteredList.add(row);
                        }
                    }
                    myObjects = filteredList;
                } else {
                    myObjects = reservedList;
                }

                FilterResults filterResults = new FilterResults();
                filterResults.values = myObjects;
                return filterResults;
            }

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

set Adapter this way

while(data.moveToNext()){

                myObjects.add(new MyObject(data.getString(1), "Дата окончания срока годности: "+data.getInt(4)+"."+data.getInt(3)+"."+data.getInt(2)));


                mAdapter = new MyAdapter(myObjects);
                mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
                mRecyclerView.setAdapter(mAdapter);
         // mAdapter.updateData(myObjects); you don't need this


            }

Now on your activity pass the string you want to search mAdapter.getFilter().filter(query);

@Override
public boolean onQueryTextChange(String newText) {
    mAdapter.getFilter().filter(newText);
    return true;
}
shb
  • 5,957
  • 2
  • 15
  • 32
  • Thank you for your answer, I'm busy now so I will try to do it a little later. – Amir Jun 09 '19 at 10:50
  • I added what you suggested, but in order not to make it even worse, I will immediately say that I have red myObjects in MyAdapter (myObjects = filteredList;),(myObjects = reservedList;),(filterResults.values = myObjects;),(myObjects = (ArrayList) filterResults.values;). What should I do to make it work? – Amir Jun 09 '19 at 11:12
  • Well, in the code that you suggested to me for getFilter all myObjects are shown in red. What do I need to do? – Amir Jun 09 '19 at 12:36
  • Implement Filterable interface. public class MyAdapter extends RecyclerView.Adapter implements View.OnClickListener, Filterable { //... – shb Jun 09 '19 at 12:38
  • and import necessary packages. – shb Jun 09 '19 at 12:41
  • https://drive.google.com/file/d/1w_V5TSB_j9inBn2d_v3FieYLyWDBatxH/view?usp=sharing – Amir Jun 09 '19 at 12:45
  • I did it "Implement Filterable interface. public class MyAdapter extends RecyclerView.Adapter implements View.OnClickListener, Filterable" – Amir Jun 09 '19 at 12:46
  • I would be very grateful if you would change your answer according to the image I sent – Amir Jun 09 '19 at 12:48
  • check the updated answer. You need to change myObjects type to List myObjects; – shb Jun 09 '19 at 12:50
  • Now the application starts, but when I enter something in the search, nothing changes (search does not work). – Amir Jun 09 '19 at 12:54
  • are you passing the your query to the adapter? and I think you should invest a bit more time to your project to figure out these easy issues. Share your updated MyAdapter class. where are you initializing reserved list? – shb Jun 09 '19 at 12:57
  • I did everything as you suggested in the answer. – Amir Jun 09 '19 at 13:00
  • Share your MyAdapter class. paste it somewhere and share the link – shb Jun 09 '19 at 13:02
  • https://drive.google.com/file/d/1VvnJjwe93x5uzdVY8VZk9A47IaRG3EP-/view?usp=sharing – Amir Jun 09 '19 at 13:04
  • Yes, I understand that I need to deal with this myself, please forgive me, but I simultaneously do other work in this project. – Amir Jun 09 '19 at 13:05
  • You didn't even initialize reservedList. in your code initially there was no mItems, you don't need mItems, myObjects is doing the work for you. – shb Jun 09 '19 at 13:08
  • Thank you very much! I apologize for the inconvenience. – Amir Jun 09 '19 at 13:42