我正在尝试从我的 Android 应用程序的实时数据库中获取
postid
。
但是,我无法这样做,并且在我的 isLiked()
函数中不断遇到错误:
Can't pass null for argument 'pathString' in child()
我的代码:
package com.example.lively.Adapter;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.lively.CommentActivity;
import com.example.lively.Model.Post;
import com.example.lively.Model.User;
import com.example.lively.PostDetailFragment;
import com.example.lively.ProfileFragment;
import com.example.lively.R;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.List;
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {
public Context mContext;
public List<Post> mPost;
private FirebaseUser firebaseUser;
public PostAdapter(Context mContext, List<Post> mPost) {
this.mContext = mContext;
this.mPost = mPost;
}
@NonNull
@Override
public PostAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.post_items, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull PostAdapter.ViewHolder holder, int position) {
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
Post post = mPost.get(position);
Glide.with(mContext).load(post.getPostImage()).into(holder.postImage);
if (post.getDescription().equals("")) {
holder.description.setVisibility(View.GONE);
} else {
holder.description.setVisibility(View.VISIBLE);
holder.description.setText(post.getDescription());
}
publisherInfo(holder.profileImage, holder.username, holder.publisher, post.getPublisher());
if(post.getPostId()!=null)
isLiked(post.getPostId(), holder.like);
nrLikes(holder.likes, post.getPostId());
getComments(post.getPostId(), holder.comments);
isSaved(post.getPostId(), holder.save);
holder.profileImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = mContext.getSharedPreferences("PEEFS", Context.MODE_PRIVATE).edit();
editor.putString("profileid", post.getPublisher());
((FragmentActivity)mContext).getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, new ProfileFragment()).commit();
}
});
holder.username.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = mContext.getSharedPreferences("PEEFS", Context.MODE_PRIVATE).edit();
editor.putString("profileid", post.getPublisher());
((FragmentActivity)mContext).getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, new ProfileFragment()).commit();
}
});
holder.publisher.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = mContext.getSharedPreferences("PEEFS", Context.MODE_PRIVATE).edit();
editor.putString("profileid", post.getPublisher());
((FragmentActivity)mContext).getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, new ProfileFragment()).commit();
}
});
holder.postImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = mContext.getSharedPreferences("PEEFS", Context.MODE_PRIVATE).edit();
editor.putString("postid", post.getPostId());
((FragmentActivity)mContext).getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, new PostDetailFragment()).commit();
}
});
holder.save.setOnClickListener(v -> {
if (holder.like.getTag().equals("save")) {
FirebaseDatabase.getInstance().getReference().child("Saves").child(firebaseUser.getUid())
.child(post.getPostId()).setValue(true);
} else {
FirebaseDatabase.getInstance().getReference().child("Saves").child(firebaseUser.getUid())
.child(post.getPostId()).removeValue();
}
});
holder.like.setOnClickListener(v -> {
if (holder.like.getTag().equals("like")) {
FirebaseDatabase.getInstance().getReference().child("Likes").child(post.getPostId())
.child(firebaseUser.getUid()).setValue(true);
} else {
FirebaseDatabase.getInstance().getReference().child("Likes").child(post.getPostId())
.child(firebaseUser.getUid()).removeValue();
}
});
holder.comment.setOnClickListener(v -> {
Intent intent = new Intent(mContext, CommentActivity.class);
intent.putExtra("postid", post.getPostId());
intent.putExtra("publisherid", post.getPublisher());
mContext.startActivity(intent);
});
holder.comments.setOnClickListener(v -> {
Intent intent = new Intent(mContext, CommentActivity.class);
intent.putExtra("postid", post.getPostId());
intent.putExtra("publisherid", post.getPublisher());
mContext.startActivity(intent);
});
}
@Override
public int getItemCount() {
return mPost.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView profileImage, postImage, like, comment, save;
public TextView username, likes, publisher, description, comments;
public ViewHolder(@NonNull View itemView) {
super(itemView);
profileImage = itemView.findViewById(R.id.imageProfile);
postImage = itemView.findViewById(R.id.postImage);
like = itemView.findViewById(R.id.like);
comment = itemView.findViewById(R.id.comment);
save = itemView.findViewById(R.id.save);
username = itemView.findViewById(R.id.username);
likes = itemView.findViewById(R.id.likes);
publisher = itemView.findViewById(R.id.publisher);
description = itemView.findViewById(R.id.description);
comments = itemView.findViewById(R.id.comments);
}
}
private void getComments(String postid, TextView comments) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Comments").child(postid);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
comments.setText("View all " + snapshot.getChildrenCount() + " comments");
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void isLiked(String postid, final ImageView imageView) {
final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if (firebaseUser != null ) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference()
.child("Likes")
.child(postid);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.child(firebaseUser.getUid()).exists()) {
imageView.setImageResource(R.drawable.baseline_thumb_up_alt_24);
imageView.setTag("liked");
} else {
imageView.setImageResource(R.drawable.baseline_thumb_up_off_alt_24);
imageView.setTag("like");
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
private void nrLikes(final TextView likes, String postid) {
DatabaseReference reference1 = FirebaseDatabase.getInstance().getReference().child("Likes")
.child(postid);
reference1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
likes.setText(snapshot.getChildrenCount() + "likes");
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void publisherInfo(ImageView imageProfile, TextView username, TextView publisher, String userid) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users").child(userid);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
User user = snapshot.getValue(User.class);
Glide.with(mContext).load(user.getImageurl()).into(imageProfile);
username.setText(user.getUsername());
publisher.setText(user.getUsername());
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void isSaved(final String postid, ImageView imageView) {
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Saves")
.child(firebaseUser.getUid());
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.child(postid).exists()) {
imageView.setImageResource(R.drawable.baseline_bookmark_24);
imageView.setTag("saved");
} else {
imageView.setImageResource(R.drawable.round_bookmark_border_24);
imageView.setTag("save");
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
实时数据库如下所示:
我尝试用不同的 if-else 条件来绑定孩子,但它不起作用。正如我们从上面的代码中看到的,我试图在进入 isLiked 函数之前进行检查。尽管如此,我还是没有看到任何进展。
正如 @TamasCsizmadia 最初在您的代码中发现的那样,当您使用时:
if (post.getPostId()!=null);
// 👆
行尾有分号,表示这行代码后面的所有内容都不属于 if 语句范围。
根据之前的评论,您说您已经删除了行尾的分号,但您得到了相同的错误。查看您的代码可以确认该行为。为什么?这是因为您没有使用大括号
{}
,这基本上意味着只有下一行被评估为 if 语句的一部分:
if(post.getPostId()!=null)
isLiked(post.getPostId(), holder.like);
//👆
nrLikes(holder.likes, post.getPostId());
getComments(post.getPostId(), holder.comments);
isSaved(post.getPostId(), holder.save);
IDE 中的缩进也表明了这一点。要解决此问题,请使用如下所示的花括号:
if(post.getPostId()!=null) {
isLiked(post.getPostId(), holder.like);
nrLikes(holder.likes, post.getPostId());
getComments(post.getPostId(), holder.comments);
isSaved(post.getPostId(), holder.save);
}
现在所有四行代码都是 if 语句范围的一部分。