Android Can';t从Firebase获取Post生成的密钥以在子级中添加注释
嘿,伙计们,我正在尝试使用Firebase实时数据库构建一个社交媒体应用程序 但是我陷入了如何获得Android Can';t从Firebase获取Post生成的密钥以在子级中添加注释,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,嘿,伙计们,我正在尝试使用Firebase实时数据库构建一个社交媒体应用程序 但是我陷入了如何获得posts-generated-key以在其中添加评论的困境,因此如何获得生成到post的push-key以及如何在单击的post中显示评论 数据库结构 以下是当我在Recyclerview中单击帖子时显示帖子详细信息的活动,它让我参与此活动 这是用于将数据发送到Recyclerview并获取putExtra的适配器 你可以试试这个 mDatabase.child("Posts").child
posts-generated-key
以在其中添加评论的困境,因此如何获得生成到post的push-key以及如何在单击的post中显示评论
数据库结构
以下是当我在Recyclerview中单击帖子时显示帖子详细信息的活动,它让我参与此活动
这是用于将数据发送到Recyclerview并获取putExtra的适配器
你可以试试这个
mDatabase.child("Posts").child("Books").addValueEventListener(new ValueEventListener() {
final ArrayList<String> keysArray= new ArrayList<String>(); // if you want to store all the keys
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()){
String key = data.getKey(); //Here you can get all your keys from Books
keysArray.add(key);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(view.getContext(),"database error",
Toast.LENGTH_SHORT).show();
}
});
mDatabase.child(“Posts”).child(“Books”).addValueEventListener(新的ValueEventListener()){
final ArrayList keysArray=new ArrayList();//如果要存储所有键
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
对于(DataSnapshot数据:DataSnapshot.getChildren()){
String key=data.getKey();//在这里,您可以从书本中获取所有密钥
keysArray.add(key);
}
}
@凌驾
已取消的公共void(DatabaseError DatabaseError){
Toast.makeText(view.getContext(),“数据库错误”,
吐司。长度(短)。show();
}
});
我希望在BooksAdapter中的post\u Id中存储生成的post键,这样当我在recycler视图中单击post时,我会发送带有Intent.putExtra的Id
public class BooksAdapter extends RecyclerView.Adapter<BooksAdapter.BooksItemsHolder> {
public BooksAdapter(List<BooksLists> listy, Context mContext) {
this.listy = listy;
this.mContext = mContext;
}
public List<BooksLists> listy ;
Context mContext;
@NonNull
@Override
public BooksItemsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item,parent,false);
BooksItemsHolder holder = new BooksItemsHolder(v);
return holder;
}
@Override
public void onBindViewHolder(@NonNull final BooksItemsHolder holder, int position) {
final BooksLists list = listy.get(position);
holder.PostTitle.setText(list.getPost_title());
holder.PostDate.setText(list.getPost_datetime());
GlideApp.with(mContext)
.load(list.getPost_image())
.into(holder.imgPostCover);
holder.UserProfileName.setText(list.getUser_name());
GlideApp.with(mContext)
.load(list.getUser_pp())
.placeholder(R.drawable.user)
.into(holder.TheUserProfilePic);
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext,ShowThePost.class);
intent.putExtra("post_ID",list.getPost_id());
intent.putExtra("post_Title",list.getPost_title());
intent.putExtra("post_Desc",list.getPost_description());
intent.putExtra("post_Image",list.getPost_image());
intent.putExtra("post_DateTime",list.getPost_datetime());
intent.putExtra("post_Price",list.getPost_real_price());
intent.putExtra("post_Currency",list.getCurrency());
intent.putExtra("post_UserName",list.getUser_name());
intent.putExtra("post_UserProfilePicture",list.getUser_pp());
intent.putExtra("category",list.getCategory());
mContext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return listy.size();
}
public class BooksItemsHolder extends RecyclerView.ViewHolder {
@BindView(R.id.TheuserProfilePicture)
CircularImageView TheUserProfilePic;
@BindView(R.id.userName) TextView UserProfileName;
@BindView(R.id.imgPostShow) ImageView imgPostCover;
@BindView(R.id.postTitle) TextView PostTitle;
@BindView(R.id.txt_Post_Date) TextView PostDate;
@BindView(R.id.GotoPostItem)
CardView linearLayout;
public BooksItemsHolder(View itemView) {
super(itemView);
ButterKnife.bind(this,itemView);
}
}
}
public class CommentsList {
public CommentsList() {
}
String userID;
String userName;
String userProfilePicture;
String commentDate;
String commentSuggestPrice;
String CommentDescription;
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserProfilePicture() {
return userProfilePicture;
}
public void setUserProfilePicture(String userProfilePicture) {
this.userProfilePicture = userProfilePicture;
}
public String getCommentDate() {
return commentDate;
}
public void setCommentDate(String commentDate) {
this.commentDate = commentDate;
}
public String getCommentSuggestPrice() {
return commentSuggestPrice;
}
public void setCommentSuggestPrice(String commentSuggestPrice) {
this.commentSuggestPrice = commentSuggestPrice;
}
public String getCommentDescription() {
return CommentDescription;
}
public void setCommentDescription(String commentDescription) {
CommentDescription = commentDescription;
}
public CommentsList(String userID, String userName, String userProfilePicture, String commentDate, String commentSuggestPrice, String commentDescription) {
this.userID = userID;
this.userName = userName;
this.userProfilePicture = userProfilePicture;
this.commentDate = commentDate;
this.commentSuggestPrice = commentSuggestPrice;
CommentDescription = commentDescription;
}
}
mDatabase.child("Posts").child("Books").addValueEventListener(new ValueEventListener() {
final ArrayList<String> keysArray= new ArrayList<String>(); // if you want to store all the keys
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()){
String key = data.getKey(); //Here you can get all your keys from Books
keysArray.add(key);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(view.getContext(),"database error",
Toast.LENGTH_SHORT).show();
}
});