所以基本上我写的是创建一个Activity
来显示RecyclerView
中的海滩列表。假设发生的是,当用户开始在他们正在寻找的海滩上打字时,在上面的搜索菜单上,海滩列表最终变窄到他们正在寻找的海滩。所以我真的在我的最后一行代码上完全正常运行,但我在这行的setFilter
部分一直出现错误
adapter.setFilter(newlist);
以下是我的代码,请指教。
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.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();}
}
请借助你。
答案 0 :(得分:2)
setFilter
RecyclerView.Adapter
函数不存在,adapter
是RecyclerAdapterBeaches
变量的类型。由于您始终为其分配setFilter
,因此您应该将其更改为该类型。
否则,它无法确保对象具有public class BeachActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
Toolbar toolbarbeach;
RecyclerView recyclerView;
RecyclerAdapterBeaches adapter; // Update this line
// ...
}
功能。
(defun is-prime (n &optional (d (- n 1)))
(or (= d 1)
(and (/= (rem n d) 0)
(is-prime n (- d 1)))))
(defun my-func (a b)
(let (lst '())
(loop for i from a to b do
(if (is-prime i)
(push i lst)
)
)
(reverse lst))
)
(defmacro do-primes ((var startv endv) &body body)
`(my-func ,startv ,endv) `(,@body)
)
(do-primes (n 5 15) (fresh-line) (princ n))
答案 1 :(得分:0)
在BeachActivity
中,您使用arrayList
设置适配器。 RecyclerView
适配器会保留arrayList
的引用。因此,如果您需要过滤掉RecyclerView
中的某些项目,则需要在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;
}
适配器中的setFilter
功能
public void setFilter(ArrayList<beachesalbum> newlist) {
this.arrayList.clear();
this.arrayList.addAll(newlist);
notifyDataSetChanged();
}
<强>更新强>
是的,从上一个答案开始。您需要使用RecyclerAdapterBeaches adapter
代替RecyclerView.Adapter adapter
。我错过了那一部分。谢谢@loosethinker。
RecyclerAdapterBeaches adapter;