RecyclerView不显示任何CardView项目 [英] RecyclerView Not Displaying Any CardView Items

查看:250
本文介绍了RecyclerView不显示任何CardView项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从ListView切换到RecyclerView,并没有看到我的移动设备上显示的任何项目。

我知道一个事实,它不必做与 getItemCount()方法,因为我已经硬编码了一个数字返回来测试它。



这是我的初始化,我用Firebase来获取新的数据:

$ @ $ $ $ $ b $保护无效的onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Firebase.setAndroidContext(this);
setContentView(R.layout.activity_discussion);
toolbar =(Toolbar)findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
setTitle(R.string.discussion_title_text);

mDateFormat = new SimpleDateFormat(MM-dd-yyyy);
mDate = new Date();
mCurrentDateString = mDateFormat.format(mDate);

mBaseRef =新的Firebase(FIREBASE_URL);
mPollsRef = mBaseRef.child(POLLS_LABEL);
mUpdateRef = mPollsRef.child(mCurrentDateString).child(String.valueOf(mPollIndex + 1));
mCommentsRef = mUpdateRef.child(COMMENTS_LABEL);

mPollImage =(ImageView)findViewById(R.id.comments_image);
mPollCommentQuestion =(TextView)findViewById(R.id.poll_comment_question);

mUserComment =(EditText)findViewById(R.id.user_comment);
mUserAvatar =(ImageView)findViewById(R.id.profile_image_avatar);
mPollCommentsList =(RecyclerView)findViewById(R.id.poll_comments_list);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
mPollCommentsList.setLayoutManager(llm);
mCommentArrayList = new ArrayList< Comments>();
mCommentIDArrayList = new ArrayList< String>();
mPollCommentsList.setAdapter(mCommentAdapter);
getWindow()。setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

Intent intent = getIntent();
String pollID = intent.getStringExtra(POLL_ID);
mPollIndex = intent.getIntExtra(POLL_INDEX,0);

$ b mUpdateRef.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){$ b $ 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,评论ID是+ commentID);
String userID =(String)dataSnapshot.child(COMMENTS_LABEL).child(mCommentIDArrayList.get(i))。child(USER_ID)。getValue ();
Log.v(USER_ID,用户ID是+ userID);
mCommentArrayList.add(0,new Comments(mUserA vatar,userID,commentID));
mCommentAdapter.notifyDataSetChanged();
}

}

@Override
public void onCancelled(FirebaseError firebaseError){

}
} );
mCommentAdapter = new MyAdapter(mCommentArrayList);

// TODO:将唯一注释ID存储在数组中

// TODO:了解如何以编程方式将图像添加到AWS,然后将URL存储在Firebase中
ImageView fab =(ImageView)findViewById(R.id.add_comment);
fab.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
HashMap< String,Object> commentMap = new HashMap< String,对象>();
commentMap.put(USER_ID,mBaseRef.getAuth()。getUid());
commentMap.put(COMMENT,mUserComment.getText()。toString()) ;
mUpdateRef.child(COMMENTS_LABEL).push()。updateChildren(commentMap);
hideKeyboard(view);
mUserComment.setText();
Toast.makeText getApplicationContext(),R.string.comment_added,Toast.LENGTH_LONG).show();
}
});

$ / code $ / $ p

这是我的适配器:

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

private ArrayList< Comments> mDataSet;

//提供对每个数据项的视图的引用
//复杂数据项可能需要每个项目多个视图,
//您提供对所有数据项的访问视图持有者中的数据项的视图
public class ViewHolder extends RecyclerView.ViewHolder {
//在这种情况下,每个数据项只是一个字符串
protected ImageView userAvatar;
保护TextView userID;
保护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);



提供一个合适的构造函数(取决于数据集的类型)
public MyAdapter(ArrayList< Comments> myDataset){
mDataSet = myDataset;
}

//创建新视图(由布局管理器调用)
@Override $ b $ public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType ){
//创建一个新视图
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.individual_comment,parent,false);
//设置视图的大小,边距,填充和布局参数
ViewHolder x = new ViewHolder(v);

return x;
}

//替换一个视图的内容(由布局管理器调用)
$ b $ @覆盖
public void onBindViewHolder(ViewHolder holder,int位置){
holder.userComment.setText(mCommentArrayList.get(position).getUserComment());
holder.userID.setText(mCommentArrayList.get(position).getUserID());


//返回数据集的大小(由布局管理器调用)
@Override
public int getItemCount(){
return mNumberOfCommentsAtPoll ;


$ / code $ / pre

解决方案

Make确定你在 RecyclerAdapter 上设置了 LayoutManager 。最常见的方法是:
$ b $ m $ c $ mCommentAdapter.setLayoutManager(LinearLayoutManager(this));


I have switched from ListView to RecyclerView and am not seeing any items displayed on my mobile device.

I know for a fact that it doesn't have to do with the getItemCount() method as I have hardcoded a number to that return to test it.

Here is my initialization, I used Firebase to pull new data:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Firebase.setAndroidContext(this);
    setContentView(R.layout.activity_discussion);
    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);
    setTitle(R.string.discussion_title_text);

    mDateFormat = new SimpleDateFormat("MM-dd-yyyy");
    mDate = new Date();
    mCurrentDateString = mDateFormat.format(mDate);

    mBaseRef = new Firebase(FIREBASE_URL);
    mPollsRef = mBaseRef.child(POLLS_LABEL);
    mUpdateRef = mPollsRef.child(mCurrentDateString).child(String.valueOf(mPollIndex + 1));
    mCommentsRef = mUpdateRef.child(COMMENTS_LABEL);

    mPollImage = (ImageView) findViewById(R.id.comments_image);
    mPollCommentQuestion = (TextView) findViewById(R.id.poll_comment_question);

    mUserComment = (EditText) findViewById(R.id.user_comment);
    mUserAvatar = (ImageView) findViewById(R.id.profile_image_avatar);
    mPollCommentsList = (RecyclerView) findViewById(R.id.poll_comments_list);
    LinearLayoutManager llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    mPollCommentsList.setLayoutManager(llm);
    mCommentArrayList = new ArrayList<Comments>();
    mCommentIDArrayList = new ArrayList<String>();
    mPollCommentsList.setAdapter(mCommentAdapter);
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

    Intent intent = getIntent();
    String pollID = intent.getStringExtra("POLL_ID");
    mPollIndex = intent.getIntExtra("POLL_INDEX", 0);


    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) {

        }
    });
    mCommentAdapter = new MyAdapter(mCommentArrayList);

    //TODO: Store unique comment ID's in an array

    //TODO: Figure out how to programatically add images to AWS and then store URL in Firebase
    ImageView fab = (ImageView) findViewById(R.id.add_comment);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            HashMap<String, Object> commentMap = new HashMap<String, Object>();
            commentMap.put("USER_ID", mBaseRef.getAuth().getUid());
            commentMap.put("COMMENT", mUserComment.getText().toString());
            mUpdateRef.child(COMMENTS_LABEL).push().updateChildren(commentMap);
            hideKeyboard(view);
            mUserComment.setText("");
            Toast.makeText(getApplicationContext(), R.string.comment_added, Toast.LENGTH_LONG).show();
        }
    });
}

Here is my adapter:

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

    private ArrayList<Comments> mDataSet;

    // 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) {
        mDataSet = 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
        ViewHolder x = new ViewHolder(v);

        return x;
    }

    // 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;
    }
}

解决方案

Make sure you set a LayoutManager on your RecyclerAdapter. The most common way would be:

mCommentAdapter.setLayoutManager(new LinearLayoutManager(this));

这篇关于RecyclerView不显示任何CardView项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆