我的recyclerview没有显示我的数组列表

时间:2019-01-29 12:14:41

标签: android android-recyclerview

我有一个recyclerview,编辑文本和按钮。当我写东西并单击按钮时,recyclerview必须显示我插入的内容。但是,当我插入并单击按钮时,什么都没有发生,但是当我单击带有空的编辑文本的按钮时,它显示的是空文本视图。

这是我的oncreate方法:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        mRecyclerViewChat = (RecyclerView) findViewById(R.id.messages_list_of_users);
        mInputMessageText = (EditText) findViewById(R.id.input_message);
        mSendMessageButton = (ImageButton) findViewById(R.id.send_message);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerViewChat.setLayoutManager(linearLayoutManager);

        chatAdapter = new ChatAdapter(mChatList);
        mRecyclerViewChat.setAdapter(chatAdapter);
    }

这是我的onclick方法:

mSendMessageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String message = mInputMessageText.getText().toString().trim();
                if (message != null && TextUtils.isEmpty(message)){
                    mChatList.add(message);
                    mInputMessageText.setText("");
                    Toast.makeText(ChatActivity.this, "message is not null", Toast.LENGTH_SHORT).show();
                    if (chatAdapter != null){
                        chatAdapter.notifyDataSetChanged();
                        Toast.makeText(ChatActivity.this, "chat adapter is not null", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

这是我的适配器类:

public class ChatAdapter extends  RecyclerView.Adapter<ChatAdapter.ChatViewHolder>{
    private ArrayList<String> mChatList;
    public ChatAdapter(ArrayList<String> mChatList) {
        this.mChatList = mChatList;
    }
    @Override
    public ChatViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.item_chat_box, parent, false);
        return new ChatViewHolder(view);
    }
    @Override
    public void onBindViewHolder(ChatViewHolder holder, int position) {
        holder.messageLeft.setText(mChatList.get(position));
    }
    @Override
    public int getItemCount() {
        return mChatList.size();
    }
    public class ChatViewHolder extends RecyclerView.ViewHolder {
        TextView messageLeft, messageRight;
        public ChatViewHolder(View itemView) {
            super(itemView);
            messageLeft = (TextView) itemView.findViewById(R.id.item_chat_box_left);
            messageRight = (TextView) itemView.findViewById(R.id.item_chat_box_right);
        }
    }
} 

这是我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <RelativeLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/grey_bg">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/messages_list_of_users"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="30dp"
            android:layout_marginBottom="80dp"
            android:layout_centerVertical="true">

        </android.support.v7.widget.RecyclerView>

        <LinearLayout
            android:id="@+id/message_linlay"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true"
            android:orientation="horizontal"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingTop="5dp"
            android:paddingBottom="2dp"
            android:background="@color/white">

            <ImageButton
                android:id="@+id/select_image"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:padding="5dp"
                app:srcCompat="@drawable/ic_add_a_photo_black_24dp"
                android:background="@color/white"/>

            <EditText
                android:id="@+id/input_message"
                android:layout_width="250dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="Write a message"
                android:inputType="textMultiLine"
                android:padding="10dp" />

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="right">

                <ImageButton
                    android:id="@+id/send_message"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:paddingLeft="1dp"
                    app:srcCompat="@drawable/ic_send_black_24dp"
                    android:background="@color/white"/>
            </LinearLayout>

        </LinearLayout>

    </RelativeLayout>
</layout>

如果我什么都不输入然后单击它,则显示为空textview

3 个答案:

答案 0 :(得分:1)

问题出在您的if (message != null && TextUtils.isEmpty(message))情况下

您正在检查 message != null && TextUtils.isEmpty(message) ,它们不能返回true,因此请更改您的条件

尝试一下

mSendMessageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String message = mInputMessageText.getText().toString().trim();
                if (message != null && !TextUtils.isEmpty(message)){
                    mChatList.add(message);
                    mInputMessageText.setText("");
                    Toast.makeText(ChatActivity.this, "message is not null", Toast.LENGTH_SHORT).show();
                    if (chatAdapter != null){
                        chatAdapter.notifyDataSetChanged();
                        Toast.makeText(ChatActivity.this, "chat adapter is not null", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

答案 1 :(得分:1)

您是否正在创建数组mChatList?因为在您的代码中我看不到您在哪里创建列表(mChatList = new ArrayList();),并且您在OnClickListener中的if条件错误,因此必须将if(message!= null &&!TextUtils.isEmpty(message) )以确保您的条件正常工作。

答案 2 :(得分:1)

在您的适配器类中,我没有看到任何类似于以下内容的方法:

public void addToChat(String message) {
    mChatList.add(message);
    notifyDataSetChanged();
}

我假设添加到mChatList在您的活动中。好吧,您的recyclerview不会看到该列表,因为它是由一个单独的mChatList对象运行的。

因此,在您的答题器中,采用上述方法,您需要进行以下修改:

if (chatAdapter != null) {
    chatAdapter.addToChat(message);
}

如果您仍要引用/更新它,则仍然可以保留其他mChatList.add(message);

但是请记住,您有两个 mChatList;一个在您的活动中,另一个在您的适配器中。虽然您的recyclerview是使用您的活动中的一个初始化的,但实际上它实际上是从其适配器中运行的一个。

注意:哦,是的,正如Nilesh所提到的,您还需要解决您的情况。

if (message != null && TextUtils.isEmpty(message))

如果消息为?您必须要说的是,如果邮件为空:

if (message != null && !TextUtils.isEmpty(message))