我们已经在项目中实现RecyclerView了一段时间,尽管后端似乎工作得很好,但一旦我调用recyclerView.setAdapter,它就会导致程序崩溃。我花了几个小时仔细研究这个程序,尽管我怀疑答案在适配器上,但到目前为止还没有什么。谁能对此有所启发?
创建并设置适配器:
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView = findViewById(R.id.navRV);
mRecyclerView.setHasFixedSize(false);
mRecyclerView.setLayoutManager(mLayoutManager);
Date todaysDate = new Date(System.currentTimeMillis());
AnnouncementList.add("Test Announcement 1", "This is some random text in the body" +
". It really needs to show up correctly!!", todaysDate);
mAdapter = new TwitterAdapter(mContext);
mRecyclerView.setAdapter(mAdapter);
这是适配器:
package com.company.application;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* Twitter Adapter:
* This adapter is the interface between the data and the view
*/
public class TwitterAdapter extends RecyclerView.Adapter<TwitterAdapter.ViewHolder> {
//Use this as the tag for logging in this activity
final private String LOGTAG = "TWITTER_ADAPTER";
private Context mContext;
public TwitterAdapter(Context mContext) {
this.mContext = mContext;
}
@NonNull
@Override
public TwitterAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.list_layout, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull TwitterAdapter.ViewHolder holder, int position) {
//Set the text and the title
holder.textViewBody.setText((String)AnnouncementList.getList().get(position).getBody());
holder.textViewTitle.setText((String)AnnouncementList.getList().get(position).getTitle());
//If we have an image, insert it so we can draw it
if(AnnouncementList.getList().get(position).getImage() != null)
holder.imageView.setImageDrawable((Drawable)AnnouncementList.getList().get(position).getImage());
else
holder.imageView.setImageDrawable(null);
}
@Override
public int getItemCount() {
Log.d(LOGTAG, "getItemCount returns " + AnnouncementList.size());
return AnnouncementList.size();
}
/**
* Holds the view so that the RecyclerView can process it
*/
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public RelativeLayout mRelativeLayout;
ImageView imageView;
TextView textViewTitle, textViewBody, textViewDate;
public ViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
textViewTitle = itemView.findViewById(R.id.textViewTitle);
textViewBody = itemView.findViewById(R.id.textViewBody);
// textViewDate = itemView.findViewById(R.id.textViewDate);
}
}
}
答案 0 :(得分:0)
您需要像这样将arraylist
传递给适配器
mAdapter = new TwitterAdapter(mContext, AnnouncementList);
然后在适配器中像这样更改构造函数
private Context mContext;
private ArrayList<ModelClass> mAnnouncementList = new ArrayList();
public TwitterAdapter(Context mContext,ArrayList<ModelClass> mAnnouncementList) {
this.mContext = mContext;
this.mAnnouncementList= mAnnouncementList;
}
然后访问这样的变量
holder.textViewBody.setText(mAnnouncementList.getList().get(position).getBody());
holder.textViewTitle.setText(mAnnouncementList.getList().get(position).getTitle());
别忘了返回下面的尺寸
@Override
public int getItemCount() {
Log.d(LOGTAG, "getItemCount returns " + mAnnouncementList.size());
return mAnnouncementList.size();
}