如何以编程方式从recyclerview删除项目?

时间:2019-03-17 15:29:58

标签: android firebase-realtime-database 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);

2 个答案:

答案 0 :(得分:0)

要删除,您可以添加此行

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;
    }

答案 1 :(得分:0)

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

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