0

I'm using the cardslib library for having cards in my Android app. It has pre-defined card layouts or we have an option to customize the layout.

While creating the adapter for recycler view, only the list of card objects with pre-defined card layouts are accepted. I get an error when I try to pass a list of custom cards to the constructor.

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

The above code shows an error. customCards is the list of custom card objects. How do I make it work ?

Anand Kumar
  • 169
  • 1
  • 11

3 Answers3

0

In your CardArrayRecyclerViewAdapter change List<Card> to List<CustomCard> in the constructor

Ajil O.
  • 6,562
  • 5
  • 40
  • 72
  • The problem is `CardArrayRecyclerViewAdapter` is an in-built adapter in the library. Am I allowed to tweak it? – Anand Kumar Jul 22 '17 at 12:48
  • Then you have to pass `List` into the adapter. Post more of your code so that someone can find a solution. – Ajil O. Jul 22 '17 at 12:50
0

you can write Custom card adapter bellow

  1. card adapter call just like

    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. custom Adapter

      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. note : CustomCard is model & row_custom_card layout xml where name Textview resde

Enamul Haque
  • 4,789
  • 1
  • 37
  • 50
0

The second argument to the CardArrayRecyclerViewAdapter constructor must be of type List<Card>. I know it seems like List<CustomCard> should work here, but it will not.

It's impossible to say what the best way to solve this problem is without seeing more code, but you could do this:

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

You can read more about why List<CustomCard> is not accepted for the List<Card> argument here Is List<Dog> a subclass of List<Animal>? Why aren't Java's generics implicitly polymorphic?

Ben P.
  • 52,661
  • 6
  • 95
  • 123