Using RecyclerView example does not compile

I am planning to develop an application to work with Android L and develop it using the new Material Design.

I am looking at the RecyclerView example at https://developer.android.com/preview/material/ui-widgets.html , but the example does not compile.

Below is my activity class

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String[] myDataset = new String[2];
        myDataset[0] = "Hello";
        myDataset[1] = "World";

        setContentView(R.layout.activity_recylcer);

        mRecyclerView = (RecyclerView)findViewById(R.id.recylcerView);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);

        mAdapter = new MyAdapter(myDataset);
        mRecyclerView.setAdapter(mAdapter);
    }

      

Below is my MyAdapter class

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

    // Provide a reference to the type of views that you are using
    // (custom viewholder)
    public static class ViewHolder extends RecyclerView.ViewHolder {
        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 size, margins, paddings and layout parameters
        ViewHolder vh = new ViewHolder(v);
        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;
    }
}

      

The problem I am having when I instantiate the ViewHolder, the example talks about passing a variable v

which is the View, but Android Studio says it expects a TextView.

I guess I did something wrong, as I would like to think that the examples that the Google Android development site will have the correct code, but I don’t see for the rest of my life that I could be wrong.

+3


source to share


3 answers


As far as I can see, it expects TextView

because you are querying TextView

in the constructor of your static ViewHolder class. Change this to View

. This can help.



+2


source


If you don't want to change the constructor as Boldizzar said, why not try to paint your inflated view as a TextView and create a viewer with that textview.



+2


source


Try this code

public ViewHolder(View v) {
    super(v);
    mTextView = (TextView) v.findViewById(R.id.textView);
}

      

R.layout.my_text_view looks like this

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:background="@drawable/background"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some text here"
        android:id="@+id/textView"
        android:layout_centerInParent="true" />

</RelativeLayout>

      

+2


source







All Articles