我想使用if条件
从我的回收视图
中删除一个项目
这是我的适配器类的代码。我想做的是,如果它的状态等于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);
要删除,您可以添加这一行
hotelList.remove(getAdapterPosition());
在ViewHolder类的onLongClick()方法中执行上述代码。
@Override
public boolean onLongClick(View view) {
//You can generate one list from which user can choose to delete.
hotelList.remove(getAdapterPosition());
return true;
}
你好!为什么不在将列表中的行传递给适配器之前删除它?
您可以在Activity中定义函数来执行此操作,然后将最终列表传递给您的适配器