2017-03-13 79 views
0

我在這裏很困惑,真的到了最後,我在我的項目中設置了一個類似的功能。如果用戶按下同樣的按鈕一次,類似計數器從0更新爲1(喜歡)等imageButton(更改顏色)更新成功。如果按兩次計數器從1更新到0(不同)成功。Firebase like counter

問題是,當一個不同的用戶也按類似的按鈕喜歡相同的職位,類似的計數器不會更新從1到2.請幫助。我希望這很清楚。以下是代碼。

viewHolder.mLikebtn.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 

     mProcessLike = true; 
     mDatabaseLikeCount = FirebaseDatabase.getInstance().getReference().child("Notes").child(post_key).child("likecount"); 

     mDatabaseLikeCount.keepSynced(true); 
     mDatabaseLike.addValueEventListener(new ValueEventListener() { 

      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       if (mProcessLike) { 
        if (dataSnapshot.child(post_key).hasChild(auth.getCurrentUser().getUid())) { 
         Log.i("D Diary", "User has already Liked. So it can be considered as Unliked."); 
         mDatabaseLike.child(post_key).child(auth.getCurrentUser().getUid()).removeValue(); 
         mDatabaseLikeCount.setValue(likeCount = likeCount - 1); 
         mProcessLike = false; 
        } else { 
         Log.i("D Diary", "User Liked"); 
         mDatabaseLike.child(post_key).child(auth.getCurrentUser().getUid()).setValue(auth.getCurrentUser().getDisplayName()); 
         mDatabaseLikeCount.setValue(likeCount = likeCount + 1); 
         Log.i(dataSnapshot.getKey(), dataSnapshot.getChildrenCount() + "Count"); 
         mProcessLike = false; 
        } 
       } 
      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 
    } 
}); 
+0

你能告訴你如何定義likeCount變量? – GAlexMES

+0

在POJO它初始化爲0 –

回答

1

這應該起作用。您應該使用事務來增加或減少多個人將與之交互的值。

if (dataSnapshot.child(post_key).hasChild(auth.getCurrentUser().getUid())) { 
    Log.i("D Diary", "User has already Liked. So it can be considered as Unliked."); 
    mDatabaseLike.child(post_key).child(auth.getCurrentUser().getUid()).removeValue(); 
    updateCounter(false); 
    mProcessLike = false; 
} else { 
    Log.i("D Diary", "User Liked"); 
    mDatabaseLike.child(post_key).child(auth.getCurrentUser().getUid()).setValue(auth.getCurrentUser().getDisplayName()); 
    updateCounter(true) 
    Log.i(dataSnapshot.getKey(), dataSnapshot.getChildrenCount() + "Count"); 
    mProcessLike = false; 
} 

隨着updateCounter:

private void updateCounter(bool increment) { 
    mDatabaseLikeCount.runTransaction(new Transaction.Handler() { 
     @Override 
     public Transaction.Result doTransaction(MutableData mutableData) { 
      if (mutableData.getValue() != null) { 
       int value = mutableData.getValue(Integer.class); 
       if(increment) { 
        value++; 
       } else { 
        value--; 
       } 
       mutableData.setValue(value); 
      } 
      return Transaction.success(mutableData); 
     } 

     @Override 
     public void onComplete(DatabaseError databaseError, boolean b, 
           DataSnapshot dataSnapshot) { 
      // Transaction completed 
      Log.d(TAG, "likeTransaction:onComplete:" + databaseError); 
     } 
    }); 
} 

Firebase Transactions

+0

你能幫我把我的更新計數器放在哪裏嗎? –

+0

就在您發佈的代碼所在的同一班級的任何地方。 –

+0

你有沒有得到它的工作? –