使用RecyclerAdapter检索Firebase键值的位置

时间:2018-07-04 02:52:33

标签: firebase recycler-adapter

    FirebaseRecyclerOptions<Posts> firebaseRecyclerOptions =new FirebaseRecyclerOptions.Builder<Posts>
          ().setQuery(PostsReference,Posts.class)
          .build();


    firebaseRecyclerAdapter =new FirebaseRecyclerAdapter<Posts, PostsHolder>(firebaseRecyclerOptions) {
      @Override

      protected void onBindViewHolder(@NonNull PostsHolder holder, final int position, @NonNull Posts model) {

         holder.setPosts(model);
         holder.itemView.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                final String get_keys=firebaseRecyclerAdapter.getItem(position);


             }
         });






      }

      @NonNull
      @Override
      public PostsHolder onCreateViewHolder(@NonNull ViewGroup parent, int 
  viewType) {
          View  view=LayoutInflater.from(parent.getContext()).inflate(R.layout.all_post_layout,parent,false);



          return  new PostsHolder(view);



      }
  };
 recyclerView.setAdapter(firebaseRecyclerAdapter);


}

@Override
protected void onStart() {
    super.onStart();
    firebaseRecyclerAdapter.startListening();
}

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

    if (firebaseRecyclerAdapter!= null) {
        firebaseRecyclerAdapter.stopListening();
    }
}



public static class PostsHolder extends RecyclerView.ViewHolder {

 private CircleImageView profile_image;
 private ImageView post_image;
 private TextView Description;
 private TextView Date,Time, Has_Updated_Post,Has_Username;
 private Context mContext;





    public PostsHolder(View itemView) {
        super(itemView);


        Date=itemView.findViewById(R.id.post_date);
        Time=itemView.findViewById(R.id.post_time);
        /*Has_Updated_Post=itemView.findViewById(R.id.text);*/
        Has_Username=itemView.findViewById(R.id.post_user_name);
        Description=itemView.findViewById(R.id.post_description);
        profile_image=itemView.findViewById(R.id.post_profile_image);
        post_image=itemView.findViewById(R.id.post_image);






    }





    public  void setPosts(Posts posts){

      String users_name=posts.getName();
      Has_Username.setText(users_name);
      String users_description=posts.getDescription();
      Description.setText(users_description);
      String users_date=posts.getDate();
      Date.setText(" " +users_date);
      String users_time=posts.getTime();
      Time.setText("" +users_time);
     String users_image=posts.getImage();

  Picasso.with(itemView.getContext()).load(users_image).into(profile_image);
      String users_posts_image=posts.getPostimage();

Picasso.with(itemView.getContext()).load(users_posts_image).into(post_image);



    }

}

我需要从firebase数据库中检索Item的位置,我无法找到解决方案来获取位置,尝试在OnCreateViewHolder和OnBindViewHolder中对其进行访问,但它们似乎都不起作用, 我什至尝试返回getAdapterPosition,但应用程序每次都崩溃

请给我建议一个解决方案。

0 个答案:

没有答案