I am new to android development, I used Two Viewholder in My adapter ONE FOR PHOTO (PHOTOVIEWHOLDER) ,ANOTHER FOR VIDEO (VideoVIEWHOLDER)
MY Problem is I have a function I HAVE PASSED ONE VIEWHOLDER (Photoviewholder) I need to use this same function to my (VideoViewholder) . Instead of repeating the same function twice , I thought is there is other anyway I can do this
private void addNewlike(final PhotoHolder holder){
Log.d(TAG, "addNewlike: adding new like ");
String newLikeID = mReference.push().getKey();
Likes likes = new Likes();
likes.setUser_id(FirebaseAuth.getInstance().getCurrentUser().getUid());
mReference.child(mContext.getString(R.string.dbname_photos))
.child(holder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(newLikeID)
.setValue(likes);
mReference.child(mContext.getString(R.string.dbname_user_photos))
.child(holder.photo.getUser_id())
.child(holder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(newLikeID)
.setValue(likes);
holder.heart.toggleLike();
}
private void addNewlike(final VideoViewHolder holder){
Log.d(TAG, "addNewlike: adding new like ");
String newLikeID = mReference.push().getKey();
Likes likes = new Likes();
likes.setUser_id(FirebaseAuth.getInstance().getCurrentUser().getUid());
mReference.child(mContext.getString(R.string.dbname_photos))
.child(holder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(newLikeID)
.setValue(likes);
mReference.child(mContext.getString(R.string.dbname_user_photos))
.child(holder.photo.getUser_id())
.child(holder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(newLikeID)
.setValue(likes);
holder.heart.toggleLike();
}
public class GestureListener extends GestureDetector.SimpleOnGestureListener{
PhotoHolder mHolder;
public GestureListener(PhotoHolder holder) {
mHolder = holder;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.d(TAG, "onDoubleTap: double tap detected.");
Log.d(TAG, "onDoubleTap: clicked on photo: " + mHolder.photo.getPhoto_id());
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference
.child(mContext.getString(R.string.dbname_photos))
.child(mHolder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes));
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
String keyID = singleSnapshot.getKey();
//case1: Then user already liked the photo
if(mHolder.likephotobycurrentUser
&& singleSnapshot.getValue(Likes.class).getUser_id()
.equals(FirebaseAuth.getInstance().getCurrentUser().getUid())
){
mReference.child(mContext.getString(R.string.dbname_photos))
.child(mHolder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(keyID)
.removeValue();
/// mReference.child(mContext.getString(R.string.dbname_user_photos))
// .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.child(mHolder.photo.getUser_id())
.child(mHolder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(keyID)
.removeValue();
mHolder.heart.toggleLike();
getLikesString(mHolder);
}
//case2: The user has not liked the photo
else if(!mHolder.likephotobycurrentUser){
//add new like
addNewlike(mHolder);
break;
}
}
if(!dataSnapshot.exists()){
//add new like
addNewlike(mHolder);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return true;
}
}