嘿伙计们试图建立一个使用Firebase实时数据库的社交媒体应用程序
但是我很困惑如何让posts generated key
在其中添加评论,以便如何将推送密钥生成到帖子以及如何在Clicked Post中显示评论
数据库结构
这是显示帖子详细信息的活动当我点击Recyclerview中的帖子时,它会让我参与此活动
public class ShowThePost extends AppCompatActivity {
Dialog dialog;
FirebaseUser mAuth;
DatabaseReference dbRef;
Intent intent;
@BindView(R.id.imgPostShow) PhotoView PostImage;
@BindView(R.id.Show_profile_image) CircularImageView UserProfilePicture;
@BindView(R.id.postTitleShow) TextView PostTitle;
@BindView(R.id.txt_Show_UserName) TextView UserProfileName;
@BindView(R.id.txt_Post_Date) TextView PostDateTime;
@BindView(R.id.Show_price) TextView RealPrice;
@BindView(R.id.txtShowDescription) TextView PostDescription;
@BindView(R.id.fabComment) com.getbase.floatingactionbutton.FloatingActionButton floatingActionsMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_the_post);
ButterKnife.bind(this);
dialog = new Dialog(this);
intent = getIntent();
getDataFromIntent();
}
public void getDataFromIntent(){
String id = intent.getStringExtra("post_ID");
String title = intent.getStringExtra("post_Title");
String description = intent.getStringExtra("post_Desc");
String image = intent.getStringExtra("post_Image");
String dateTime = intent.getStringExtra("post_DateTime");
String price = intent.getStringExtra("post_Price");
String currency = intent.getStringExtra("post_Currency");
String UserName = intent.getStringExtra("post_UserName");
String UserProfilePic = intent.getStringExtra("post_UserProfilePicture");
GlideApp.with(this)
.load(image)
.into(PostImage);
GlideApp.with(this)
.load(UserProfilePic)
.into(UserProfilePicture);
PostTitle.setText(title);
UserProfileName.setText(UserName);
PostDescription.setText(description);
RealPrice.setText(price + " : " + currency);
PostDateTime.setText(dateTime);
}
public void ShowCommentDialog() {
// final AlertDialog.Builder alert = new AlertDialog.Builder(this);
// View view = getLayoutInflater().inflate(R.layout.create_comment,null);
// alert.setCancelable(false);
dialog.setContentView(R.layout.create_comment);
final EditText txtSuggestprice = (EditText) dialog.findViewById(R.id.priceSuggested);
final EditText txtNotes = (EditText)dialog.findViewById(R.id.txtnotes);
final Button btnDone = (Button)dialog.findViewById(R.id.btnCommentDone);
final Button btnClose = (Button) dialog.findViewById(R.id.btnClose);
btnDone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String category = intent.getStringExtra("category");
// String Notes = txtNotes.getText().toString();
// String SuggestedPrice =
//
txtSuggestprice.getText().toString();
// String UserID = mAuth.getUid().toString();
// String UserProfilePic = mAuth.getPhotoUrl().toString();
// String UserName = mAuth.getDisplayName().toString();
//
// CommentsList commentsList = new CommentsList();
// commentsList.setUserID(UserID);
// commentsList.setUserProfilePicture(UserProfilePic);
// commentsList.setCommentDescription(Notes);
// commentsList.setCommentSuggestPrice(SuggestedPrice);
// commentsList.setCommentDate("date");
//
dbRef = FirebaseDatabase.getInstance().getReference("Posts");
Toast.makeText(ShowThePost.this, dbRef.child(category).push().getKey().toString(), Toast.LENGTH_SHORT).show();
}
});
btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
Toasty.info(ShowThePost.this, "Canceled", Toast.LENGTH_SHORT).show();
}
});
dialog.setCancelable(false);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
@OnClick(R.id.fabComment)
public void OpenDialog(){
ShowCommentDialog();
}
}
这是用于将数据发送到Recyclerview并获取putExtra
的适配器
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);
}
}
}
Comments.class列表
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;
}
}
答案 0 :(得分:0)
你可以试试这个
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();
}
});