How can i show my filtered data on another screen in android?

56 views Asked by At

I am working on an app where at my home fragment I have a searchview under that there is a recyclerview where I am showing the whole data but when user uses searchview and search for any data now I want to show the data to another fragment, Ho can I achieve that?? please guide me

IMAGEenter image description here

CODE FOR SEARCH ON HOME FRAGMENT

  @Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    if (searchView != null) {
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {

                pd.setTitle("Searching");
                pd.show();

                Runnable progressRunnable = new Runnable() {

                    @Override
                    public void run() {
                        search(query);
                        pd.cancel();
                    }
                };

                Handler pdCanceller = new Handler();
                pdCanceller.postDelayed(progressRunnable, 3000);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {


                if (newText.isEmpty())
                {
                    getData();
                }

                return false;
            }
        });
    }

    getDataCompleteData();
    
}



//SEARCH METHOD
private void search(String s) {

     mydatalist = new ArrayList<>();

    for (RecyclerviewModel object : datalist) {
        if (object.getSearching().contains(s.toLowerCase().trim())) {
            mydatalist.add(object);
        }

        RecyclerviewAdapter adapter = new RecyclerviewAdapter(mydatalist, getContext(), HomeFragment.this);
        recyclerView.setAdapter(adapter);

    }


}

THIS IS MY CODE FOR SEARCH ON HOME FRAGMENT BUT THIS CODE SHOWS THE FILTERED(SEARCHED DATA) ON THE SAME FRAGMENT, I WANT THAT WHEN USER ENTER A QUERY IN SEARCHVIEW, THEN SEARCHED DATA SHOULD BE SHOWN ON THE NEW FRAGMENT.

1

There are 1 answers

20
Khush Parmar On

From your requirement i can not able to understand your actual approach to show the data. As i understand this approach to do the same don't know this will fulfil your requirement which you're looking for.

You can transfer data between fragments using bundle. After user search any value you will get the filtered list from the main data list, now you can pass this filtered list using the code below to put when you get the filtered list from your search function in your case you have mydatalist in your search function. Make sure to create your data class of the list Serializable.

    Bundle bundle = new Bundle();
    bundle.putParcelableArrayList("list", mydatalist); 
    
    Fragment2 fragment2 = new Fragment2();
    fragment2.setArguments(bundle);
    
    getFragmentManager()
          .beginTransaction()
          .replace(<Your container id>, fragment2)
          .commit();

Now in your second fragment get the list like the code below and set as per your requirement.

    Bundle bundle = this.getArguments();
    
    if(bundle != null){
         // handle your code here.
         <Your List Type> = listbundle.getParcelableArrayList("list");
    }