无法使用自定义卡创建适配器

时间:2017-07-22 12:40:02

标签: android android-arrayadapter android-cardview recycler-adapter

我正在使用cardslib库在我的Android应用中拥有卡片。它具有预定义的卡片布局,或者我们可以选择自定义布局。

在为回收器视图创建适配器时,仅接受具有预定义卡布局的卡对象列表。当我尝试将自定义卡列表传递给构造函数时,出现错误。

CardArrayRecyclerViewAdapter cardArrayRecyclerViewAdapter = new CardArrayRecyclerViewAdapter(getApplicationContext(), customCards);

上面的代码显示错误。 customCards是自定义卡片对象的列表。我如何使它工作?

3 个答案:

答案 0 :(得分:0)

在构建函数中CardArrayRecyclerViewAdapter更改List<Card>List<CustomCard>

答案 1 :(得分:0)

你可以写下自定义卡适配器

  1. 卡适配器调用就像

    一样
    List<CustomCard> cardlist = new ArrayList<>();
    recyclerView.setHasFixedSize(true);
    // recyclerView
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    // create an Object for Adapter
    CardDataAdapter  mAdapter = new CardDataAdapter(cardlist);
    // set the adapter object to the Recyclerview
    recyclerView.setAdapter(mAdapter);
    
  2. 自定义适配器

      class CardDataAdapter extends RecyclerView.Adapter<CardDataAdapter.ViewHolder> {
    
    private List<CustomCard> dataSet;
    
    public CardDataAdapter(List<CustomCard> list) {
    
        dataSet = list;
    }
    
    
    @Override
    public CardDataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        // create a new view
        View itemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(
                R.layout.row_custom_card, null);
    
        itemLayoutView.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
    
        // create ViewHolder
    
        CardDataAdapter.ViewHolder viewHolder = new CardDataAdapter.ViewHolder(itemLayoutView);
    
        return viewHolder;
    }
    
    
    @Override
    public void onBindViewHolder(CardDataAdapter.ViewHolder viewHolder, int i) {
    
        CustomCard fp = dataSet.get(i);
    
        viewHolder.tv_title.setText(fp.getName());
    
    
        viewHolder.feed = fp;
    }
    
    @Override
    public int getItemCount() {
    
        return dataSet.size();
    }
    
    // inner class to hold a reference to each item of RecyclerView
    public  class ViewHolder extends RecyclerView.ViewHolder {
    
        public TextView tv_title;
    
        public NotificationModel feed;
    
        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);
    
            tv_title = (TextView) itemLayoutView  .findViewById(R.id.tv_title);
            itemLayoutView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                   //Write here
    
                }
            });
    
    
    
           }
    
        }
     }
    
  3. 注意:CustomCard是型号&amp; row_custom_card布局xml,其中名称为Textview resde

答案 2 :(得分:0)

CardArrayRecyclerViewAdapter构造函数的第二个参数必须是List<Card>类型。我知道List<CustomCard>似乎应该在这里工作,但事实并非如此。

无法说明解决此问题的最佳方法是在没有看到更多代码的情况下,但您可以这样做:

List<Card> cards = new ArrayList<>();
cards.addAll(customCards);
// now call constructor with `cards` instead of `customCards`

您可以在此处详细了解List<CustomCard>参数List<Card>不接受if ($('.html').data('load-file')) { $(this).load($(this).data("load-file") + ' ' + $(this).data("load-content")); }的原因Is List<Dog> a subclass of List<Animal>? Why aren't Java's generics implicitly polymorphic?