Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use RecyclerView, Trying to follow developer.android.com suggestion but get error

I'm trying to use RecyclerView to show my dataset, trying to follow this website https://developer.android.com/training/material/lists-cards.html

Problem is that there is some part wrong and can't find out how to fix it, I did exactly how that site suggest.

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private String[] mDataset;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView mTextView;
        public ViewHolder(TextView v) {
            super(v);
            mTextView = v;
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(String[] myDataset) {
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                           .inflate(R.layout.my_text_view, parent, false);
        // set the view's size, margins, paddings and layout parameters
        ...
        ViewHolder vh = new ViewHolder(v);     // ERROR
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        holder.mTextView.setText(mDataset[position]);

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}

Android Studio gives me an error:

ViewHolder (android.widget.TextView) in ViewHolder cannot be applied
to         (android.view.View)

How should I do this?

It seems developer.android.com have some typo here?

like image 339
EspeH Avatar asked Oct 30 '22 20:10

EspeH


1 Answers

When you inflate a view, it's usually a good idea to create an instance of the specific type of view that you are inflating. In this case you are inflating a TextView. Since TextView extends from View the compiler will not complain about it, but when you try to use the View as a TextView your app will crash.

// create a new view
TextView v = (TextView) LayoutInflater.from(parent.getContext())
                       .inflate(R.layout.my_text_view, parent, false);

Change View to TextView.

like image 54
Scott Cooper Avatar answered Nov 09 '22 10:11

Scott Cooper