gpt4 book ai didi

android - 动态 RecyclerView 不填充数据

转载 作者:太空宇宙 更新时间:2023-11-03 13:49:01 24 4
gpt4 key购买 nike

我已经从 ListView 切换到 RecyclerView,并且我使用来自 Firebase 的数据动态添加 RecyclerView 项目。但是,我无法成功生成 RecyclerView。

目前,屏幕是空白的,并且没有 CardView 项目被添加到 View 中。我假设我的 RecyclerView 适配器有问题。

我的监听器,我在此处添加到 mCommentArrayList,然后从我的 RecyclerView 适配器中提取此数据:

  mUpdateRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
setImage(dataSnapshot);
setQuestion(dataSnapshot);
createInitialCommentIDArray(dataSnapshot);
mNumberOfCommentsAtPoll = (int) dataSnapshot.child(COMMENTS_LABEL).getChildrenCount();
for (int i = 0; i < mNumberOfCommentsAtPoll; i++) {
String commentID = (String) dataSnapshot.child(COMMENTS_LABEL).child(mCommentIDArrayList.get(i)).child("COMMENT").getValue();
Log.v("COMMENT_ID", "The comment ID is " + commentID);
String userID = (String) dataSnapshot.child(COMMENTS_LABEL).child(mCommentIDArrayList.get(i)).child("USER_ID").getValue();
Log.v("USER_ID", "The user ID is " + userID);
mCommentArrayList.add(0 , new Comments(mUserAvatar, userID, commentID));
mCommentAdapter.notifyDataSetChanged();
}

}

@Override
public void onCancelled(FirebaseError firebaseError) {

}
});

这是我的 RecyclerView 适配器:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
protected ImageView userAvatar;
protected TextView userID;
protected TextView userComment;

public ViewHolder(View v) {
super(v);
userAvatar = (ImageView) findViewById(R.id.profile_image_avatar);
userID = (TextView) findViewById(R.id.user_ID);
userComment = (TextView) findViewById(R.id.user_comment);
}
}

// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(ArrayList<Comments> myDataset) {
mCommentArrayList = myDataset;
}

// Create new views (invoked by the layout manager)
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.individual_comment, parent, false);
// set the view's size, margins, paddings and layout parameters

return new ViewHolder(v);
}

// Replace the contents of a view (invoked by the layout manager)

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.userComment.setText(mCommentArrayList.get(position).getUserComment());
holder.userID.setText(mCommentArrayList.get(position).getUserID());
}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mNumberOfCommentsAtPoll;
}
}

最佳答案

RecycleViewAdapter 的 getItemCount() 中,您应该返回列表数据源大小

@Override
public int getItemCount() {
return mCommentArrayList.size();
}

关于android - 动态 RecyclerView 不填充数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37403181/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com