RecyclerView没有显示

时间:2016-06-10 08:22:16

标签: android android-recyclerview adapter

我使用RecyclerView和自定义适配器,但它没有显示数据。 适配器从数据库下载一些缩略图图像,并从Api密钥获取标题和信息。

适配器类的代码

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

 private List<String> fTitle;
 private List<String> fInfos;
 private List<String> thumbs;

 public mostViewedAdapter(List<String> titoli, List<String> infos, List<String>thumbsUrl){    
   fTitle = titoli;
   fInfos = infos;
   thumbs = thumbsUrl;    
 }

 public static class ViewHolder extends RecyclerView.ViewHolder {
    public TextView titleTextView;
    public TextView infoTextView;
    public ImageView thumbImageView;

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

       titleTextView = (TextView) itemView.findViewById(R.id.title);
       infoTextView = (TextView) itemView.findViewById(R.id.infos);
       thumbImageView = (ImageView) itemView.findViewById(R.id.thumb);    
    }
  }  

 @Override
 public mostViewedAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Context context = parent.getContext();
    LayoutInflater inflater = LayoutInflater.from(context);

    View mvView = inflater.inflate(R.layout.most_viewed, parent, false);    
    ViewHolder viewHolder = new ViewHolder(mvView);
    return viewHolder;
 }

 @Override
 public void onBindViewHolder(mostViewedAdapter.ViewHolder holder, int position) {

    String titolo = fTitle.get(position);
    String info = fInfos.get(position);
    String thumb = thumbs.get(position);

    UrlGenerator url = new UrlGenerator(thumb);
    Picasso.with(holder.thumbImageView.getContext()).load(url.generateImgUrl())
          .resize(50,70).centerCrop().into(holder.thumbImageView);
    holder.infoTextView.setText(info);
    holder.titleTextView.setText(titolo);

    TextView tvTitolo = holder.titleTextView;
    TextView tvInfo = holder.infoTextView;
    ImageView ivImage = holder.thumbImageView;    
  }

 @Override
 public int getItemCount() {
   return 0;
 }   
}

Recycler view:

public void initUI() {

    RecyclerView recList = (RecyclerView) findViewById(R.id.rvCinema);
    recList.hasFixedSize();

    mostViewedAdapter adapter = new mostViewedAdapter(titoli, info, thumbsUrl);
    recList.setAdapter(adapter);
    RecyclerView.LayoutManager lm = new LinearLayoutManager(this);
    recList.setLayoutManager(lm);
}

2 个答案:

答案 0 :(得分:1)

尝试这样:

<强> MainActivity

public class MainActivity extends AppCompatActivity {
 RecyclerView mRecyclerView;
 mostViewedAdapter mViewAdapter;
 //.... other elements

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);     

    mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView_ID);
    mRecyclerView.setHasFixedSize(true);        
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
    mViewAdapter= new mostViewedAdapter (listName, listInfo, listThumbs);
    mRecyclerView.setAdapter(mViewAdapter);
 }
}

适配器类

不是将0作为计数返回,而是返回三个列表中任何一个列表的数组大小。

@Override
public int getItemCount() {
  return fTitle.size();
   //return fInfos.size();  
   //return thumbs.size();  
}

希望我帮助,如果仍然面临任何问题,请发布logcat。

答案 1 :(得分:0)

请返回fTitle.size();
     @Override public int getItemCount() { return fTitle.size(); }