【发布时间】:2021-11-03 11:29:42
【问题描述】:
我实际上是 Android 编程新手,但我想使用随机键从 Firebase 数据库中检索一个子项(评论)到 RecyclerView。我已经编写了一些代码,但是当我启动应用程序时,孩子(评论)没有显示它也没有给我任何错误。请帮忙,提前谢谢。
我的 Java 代码:
private String commentsID = "";
private ImageView imageView, share, commentimage, profileimage, newslikeimage, send;
private TextView heading, description, newsdate, newstime, newscounter, sharecounter, newcommentcounter, profilename;
private String commentsend, commentcurrentusers, newcommentcurrentusers;
private EditText writecomment;
private DatabaseReference commentRef;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView recyclerView;
private String saveCurrentDate, saveCurrentTime, commentRandomKey;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comment);
commentRef = FirebaseDatabase.getInstance().getReference().child("Comments");
recyclerView = findViewById(R.id.commentrecycler);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setLayoutManager(layoutManager);
commentsID = getIntent().getStringExtra("comments");
imageView = findViewById(R.id. newsimage);
heading = findViewById(R.id. newsname);
description = findViewById(R.id. newsdescription);
newsdate = findViewById(R.id. newdate);
newstime = findViewById(R.id. newtime);
newscounter = findViewById(R.id. newcounter);
sharecounter = findViewById(R.id. sharecounter);
commentimage = findViewById(R.id. commentimage);
newcommentcounter = findViewById(R.id. newcommentcounter);
newslikeimage = findViewById(R.id. newslike);
share = findViewById(R.id. share);
profileimage = findViewById(R.id. profileimage);
profilename = findViewById(R.id. profilename);
send = findViewById(R.id. send);
writecomment = findViewById(R.id. sendcomment);
}
@Override
protected void onStart() {
super.onStart();
FirebaseUser currentuser = FirebaseAuth.getInstance().getCurrentUser();
newcommentcurrentusers = currentuser.getUid();
getCommentsDetails(commentsID);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
commentsend = writecomment.getText().toString();
if (TextUtils.isEmpty(commentsend))
{
Toast.makeText(CommentActivity.this, "Please type your comment", Toast.LENGTH_SHORT).show();
}
else
{
sendingCommentsToFirebase();
}
}
});
FirebaseRecyclerOptions<Comment> options =
new FirebaseRecyclerOptions.Builder<Comment>()
.setQuery(commentRef, Comment.class)
.build();
FirebaseRecyclerAdapter<Comment, CommentsViewHolder> adapter =
new FirebaseRecyclerAdapter<Comment, CommentsViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull CommentsViewHolder holder, int position, @NonNull Comment model) {
holder.comment.setText(model.getComments());
}
@NonNull
@Override
public CommentsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.comment_item_layout, parent, false);
CommentsViewHolder holder = new CommentsViewHolder(view);
return holder;
}
};
recyclerView.setAdapter(adapter);
adapter.startListening();
}
private void sendingCommentsToFirebase()
{
FirebaseUser currentuser = FirebaseAuth.getInstance().getCurrentUser();
commentcurrentusers = currentuser.getUid();
Calendar calendar = Calendar.getInstance();
SimpleDateFormat currentDate = new SimpleDateFormat("MMM dd, yyyy");
saveCurrentDate = currentDate.format(calendar.getTime());
SimpleDateFormat currentTime = new SimpleDateFormat("HH:mm:ss a");
saveCurrentTime = currentTime.format(calendar.getTime());
commentRandomKey = saveCurrentDate + saveCurrentTime;
HashMap<String, Object> commentMap = new HashMap<>();
commentMap.put("comments", commentsend);
commentMap.put("userID", commentcurrentusers);
commentMap.put("newspid", commentRandomKey);
commentRef.child(commentsID).push().setValue(commentMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task)
{
if (task.isSuccessful())
{
Toast.makeText(CommentActivity.this, "Comments added", Toast.LENGTH_SHORT).show();
}
}
});
}
private void getCommentsDetails(String commentsID) {
DatabaseReference newsRef = FirebaseDatabase.getInstance().getReference().child("News");
newsRef.child(commentsID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
News news = snapshot.getValue(News.class);
heading.setText(news.getHeading());
description.setText(news.getDecription());
profilename.setText(news.getUsername());
newsdate.setText(news.getDate());
newstime.setText(news.getTime());
newscounter.setText(news.getLikes());
sharecounter.setText(news.getShare());
newcommentcounter.setText(news.getComment());
Picasso.get().load(news.getProfileimage()).into(profileimage);
Picasso.get().load(news.getImage()).into(imageView);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
这是我的视图持有者
public class CommentActivity extends AppCompatActivity
{
private String commentsID = "";
private ImageView imageView, share, commentimage, profileimage, newslikeimage, send;
private TextView heading, description, newsdate, newstime, newscounter, sharecounter, newcommentcounter, profilename;
private String commentsend, commentcurrentusers, newcommentcurrentusers;
private EditText writecomment;
private DatabaseReference commentRef;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView recyclerView;
private String saveCurrentDate, saveCurrentTime, commentRandomKey;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comment);
commentRef = FirebaseDatabase.getInstance().getReference().child("Comments");
recyclerView = findViewById(R.id.commentrecycler);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setLayoutManager(layoutManager);
commentsID = getIntent().getStringExtra("comments");
imageView = findViewById(R.id. newsimage);
heading = findViewById(R.id. newsname);
description = findViewById(R.id. newsdescription);
newsdate = findViewById(R.id. newdate);
newstime = findViewById(R.id. newtime);
newscounter = findViewById(R.id. newcounter);
sharecounter = findViewById(R.id. sharecounter);
commentimage = findViewById(R.id. commentimage);
newcommentcounter = findViewById(R.id. newcommentcounter);
newslikeimage = findViewById(R.id. newslike);
share = findViewById(R.id. share);
profileimage = findViewById(R.id. profileimage);
profilename = findViewById(R.id. profilename);
send = findViewById(R.id. send);
writecomment = findViewById(R.id. sendcomment);
}
@Override
protected void onStart() {
super.onStart();
FirebaseUser currentuser = FirebaseAuth.getInstance().getCurrentUser();
newcommentcurrentusers = currentuser.getUid();
getCommentsDetails(commentsID);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
commentsend = writecomment.getText().toString();
if (TextUtils.isEmpty(commentsend))
{
Toast.makeText(CommentActivity.this, "Please type your comment", Toast.LENGTH_SHORT).show();
}
else
{
sendingCommentsToFirebase();
}
}
});
FirebaseRecyclerOptions<Comment> options =
new FirebaseRecyclerOptions.Builder<Comment>()
.setQuery(commentRef, Comment.class)
.build();
FirebaseRecyclerAdapter<Comment, CommentsViewHolder> adapter =
new FirebaseRecyclerAdapter<Comment, CommentsViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull CommentsViewHolder holder, int position, @NonNull Comment model) {
holder.comment.setText(model.getComments());
}
@NonNull
@Override
public CommentsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.comment_item_layout, parent, false);
CommentsViewHolder holder = new CommentsViewHolder(view);
return holder;
}
};
recyclerView.setAdapter(adapter);
adapter.startListening();
}
private void sendingCommentsToFirebase()
{
FirebaseUser currentuser = FirebaseAuth.getInstance().getCurrentUser();
commentcurrentusers = currentuser.getUid();
Calendar calendar = Calendar.getInstance();
SimpleDateFormat currentDate = new SimpleDateFormat("MMM dd, yyyy");
saveCurrentDate = currentDate.format(calendar.getTime());
SimpleDateFormat currentTime = new SimpleDateFormat("HH:mm:ss a");
saveCurrentTime = currentTime.format(calendar.getTime());
commentRandomKey = saveCurrentDate + saveCurrentTime;
HashMap<String, Object> commentMap = new HashMap<>();
commentMap.put("comments", commentsend);
commentMap.put("userID", commentcurrentusers);
commentMap.put("newspid", commentRandomKey);
commentRef.child(commentsID).push().setValue(commentMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task)
{
if (task.isSuccessful())
{
Toast.makeText(CommentActivity.this, "Comments added", Toast.LENGTH_SHORT).show();
}
}
});
}
private void getCommentsDetails(String commentsID) {
DatabaseReference newsRef = FirebaseDatabase.getInstance().getReference().child("News");
newsRef.child(commentsID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
News news = snapshot.getValue(News.class);
heading.setText(news.getHeading());
description.setText(news.getDecription());
profilename.setText(news.getUsername());
newsdate.setText(news.getDate());
newstime.setText(news.getTime());
newscounter.setText(news.getLikes());
sharecounter.setText(news.getShare());
newcommentcounter.setText(news.getComment());
Picasso.get().load(news.getProfileimage()).into(profileimage);
Picasso.get().load(news.getImage()).into(imageView);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
这是我的班级:
public class Comment
{
private String comments, newspid, userID;
private Comment()
{
}
public Comment(String comments, String newspid, String userID) {
this.comments = comments;
this.newspid = newspid;
this.userID = userID;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
public String getNewspid() {
return newspid;
}
public void setNewspid(String newspid) {
this.newspid = newspid;
}
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
}
【问题讨论】:
-
如果您遇到问题,最好在发布问题时创建MCVE。您为此问题发布了近 400(四百)行行代码。人们需要解析和尝试在线调试的内容很多。请编辑您的问题并隔离问题,这样可以增加获得帮助的机会。
-
好的,谢谢您的建议
-
请修剪您的代码,以便更容易找到您的问题。请按照以下指南创建minimal reproducible example。
标签: java android firebase-realtime-database firebaseui