首页 > 解决方案 > 如何以编程方式从 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);

标签: androidfirebase-realtime-databaseandroid-recyclerview

解决方案


你好!为什么在将列表传递给适配器之前不删除列表行?

您可以在 Activity 中定义函数来执行此操作,然后将最终列表传递给您的适配器


推荐阅读