我正在尝试将数据库中的数据读取到recyclerview列表中。
项目数已正确检索,因此我的数据库引用工作正常,但数据为空。
这是我用来检索数据并将其绑定到我的模型类的代码-
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
getSupportActionBar().hide();
FirebaseApp.initializeApp(this);
mButtonSend = findViewById(R.id.button_send);
mEditTextFileName = findViewById(R.id.comment_field);
mRecyclerView = findViewById(R.id.recycler_view_too);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mcUploads = new ArrayList<>();
mStorage = FirebaseStorage.getInstance();
mDatabaseRef =
FirebaseDatabase.getInstance().getReference("Comments");
mAdapter = new RecyclerViewAdapterComment(PostActivity.this,
mcUploads);
mRecyclerView.setAdapter(mAdapter);
mDatabaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Comments upload = postSnapshot.getValue(Comments.class);
mcUploads.add(upload);
Log.e("ccmain", "This is my message" + mcUploads);
}
mAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(PostActivity.this, databaseError.getMessage(),
Toast.LENGTH_SHORT).show();
}
});
mButtonSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mUploadTask != null && mUploadTask.isInProgress()) {
Toast.makeText(PostActivity.this, "Comment is being sent",
Toast.LENGTH_SHORT).show();
} else {
uploadComment();
}
}
});
}
private void uploadComment() {
if (mEditTextFileName == null ) {
Toast.makeText(this, "Type something", Toast.LENGTH_LONG).show();
} else {
Comments upload;
//call the constructor from the Posts class in "models" package
upload = new Comments(mEditTextFileName.getText().toString().trim());
String uploadId = mDatabaseRef.push().getKey();
mDatabaseRef.child(uploadId).setValue(upload);
}
}
}
这是我的RecyclerViewAdapter类-
public class RecyclerViewAdapterComment extends RecyclerView.Adapter<RecyclerViewAdapterComment.ImageViewHolderComment> {
private Context mContext;
private List<Comments> mcUploads;
public RecyclerViewAdapterComment(Context context, List<Comments> uploads) {
mContext = context;
mcUploads = uploads;
}
@Override
public ImageViewHolderComment onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.comment_single_item, parent, false);
return new ImageViewHolderComment(v);
}
@Override
public void onBindViewHolder(ImageViewHolderComment holder, int position) {
Comments uploadCurrent = mcUploads.get(position);
holder.commentText.setText(uploadCurrent.getComment());
Picasso.get().load(uploadCurrent.getProfile_p_url()).fit().centerCrop().into(holder.commentPhoto);
//LOGS
Log.e("rcc","::"+mcUploads.get(position));
Log.e("rcc","::"+uploadCurrent.getComment());
Log.e("rcc","::"+uploadCurrent.getProfile_p_url());
}
@Override
public int getItemCount() {
return mcUploads.size();
}
public class ImageViewHolderComment extends RecyclerView.ViewHolder {
public TextView commentText;
public ImageView commentPhoto;
public ImageViewHolderComment(@NonNull View itemView) {
super(itemView);
commentText = itemView.findViewById(R.id.comment_content);
commentPhoto = itemView.findViewById(R.id.comment_photo);
}
}
}
在我的bindViewHolder上,mcUploads.get(position)
的数据也正确显示,但是
holder.commentText.setText(uploadCurrent.getComment());
Picasso.get().load(uploadCurrent.getProfile_p_url()).fit().centerCrop().into(holder.commentPhoto);
都为空
评论模型类别:
package com.example.atelier.models;
import com.google.firebase.database.Exclude;
public class Comments {
private String comment;
private String profile_p_url;
private String mKey;
public Comments() {
}
public Comments(String comment) {
this.comment = comment;
}
public String getComment() {
return comment;
}
public String getProfile_p_url() {
return profile_p_url;
}
public void setComment(String description) {
this.comment = comment;
}
public void setProfile_p_url(String image_url) {
this.profile_p_url = profile_p_url;
}
@Exclude
public String getKey() {
return mKey;
}
@Exclude
public void setKey(String key) {
mKey = key;
}
}
数据库结构:(其中一些也有图像,但是我只对文本感到满意,然后我自己就能弄清楚)
{
"Comments" : {
"-LvzF_VRJQ25bTUZK-dt" : {
"comment" : "woooorrrkk",
"profile_p_url" : "https://firebasestorage.googleapis.co..."
},
"-LvzRnxOZI6MMTMcg0Nf" : {
"comment" : "ngsjtsjyskydkysoy",
"profile_p_url" : "https://firebasestorage.googleapis..."
},
"-LvzYP1jevJYongMxGiJ" : {
"comment" : "kgsjiiyxkgxkt"
},
"-Lvz_5aM-kIg6qaKPFjv" : {
"comment" :
"jajhahahannabahhsbsbbs"
}`
日志返回:
2019-12-13 15:50:17.972 5664-5664/com.example.atelier E/rcc:
::com.example.atelier.models.Comments@54e5ea1
2019-12-13 15:50:17.972 5664-5664/com.example.atelier E/rcc: ::null
2019-12-13 15:50:17.972 5664-5664/com.example.atelier E/rcc: ::null
2019-12-13 15:50:17.979 5664-5664/com.example.atelier E/rcc:
::com.example.atelier.models.Comments@c4b4d02
2019-12-13 15:50:17.979 5664-5664/com.example.atelier E/rcc: ::null
2019-12-13 15:50:17.979 5664-5664/com.example.atelier E/rcc: ::null
2019-12-13 15:50:17.984 5664-5664/com.example.atelier E/rcc:
::com.example.atelier.models.Comments@4be635f
2019-12-13 15:50:17.984 5664-5664/com.example.atelier E/rcc: ::null
2019-12-13 15:50:17.984 5664-5664/com.example.atelier E/rcc: ::null
2019-12-13 15:50:17.989 5664-5664/com.example.atelier E/rcc:
::com.example.atelier.models.Comments@171bbc8
2019-12-13 15:50:17.989 5664-5664/com.example.atelier E/rcc: ::null
2019-12-13 15:50:17.989 5664-5664/com.example.atelier E/rcc: ::null