0

I have this application which retrieves data from a MySql database and parses it into a recyclerView. I have a editText and i want to be able to filter the recyclerView items while typing.

Here is my MainActivity (SearchAvtivity):

public class SearchActivity extends AppCompatActivity {
    private static final String TAG = "Search Activity";
    private Context mContext = SearchActivity.this;
    private static final int ACTIVITY_NUM = 1;

    String urlAddress = "myDatabaseAddressGoesHere.PHP";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        Log.d(TAG, "onCreate: started");
        setupBottomNavigationView();
        final RecyclerView rv = (RecyclerView) findViewById(R.id.SearchRv);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setItemAnimator(new DefaultItemAnimator());
        Downloader dl = new Downloader(SearchActivity.this,urlAddress,rv);
        dl.execute();
     }
          
         

    /**
     * Setup Bottom Navigation View
     */

    private void setupBottomNavigationView(){
        Log.d(TAG, "setupBottomNavigationView: Setting up Bottom Navigation View");
        BottomNavigationViewEx bottomNavigationViewEx = (BottomNavigationViewEx)findViewById(R.id.bottom_nav_bar);
        BottomNavigationViewHelper.setupBottomNavigationView(bottomNavigationViewEx);
        BottomNavigationViewHelper.enableNavigation(mContext,bottomNavigationViewEx);
        Menu menu = bottomNavigationViewEx.getMenu();
        MenuItem menuItem = menu.getItem(ACTIVITY_NUM);
        menuItem.setChecked(true);

    }
}

and here is the MyAdapter:

public class MyAdapter extends RecyclerView.Adapter<MyHolder> {

    Context c;
    ArrayList<SpaceCraft> spaceCrafts;
    List<MyHolder> displayedList;

    public MyAdapter(Context c, ArrayList<SpaceCraft> spaceCrafts) {
        this.c = c;
        this.spaceCrafts = spaceCrafts;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_adapter_vertical,parent,false);
        return new MyHolder(v);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        holder.nameTv.setText(spaceCrafts.get(position).getName());
        holder.addressTv.setText(spaceCrafts.get(position).getAddress());
        holder.phoneTv.setText(spaceCrafts.get(position).getPhone());
        holder.emailTv.setText(spaceCrafts.get(position).getEmail());

    }

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

Ido not know if you need to see the vlasses: downloader, parser, connector or holder. I so please tell me.

  • Possible duplicate of [Android - Implementing search filter to a RecyclerView](https://stackoverflow.com/questions/40754174/android-implementing-search-filter-to-a-recyclerview) – Raghavendra Sep 11 '17 at 11:14

2 Answers2

1

You can do it with a TextWatcher on your editText and inside it filter your list.

Example :

 ((EditText) findViewById(R.id.your_edit_text)).addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void afterTextChanged(Editable s) {
        String text = s.toString();
        ArrayList<String> yourCopyList = new ArrayList<>(yourList)
        yourList.clear();
        if (text.isEmpty()) {
            addAllYourDataToList();
        } else {
            text = text.toLowerCase();
            for (int i = 0; i<yourCopyList.size(); i++) {
                // Adapt the if for your usage
                if (yourCopyList.get(i).getName().toLowerCase().contains(text)) {
                    yourList.add("the string");
                }
            }
        }
        adapter.notifyDataSetChanged();
    }
});

In this example you remove all the data in your list and add only the data corresponding on your text filter

Vodet
  • 1,491
  • 1
  • 18
  • 36
  • where should i add this textWatcher? –  Sep 11 '17 at 12:01
  • 1
    this is your editText ((EditText) findViewById(R.id.your_edit_text)) and add on it a addTextChangedListener() this method take a textwatcher on argument. I suppose that your editText is inside your Activity so put this code in activity – Vodet Sep 11 '17 at 12:07
  • I did as you said, but in the last part part "yourList.add("the String)" I get the error : add (**************SpaceCract) in ArrayList cannot be applied to (java.lang.string) –  Sep 12 '17 at 07:30
  • I put the String type for the example, in you code you have to change it and put your SpaceCraft Object inside it – Vodet Sep 12 '17 at 07:35
0

You Should use a TextWatcher() For RecyclerView.

Arjun Solanki
  • 236
  • 1
  • 11
  • This is not an answer at all. When giving answers on Stackoverflow please use relevant code as an acceptable form of answer. – yams Aug 19 '20 at 14:36