在onBindViewHolder Firebase数据库中获取关键位置

时间:2018-09-28 02:17:48

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

我想在数据库中获得关键位置。

得到错误:getRef(position) : Cannot resolve method 'getRef(int)'

final String post_key = getRef(position).getKey();

这是我的代码CustomPostAdapter

public class CustomPostAdapter extends RecyclerView.Adapter<CustomPostAdapter.ViewHolder>{
    public List<Spacecraft> userList ;
    public Context context ;

    private boolean mProcessLike = false ;
    private DatabaseReference mDatabaseLike ;

  //  public ImageView imagePostt ;

    public CustomPostAdapter(Context context , List<Spacecraft> userList){
        this.userList = userList ;
        this.context = context;
    }

    @NonNull
    @Override
    public CustomPostAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_posts_item, parent,false);
        return new CustomPostAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {


        mDatabaseLike = FirebaseDatabase.getInstance().getReference().child("Likes");
        mDatabaseLike.keepSynced(true);


        final String post_key = getRef(position).getKey();

        final String name =  userList.get(position).getName();
        final String image_post = userList.get(position).getImage_post();
        final String descrip = userList.get(position).getDescrip();
        final String hachatg = userList.get(position).getHachtag();
        final String user = userList.get(position).getUser();
        final String time = userList.get(position).getTime();

        //Text Post
        holder.nameText.setText(name);
        holder.descripText.setText(descrip);
        holder.hachtagText.setText(" " + hachatg);
        holder.timeText.setText(" " + time);


        //Imgae Post if null set Visibtly Gone to ImageView
        if (TextUtils.isEmpty(image_post)){
            holder.imagePostt.setVisibility(View.GONE);

        }else {
            //Image Post
            Picasso.with(context).load(image_post).into(holder.imagePostt , new Callback() {
                @Override
                public void onSuccess() {
                    holder.imagePostt.setVisibility(View.VISIBLE);
                }
                @Override
                public void onError() {
                    holder.imagePostt.setVisibility(View.GONE);
                }
            });
        }

        //Onclick items
        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context , ContentActivity.class);
                intent.putExtra("NAME_KEY", name);
                intent.putExtra("HACHTAG_KEY", hachatg);
                intent.putExtra("DESCRIP_KEY", descrip);
                intent.putExtra("USER_KEY", user);
                intent.putExtra("IMAGE_POST", image_post);


                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        });

        //Button Like Onclice
        holder.mLikeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mProcessLike = true;

                if (mProcessLike){
                    mDatabaseLike.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {


                            //Chek if user already Like or not
                            if (dataSnapshot.child(post_key).hasChild(user)){



                            }else {

                                mDatabaseLike.child(post_key).child(user).setValue("RandomValue");
                                mProcessLike = false;
                            }

                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });
                }


            }
        });



    }

    @Override
    public int getItemCount() {
        return userList.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        View mView;

        public TextView nameText , hachtagText , descripText, urlText , timeText;
        public ImageView imagePostt ;
        public ImageButton mLikeBtn ;


        public ViewHolder(View itemView) {
            super(itemView);
            mView = itemView;


            nameText = (TextView)mView.findViewById(R.id.text_nameProblem);
            hachtagText = (TextView)mView.findViewById(R.id.text_nameUser);
          //  urlText = (TextView)mView.findViewById(R.id.textUrl);
            descripText = (TextView)mView.findViewById(R.id.text_Discription);
            timeText = (TextView)mView.findViewById(R.id.timeText);
            imagePostt = (ImageView)mView.findViewById(R.id.imagePost);
            mLikeBtn = (ImageButton)mView.findViewById(R.id.btnLike);




        }
    }
}

2 个答案:

答案 0 :(得分:1)

您应该使用具有方法getRef()的FirebaseRecyclerAdapter。跟随this link来学习如何使用它

答案 1 :(得分:0)

有两种方法可以解决此问题。第一个方法是将change(int**, int, int)作为属性存储在用户对象中,并使用:

key

第二个是简单地使用final String key = userList.get(position).getKey(); 并调用:

FirebaseRecyclerAdapter

如@Puf的here所述。