在 Android 中对使用 Firebase 的点赞计数器感到困惑

Confused with Likes Counter using Firebase in Android

我是初学者,我正在从事一个 android 项目,用户喜欢 post 管理员 post 为他们服务的项目。当用户点击赞按钮时,赞会更新到 firebase。问题是,即使用户喜欢或不喜欢 post,点赞数仍在增加。请查收。

注:

Log.d(TAG, "Liked onDataChange: "+isLiked);

returns false 无论我们喜欢还是不喜欢 shinebutton。 Shinebutton 是一个图书馆。

Link 到它的 GitHub : Shinebutton Github

onLike 方法:

        @Override
        public void onLike(View v, int position) {

            UploadImage selectedItem = mUploads.get(position);
            String selectedKey = selectedItem.getKey();
            final ShineButton shineButton = new ShineButton(Objects.requireNonNull(getActivity()));
            shineButton.setBtnFillColor(RED);
            shineButton.setShapeResource(R.raw.heart);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100,100);
            shineButton.setLayoutParams(layoutParams);



            final DatabaseReference likesRef = FirebaseDatabase.getInstance().getReference().child("ImageUploads").child(selectedKey).child("likes");
            likesRef.addListenerForSingleValueEvent(new ValueEventListener(){
                @Override
                public void onDataChange(@NotNull DataSnapshot dataSnapshot) {
                    long numLikes = 0;
                    if(dataSnapshot.exists()){
                        numLikes = dataSnapshot.getValue(Long.class);
                    }
                    boolean isLiked = shineButton.isSelected();

                    Log.d(TAG, "Liked onDataChange: "+isLiked);
                    if(isLiked){
                        //If already liked then user wants to unlike the post

                        likesRef.setValue(numLikes-1);

                    }else {
                        //If not liked already then user wants to like the post
                        likesRef.setValue(numLikes+1);
                        shineButton.setEnabled(false);
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

            ShineButton like  =  layout.findViewById(R.id.like_feed);
            System.out.println(RED);
            like.setBackground(getResources().getDrawable(R.drawable.like_feed_bg));

        }

代码修改后:现在无论我们喜欢还是不喜欢,计数都不会增加或减少。

Onlike 方法:

            public void onLike(View v, int position) {

            UploadImage selectedItem = mUploads.get(position);
            String selectedKey = selectedItem.getKey();
            final ShineButton shineButton = new ShineButton(Objects.requireNonNull(getActivity()));
            shineButton.setBtnFillColor(RED);
            shineButton.setShapeResource(R.raw.heart);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100,100);
            shineButton.setLayoutParams(layoutParams);

            final DatabaseReference likesRef = FirebaseDatabase.getInstance().getReference().child("ImageUploads").child(selectedKey).child("likes");
            likesRef.addListenerForSingleValueEvent(new ValueEventListener(){
                @Override
                public void onDataChange(@NotNull final DataSnapshot dataSnapshot) {
                    long numLikes = 0;

                    if(dataSnapshot.exists()){
                        numLikes = dataSnapshot.getValue(Long.class);
                    }

                    final long finalNumLikes = numLikes;

                    shineButton.setOnCheckStateChangeListener(new ShineButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(View view, boolean checked) {


                            if (checked){
                                likesRef.setValue(finalNumLikes-1);
                            }
                            else{
                                likesRef.setValue(finalNumLikes+1);
                            }
                        }
                    });

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

            ShineButton like  =  layout.findViewById(R.id.like_feed);
            System.out.println(RED);
            like.setBackground(getResources().getDrawable(R.drawable.like_feed_bg));

        }

问题是您没有检查按下按钮的状态。您可以通过执行以下操作来实现此目的:

替换:

if(isLiked){ 
    //If already liked then user wants to unlike the post 
    likesRef.setValue(numLikes-1); }
else { 
    //If not liked already then user wants to like the post 
    likesRef.setValue(numLikes+1);
    shineButton.setEnabled(false); 
}

有了这个:

shineButton.setOnCheckStateChangeListener(new ShineButton.OnCheckedChangeListener() { 
@Override 
    public void onCheckedChanged(View view, boolean checked) { 
        if (checked){
            likesRef.setValue(numLikes+1);
        } else {
            likesRef.setValue(numLikes-1);
        }
    } 
});

您还必须在关闭应用程序时保留按钮的参考。您可以使用 SharedPreferences.

执行此操作