作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图为 Android 制作一个聊天应用程序,所以我使用了 RecyclerView
。我的适配器有问题,我的聊天室收到的 JSON 响应显示为空白。我的代码是否遗漏了某些内容?
这是我的适配器类
public class PesanRecycleAdapter extends RecyclerView.Adapter<PesanRecycleAdapter.ViewHolder> {
private String username;
private Context context;
private int SELF = 786;
private ArrayList<Pesan> pesan;
public PesanRecycleAdapter(Context context, ArrayList<Pesan> pesan, String username){
this.username = username;
this.pesan = pesan;
this.context = context;
}
@Override
public int getItemViewType(int position) {
Pesan psn = pesan.get(position);
if (psn.getUsernameFrom() == username) {
return SELF;
}
return position;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView;
if (viewType == SELF) {
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.chat_thread_me, parent, false);
} else {
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.chat_thread_other, parent, false);
}
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Pesan psn = pesan.get(position);
holder.textViewMessage.setText(psn.getPesan());
holder.textViewTime.setText(psn.getSentAt());
}
@Override
public int getItemCount() {
return pesan.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView textViewMessage;
public TextView textViewTime;
public ViewHolder(View itemView) {
super(itemView);
textViewMessage = (TextView) itemView.findViewById(R.id.textViewMessage);
textViewTime = (TextView) itemView.findViewById(R.id.textViewTime);
}
}
}
欢迎任何帮助或建议
在这里我在 Activity 中使用了该适配器
public void getpesan() {
try {
apiservice.get_pesan(username, "ahnafgg").enqueue(new Callback<ResponPesan>() {
@Override
public void onResponse(Call<ResponPesan> call, Response<ResponPesan> response) {
Log.d(TAG, "onResponse: response..."+response);
dummyData = response.body().getPesan();
createRecycleView();
}
@Override
public void onFailure(Call<ResponPesan> call, Throwable t) {
Log.d(TAG, "onFailure: response...");
Toast.makeText(RuangPesan.this,"Gagal ambil chat",Toast.LENGTH_SHORT).show();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void createRecycleView(){
adapter = new PesanRecycleAdapter(RuangPesan.this, pesan, username);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
Log.d(TAG, "********************************");
scrollToBottom();
}
最佳答案
可能您忘记在初始化回收器 View 之前在模型中设置日期在您的 Activity 类中使用以下方式:
initData(); //Do this first
RecyclerView recyclerView = findViewById(R.id.recyclerview);
Context context = MainActivity.this;
PesanRecycleAdapter pesanRecycleAdapter = new PesanRecycleAdapter(context, list,"Fahad");
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(pesanRecycleAdapter);
关于java - 如何为聊天应用程序创建 RecyclerView 适配器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56610003/
我是一名优秀的程序员,十分优秀!