我在创建一个社交网络的过程中,遇到了一个问题,即如何在回收中显示一个用户的帖子,比如在“我的帖子”活动中,帖子本身已经在firebase数据库中。
向firebase数据库添加信息
StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("postImage");
StorageReference imageFilePath = storageReference.child(postImageUri.getLastPathSegment());
imageFilePath.putFile(postImageUri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
imageFilePath.getDownloadUrl()
.addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String imageFilePath = uri.toString();
Post post = new Post(inputTitle.getText().toString(),
currentUser.getPhotoUrl().toString(),
imageFilePath,currentUser.getUid(),
currentUser.getDisplayName().toString(),inputLocation.getText().toString(),
inputReasons.getText().toString(),inputDescription.getText().toString());
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance("https://enigma-41323-default-rtdb.europe-west1.firebasedatabase.app/");
DatabaseReference databaseReference = firebaseDatabase.getReference("Posts").push();
String key = databaseReference.getKey();
post.setRowPostKey(key);
databaseReference.setValue(post)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
getParentFragmentManager().beginTransaction().setCustomAnimations(R.anim.anim_slide_out_left,R.anim.anim_slide_in_left).replace(R.id.homeCont,HomeFragment.class,null).commit();
}
});
}
});
}
});
回收者信息视图
@Override
public void onStart() {
super.onStart();
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference("Posts");
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {
postList = new ArrayList<>();
for (DataSnapshot postSnap:snapshot.getChildren()) {
Post post = postSnap.getValue(Post.class);
postList.add(post);
adapter = new PostAdapter(getContext(),postList);
PostRV.setAdapter(adapter);
PostRV.setHasFixedSize(true);
PostRV.setLayoutManager(new LinearLayoutManager(getContext()));
adapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(@NonNull @NotNull DatabaseError error) {
}
});
}
暂无答案!
目前还没有任何答案,快来回答吧!