'using search bar in home fragment to search for drawings, i tried but only the search bar appear without the ability to search particular drawing

hello i want to implement search bar in my home fragment class but i didn't know how to code the right command, i want to add a search bar in my home fragment to search for any drawing in the app but i could not always gets error.


home fragment.

   public class HomeFragment extends Fragment{


    private ArrayList<DrawingItem> drawingItems = new ArrayList<>();
   

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_home, container, false);

        RecyclerView recyclerView = root.findViewById(R.id.recycleView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(new DrawingAdapter(drawingItems, getActivity()));
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        
        drawingItems.add(new DrawingItem(R.drawable.girl_with_a_pearl_earing, "Girls with a pearl Earring","0","0",getString(R.string.drawing1_desc)));
        drawingItems.add(new DrawingItem(R.drawable.self_portait_ear, "Self Portrait Ear","1","0",getString(R.string.drawing2_desc)));
        drawingItems.add(new DrawingItem(R.drawable.vase, "Vase with 12 Sunflowers","2","0",getString(R.string.drawing3_desc)));
        drawingItems.add(new DrawingItem(R.drawable.night_cafe, "Coffee Terrace at Night","3","0",getString(R.string.drawing4_desc)));
        drawingItems.add(new DrawingItem(R.drawable.gurnica, "Guernica","4","0",getString(R.string.drawing5_desc)));
        drawingItems.add(new DrawingItem(R.drawable.the_scream, "The Scream","5","0",getString(R.string.drawing6_desc)));
        drawingItems.add(new DrawingItem(R.drawable.the_falling_rocket, "Nocturne in Black and Gold, the Falling Rocket","6","0",getString(R.string.drawing7_desc)));
        drawingItems.add(new DrawingItem(R.drawable.monalisa, "Monalisa","7","0",getString(R.string.drawing8_desc)));
        drawingItems.add(new DrawingItem(R.drawable.impresstion_sunrise, "Impression, Sunrise","8","0",getString(R.string.drawing9_desc)));
        drawingItems.add(new DrawingItem(R.drawable.the_last_supper,"The Last Supper", "9","0",getString(R.string.drawing9_desc)));
        drawingItems.add(new DrawingItem(R.drawable.water_lilies, "Water Lilies","10","0",getString(R.string.drawing10_desc)));
        drawingItems.add(new DrawingItem(R.drawable.the_magpie, "The Magpie","11","0",getString(R.string.drawing11_desc)));

        return root;
    }}

mainactivity

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    BottomNavigationView navView = findViewById(R.id.nav_view);

    AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
            R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
    NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
    NavigationUI.setupWithNavController(navView, navController);
}}

toolbar

 <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/action_bar"
    android:icon="@drawable/ic_baseline_search_white24"
    app:showAsAction="collapseActionView|ifRoom"
    android:title="@android:string/ok"
    app:actionViewClass="androidx.appcompat.widget.SearchView" />

i tried multiple coding but none of them worked, so please if you know help me



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source