What are the accurate lines of code for search view in recycler view using items stored in string.xml under some string-array name

11 views Asked by At
  • Can someone seriously take and answer my question. When I use the following lines of code, the search view is working fine and not crashing but my issue is I want to extract items from string_array or string.xml and using the for loop as shown down in the second code which is not working:
RecyclerView recyclerView = findViewById(R.id.r1);

       adapter = new RecyclerAdapter(getApplicationContext(), 
       setSearchModels());
       recyclerView.setAdapter(adapter);
       recyclerView.setLayoutManager(new LinearLayoutManager(this));
   }

   public ArrayList<SearchModel> setSearchModels() {
       ArrayList<SearchModel> holder = new ArrayList<>();
       SearchModel ob1 = new SearchModel();
       ob1.setItemNames("Mango");
       holder.add(ob1);

       SearchModel ob2 = new SearchModel();
       ob2.setItemNames("Apple");
       holder.add(ob2);

       return holder;
   }
  • But if I donot add items in Main using the above code and instead
    extract items from string.xml using the following code,my search view crashes and closes on typing in it. please someone look carefully at these codes and show me the full code lines to create a search view on recycler view while taking items from string_array withour crashing my app.Is there something wrong with getApplicationcontext or what else. The following code is crashing and closing the app, what and where is the issue,
    please suggest:
 RecyclerView recyclerView = findViewById(R.id.r1);
        setSearchModels();
        RecyclerAdapter adapter = new RecyclerAdapter(this, 
        searchModels);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }
    vate void setSearchModels() {
        String[] searchItems = 
        getResources().getStringArray(R.array.search_items);
        for (int i = 0; i < searchItems.length; i++) {
            searchModels.add(new SearchModel(searchItems[i]));

        }
0

There are 0 answers