Delete data from firebase recycler view adapter without deleting in the database

折月煮酒 提交于 2019-12-25 05:23:22

问题


That is it, can i remove a view from firebase recycler view adapter, but not deleting in the database?How?

 final FirebaseRecyclerAdapter<Game,GameViewHolder> firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Game, GameViewHolder>(
            Game.class,
            R.layout.row,
            GameViewHolder.class,
            mdatabase

    ) {
        @Override
        protected void populateViewHolder(GameViewHolder viewHolder, Game model, final int position) {

            viewHolder.setTitle(model.getGame_name());
            viewHolder.setJugado(model.getJugado());
            viewHolder.setCreator(model.getCreator_name());

    recyclerView.setAdapter(firebaseRecyclerAdapter);


}

回答1:


FirebaseRecyclerAdapter doesn't support this. The whole point of FirebaseRecyclerAdapter is that it stays in sync with the contents of the location that you originally gave it. If something deletes data from the database, it will be automatically and unconditionally deleted from your adapter.

If you want this behavior, you will have to implement your own RecyclerView adapter, preferably one that does not use an active listeners to be notified of every change to the location of the data. You would have to listen to the entire contents just once, then send that to an adapter to populate a RecyclerView.




回答2:


And how about this in ViewHolder?

itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                itemView.setVisibility(View.INVISIBLE);
            }
        });



回答3:


Yes you can. You have to add this in your GameViewHolder class:

  1. Create a function and get reference of your cardview
  2. Create a layout params and Create an instance of the parent of your cardview then set the height and the width equals to 0

public  void  Remove() {
    cardView = mView.findViewById(R.id.cardview);
    cardView.setLayoutParams(new FrameLayout.LayoutParams(0,0));
}
  1. Use the function

viewHolder.remove();


来源:https://stackoverflow.com/questions/41925871/delete-data-from-firebase-recycler-view-adapter-without-deleting-in-the-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!