添加到收藏夹

时间:2019-11-09 18:20:42

标签: android firebase-realtime-database android-recyclerview

我有一个问题,希望您能帮助我解决问题。

我正在将数据实时存储在Firebase数据库中的回收视图中,我想通过复选框添加功能,用户可以将其添加到我的收藏夹中,当我添加它时,我添加的鸟正在另一个循环视图中重新加载。

DATA ViewHolder

public class ViewHolder extends RecyclerView.ViewHolder   {


CheckBox favorito1;
FirebaseDatabase mfirebaseDatabase;
DatabaseReference mdatabaseReference;



View mView;

public ViewHolder(@NonNull View itemView) {
    super(itemView);


    mView = itemView;


    mfirebaseDatabase = FirebaseDatabase.getInstance();
    mdatabaseReference = mfirebaseDatabase.getReference();



    favorito1 = itemView.findViewById(R.id.san_miguel_favorite);

    itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            mClickListener.onItemClick(view,getAdapterPosition());



        }
    });

    itemView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {


            mClickListener.onItemLongClick(view,getAdapterPosition());
            return true;
        }
    });

}


public void setDatails(Context ctx, String title, String image, String description) {

    TextView mTitletv = mView.findViewById(R.id.rtitleTv);
    ImageView mimageIv = mView.findViewById(R.id.rImageView);
    TextView mDetailtv = mView.findViewById(R.id.san_miguel_description);





    Picasso.get().load(image).into(mimageIv);
    mDetailtv.setText(description);


}

private ViewHolder.ClickListener mClickListener;





public interface ClickListener{

    void onItemClick(View view, int position);
    void onItemLongClick(View view, int position);
}

public  void  setOnClickListener (ViewHolder.ClickListener clickListener){


    mClickListener = clickListener;

}

}

MainActivity

public class Alto_de_san_miguel extends AppCompatActivity {



LinearLayoutManager mlinearLayoutManager;
RecyclerView mrecyclerview;
FirebaseDatabase mfirebaseDatabase;
DatabaseReference mdatabaseReference;
FirebaseRecyclerAdapter<Model, ViewHolder> firebaseRecyclerAdapter;
FirebaseRecyclerOptions<Model> options;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.alto_de_san_miguel);





    mlinearLayoutManager = new LinearLayoutManager(this);
    mlinearLayoutManager.setReverseLayout(true);
    mlinearLayoutManager.setStackFromEnd(true);

    mrecyclerview = findViewById(R.id.reciclerview_san_miguel);

    mfirebaseDatabase = FirebaseDatabase.getInstance();
    mdatabaseReference = mfirebaseDatabase.getReference("san_miguel");










show_san_miguel();


}

private void show_san_miguel() {



    options = new FirebaseRecyclerOptions.Builder<Model>().setQuery(mdatabaseReference,Model.class).build();

    firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Model, ViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull Model model) {


            holder.setDatails(getApplicationContext(), model.getTitle(), model.getImage(), model.getDescription());

        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {


            View itemview = LayoutInflater.from(parent.getContext()).inflate(R.layout.fila_san_miguel, parent, false);

            ViewHolder viewHolder = new ViewHolder(itemview);
            viewHolder.setOnClickListener(new ViewHolder.ClickListener() {
                @Override
                public void onItemClick(View view, int position) {

                }

                @Override
                public void onItemLongClick(View view, int position) {

                }
            });


            return viewHolder;
        }
    };
    mrecyclerview.setLayoutManager(mlinearLayoutManager);
    firebaseRecyclerAdapter.startListening();
    mrecyclerview.setAdapter(firebaseRecyclerAdapter);


}

@Override
protected void onStart() {
    super.onStart();

    if(firebaseRecyclerAdapter != null){

        firebaseRecyclerAdapter.startListening();
    }
}
}

DATO模型

public class Model {

private String id;
private  String title, image, description;

public Model(String id, String title, String image) { }


public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}


public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}
}

enter image description here enter image description here

0 个答案:

没有答案