android - 如何以编程方式从 recyclerview 中删除项目?
问题描述
我想从我的recyclerview
使用中删除一个项目if condition
这是我的适配器类的代码。我想要做的是从显示中删除它的状态是否等于unlive
我正在从 firebase 检索数据
ArrayList<Adapter_Hotels> hotelsList;
Context context;
public Hotels_Adapter(ArrayList<Adapter_Hotels> list, Context context) {
this.hotelsList = list;
this.context = context;
}
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.listview_layout, parent, false);
return new MyHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyHolder holder, int i) {
holder.hName.setText(hotelsList.get(i).getTitle());
holder.hAddress.setText(hotelsList.get(i).getProvince() + ", " + hotelsList.get(i).getCountry());
Picasso.get().load(hotelsList.get(i).getUrl_path()).fit().into(holder.hImage);
String status = hotelsList.get(i).getStatus();
if (status.equals("unlive")) {
removeItem(holder.getAdapterPosition());
}
}
@Override
public int getItemCount() {
return hotelsList.size();
}
public void removeItem(int position){
hotelsList.remove(position);
this.notifyItemRemoved(position);
}
我的活动代码
mRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot hotelsSnapshot : dataSnapshot.getChildren()) {
Adapter_Hotels hotels = hotelsSnapshot.getValue(Adapter_Hotels.class);
String hotel_status = hotels.getStatus();
String hotel_name = hotels.getTitle();
String hotel_image = hotels.getUrl_path();
String hotel_province = hotels.getProvince();
String hotel_country = hotels.getCountry();
String hn = hotels.setTitle(hotel_name);
String hi = hotels.setUrl_path(hotel_image);
String hp = hotels.setProvince(hotel_province);
String hc = hotels.setCountry(hotel_country);
String hs = hotels.setStatus(hotel_status);
hotelList.add(new Adapter_Hotels(hn, hi, hc, hp, hs));
Log.v("DSDS", String.valueOf(hotelList.size()));
dialog.dismiss();
getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
hotelsAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
hotelsAdapter = new Hotels_Adapter(hotelList, getContext());
mListview.setLayoutManager(new LinearLayoutManager(getContext()));
mListview.setAdapter(hotelsAdapter);
解决方案
你好!为什么在将列表传递给适配器之前不删除列表行?
您可以在 Activity 中定义函数来执行此操作,然后将最终列表传递给您的适配器
推荐阅读
- node.js - AWS iam.deleteAccessKey 在 Node10 Lambda 中不可靠
- c - 在链接/编译时静态包含所有库而不是库的一部分
- sql-server - 作为服务运行时 ODBC SQLDriverConnect 崩溃
- react-native - 如何通过本机反应显示来自 JSON 的正确 html 符号
- zsh - zsh 不打印从标准输入接收到的命令
- javascript - 如何在写作时将表情符号字符串转换为它的图标?
- r - 将 shinydashboard 上传到 shiny.io 时出错
- apache-spark - 使用 Window() 计算 PySpark 中数组的滚动总和?
- python - Python:输入中的Split()方法改变基于文本的RPG游戏中的“if”语句结果
- wavefront - 波前材料文件是否需要指定所有颜色类型,还是只能使用一种?