Unknown Creation Unknown Creation - 29 days ago 12
Java Question

setFilter help needed

So basically I am writing creating an

Activity
which shows a list of Beaches in a
RecyclerView
. What is suppose to happen is that on the search menu above when the user begins typing in the Beach they are looking for, the list of beaches narrows eventually to the beach they are looking for. So I am literally on my last line of code for this to be fully functional but I keep getting a error on the
setFilter
part of this line

adapter.setFilter(newlist);


Below is my code, please advise.

BeachActivity.java

public class BeachActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
Toolbar toolbarbeach;
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
ArrayList<beachesalbum> arrayList = new ArrayList<>();
String [] b_name = {"South Beach","North Beach","Another Beach","Beachman"};
int [] b_pic = {R.drawable.b1,R.drawable.b2,R.drawable.b3,R.drawable.b4,};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_beach);
toolbarbeach=(Toolbar)findViewById(R.id.toolbar_beaches);
setSupportActionBar(toolbarbeach);
getSupportActionBar().setTitle("Beaches");
recyclerView = (RecyclerView)findViewById(R.id.recyclerview);
layoutManager = new GridLayoutManager(this,2);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);

int count = 0;

for (String Name :b_name)
{
arrayList.add(new beachesalbum(Name,b_pic[count]));
count++;
}
adapter = new RecyclerAdapterBeaches(arrayList);
recyclerView.setAdapter(adapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_items,menu);
MenuItem menuItem = menu.findItem(R.id.beachsearch);
SearchView searchView = (SearchView)MenuItemCompat.getActionView(menuItem);
searchView.setOnQueryTextListener(this);
return true;
}

@Override
public boolean onQueryTextSubmit(String query) {
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
newText = newText.toLowerCase();
ArrayList<beachesalbum> newlist = new ArrayList <>();

for(beachesalbum beachesalbum : arrayList)
{
String name = beachesalbum.getName().toLowerCase();
if (name.contains(newText));
newlist.add(beachesalbum);
}
adapter.setFilter(newlist);

return true;
}
}


As you can see my error is at
adapter.setFilter(newlist);
above

beachesalbum.java

public class beachesalbum {
private int img_id;
private String name;
public beachesalbum(String name,int img_id)
{
this.setName(name);
this.setImg_id(img_id);
}

public int getBeach_id() {
return beach_id;
}

public void setBeach_id(int beach_id) {
this.beach_id = beach_id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

private int beach_id;

beachesalbum(int img_id)
{
setImg_id(img_id);
}



public int getImg_id() {
return img_id;
}

public void setImg_id(int img_id) {
this.img_id = img_id;
}
}


AND FINALLY
RecyclerAdapterBeaches.java

public class RecyclerAdapterBeaches extends RecyclerView.Adapter<RecyclerAdapterBeaches.BeachesViewHolder>{
ArrayList<beachesalbum> arrayList = new ArrayList<>();
RecyclerAdapterBeaches(ArrayList<beachesalbum>arrayList)
{
this.arrayList = arrayList;
}
@Override
public BeachesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout,parent,false);

return new BeachesViewHolder(view);
}

@Override
public void onBindViewHolder(BeachesViewHolder holder, int position) {
holder.b_pic.setImageResource(arrayList.get(position).getImg_id());
holder.b_id.setText(arrayList.get(position).getName());

}

@Override
public int getItemCount() {

return arrayList.size();
}
public static class BeachesViewHolder extends RecyclerView.ViewHolder
{
ImageView b_pic;
TextView b_id;

public BeachesViewHolder(View itemView) {
super(itemView);
b_pic=(ImageView)itemView.findViewById(R.id.beachpic);
b_id=(TextView)itemView.findViewById(R.id.beaches_id);

}
}

public void setFilter(ArrayList<beachesalbum>newlist)
{arrayList=new ArrayList<>();
arrayList.addAll(newlist);
notifyDataSetChanged();}
}


Please lend your assistance.

Answer

In your BeachActivity you're setting the adapter with arrayList. RecyclerView adapter keeps the reference of your arrayList. So you if you need to filter out some items from your RecyclerView, you need to make the changes in the arrayList.

@Override
public boolean onQueryTextChange(String newText) {
    newText = newText.toLowerCase();

    // Clear the arrayList first.
    arrayList.clear();

    // If the search text is empty, repopulate the arrayList
    if(newText.length == 0) {
        int count = 0;
        for (String Name :b_name) {
            arrayList.add(new beachesalbum(Name,b_pic[count]));
            count++;
        }
    }

    else {
        int count = 0;
        for (String Name :b_name) {
            if(Name.contains(newText))
                arrayList.add(new beachesalbum(Name,b_pic[count]));
            count++;
        }
    }

    adapter.setFilter(arrayList);

    return true;
}

And the setFilter function in your adapter

public void setFilter(ArrayList<beachesalbum> newlist) {
    this.arrayList.clear();
    this.arrayList.addAll(newlist);
    notifyDataSetChanged();
}

Update

And yes, from the previous answer. You need to take the RecyclerAdapterBeaches adapter instead of RecyclerView.Adapter adapter. I missed that part. Thanks @loosethinker.

RecyclerAdapterBeaches adapter;