聊天应用程序的recyclerview不显示任何数据或视图

时间:2018-11-15 20:38:21

标签: java android android-recyclerview chat retrofit2

您好,我正在使用android构建聊天应用程序用户界面,但它在运行时不会显示任何数据或视图, 我与Bot服务器互动,该服务器在发送文本后应返回文本。我正在对HTTP连接进行改造。

显示的模型类是json的响应

请帮助我,问我您想要的任何细节

我的适配器

public class MessageListAdapter extends 
RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int VIEW_TYPE_MESSAGE_SENT = 1;
private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2;
private List<Messages> data;
private Context mContext;

// constructor
public MessageListAdapter(List<Messages> data, Context mContext) {
    this.data = data;
    this.mContext = mContext;
}


@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View itemView;

    if (viewType == VIEW_TYPE_MESSAGE_SENT) {


        return new SentMessageHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.user_message, parent, false));
    } else {


        return new RecvdMessageHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.bot_message, parent, false));
    }

}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    int type = getItemViewType(position);

    Messages messages = data.get(position);


    if (type == VIEW_TYPE_MESSAGE_SENT) {


        SentMessageHolder sentMessageHolder = (SentMessageHolder) holder;
        sentMessageHolder.messageBody.setText(messages.getText());

    } else {


        RecvdMessageHolder recvdMessageHolder = (RecvdMessageHolder) holder;
        recvdMessageHolder.messageBody.setText(messages.getText());
    }
}

public int getItemViewType(int position) {

    Messages messages = data.get(position);


    if (messages.getRecipientId() == ("default")) {
        return VIEW_TYPE_MESSAGE_SENT;
    } else
        return VIEW_TYPE_MESSAGE_RECEIVED;

}

@Override
public int getItemCount() {


    if (data != null) {
        return data.size();

    } else return 0;
}

class SentMessageHolder extends RecyclerView.ViewHolder {

    public TextView messageBody;


    public SentMessageHolder(View itemView) {
        super(itemView);

        messageBody = (TextView) itemView.findViewById(R.id.text_message_body);

    }
}

class RecvdMessageHolder extends RecyclerView.ViewHolder {

    public TextView messageBody;

    public RecvdMessageHolder(View itemView) {
        super(itemView);

        messageBody = (TextView) itemView.findViewById(R.id.text_message_body);

    }
}

我的活动

public class MainActivity extends AppCompatActivity {
private EditText editText;
private RecyclerView mMessageRecycler;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter MessageListAdapter;
List<Messages> data;


//init client
Retrofit retrofit = RetrofitClient.getRetrofitClient();
//contact with the interface
APIService apiService = retrofit.create(APIService.class);


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText = findViewById(R.id.my_msg);

    data = new ArrayList<>();

    mMessageRecycler = (RecyclerView) findViewById(R.id.reyclerview_message_list);
    MessageListAdapter = new MessageListAdapter(data , this);
   // messageListAdapter.notifyDataSetChanged();
    mMessageRecycler.setAdapter(MessageListAdapter);
    mMessageRecycler.setLayoutManager(new LinearLayoutManager(this));


    mMessageRecycler.setHasFixedSize(true);




}

public void clickedbtn(View view) {
    fetchMessages();

}

private void fetchMessages() {


    Call<List<Messages>> res = apiService.getBotResponse(editText.getText().toString());

        res.enqueue(new Callback<List<Messages>>() {
            @Override
            public void onResponse(Call<List<Messages>> call, Response<List<Messages>> response) {

                List<Messages> messages = response.body();
                data.addAll(messages);//Changed here
                MessageListAdapter.notifyDataSetChanged();//Changed here


                mMessageRecycler.smoothScrollToPosition(messages.size()-1);




            }

            @Override
            public void onFailure(Call<List<Messages>> call, Throwable t) {


            }
        });
    }

我的模型课

public class Messages {

@SerializedName("recipient_id")
@Expose
private String recipientId;
@SerializedName("text")
@Expose
private String text;


public Messages(String text) {
    this.text = text;
}

public String getRecipientId() {
    return recipientId;
}

public void setRecipientId(String recipientId) {
    this.recipientId = recipientId;
}

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

1 个答案:

答案 0 :(得分:0)

尝试将Adapter RecyclerView.setAdapter()的实例化分别添加到改造onResponse()中: 像这样:

Call<List<Messages>> res = apiService.getBotResponse(editText.getText().toString());

    res.enqueue(new Callback<List<Messages>>() {
        @Override
        public void onResponse(Call<List<Messages>> call, Response<List<Messages>> response) {

            List<Messages> messages = response.body();
            //Init the Adpater here!!
            MessageListAdapter = new MessageListAdapter(data , this);
            //SetAdapter here!!
            mMessageRecycler.setAdapter(MessageListAdapter);
            data.addAll(messages);//Changed here

            MessageListAdapter.notifyDataSetChanged();//Changed here


            mMessageRecycler.smoothScrollToPosition(messages.size()-1);




        }

        @Override
        public void onFailure(Call<List<Messages>> call, Throwable t) {


        }
    });

编辑:

除上述内容外,您的clickedbtn()应该如下所示:

public void clickedbtn(View view) {

    Messages msg = new Messages(editText.getText().toString());
    msg.setRecipientId("default");

    data.add(msg);
    MessageListAdapter.notifyDataSetChanged();

    fetchMessages();

}