基本的に私はRecyclerView
のビーチのリストを示すActivity
の作成を書いています。考えられるのは、上記の検索メニューで、ユーザーが探しているビーチでの入力を開始すると、最終的には探しているビーチのビーチのリストが狭くなるということです。だから私は、これは完全に機能するためのコードの私の最後の行に文字通りですが、私は、この行の下setFilter help
adapter.setFilter(newlist);
のsetFilter
一部が私のコードでのエラーを得続ける、アドバイスしてください。あなたは私のエラーを見ることができるように
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;
}
}
はadapter.setFilter(newlist);
上記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;
}
}
し、最終的に RecyclerAdapterBeaches.jaでありますva
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();}
}
お願いします。
*のSetFilter *上のエラー? - **どのようなエラー**の –
可能な重複[SearchViewとRecyclerViewをフィルタリングする方法](http://stackoverflow.com/questions/30398247/how-to -filter-a-recyclerview-with-a searchview) –