Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android

Niyamat Almass
Niyamat Almass
8,176 Points

Need help for understanding recyclerView?

I want to practice recyclerView in android.So I created a project that show a 0 - 40 numbers in a list.(I know that is really easy).

But I can't do that.I don't know what to do next. I updated my project on github Here is the link

Please someone help me!!

1 Answer

Hi Niyamat,

ok i found some lines that could cause you an error:

  1. In the view holder class that you created, you have two TextViews, however you initizlized only one. see that the text view mHoleNumber is not initizlized.

  2. Still in the view holder class, in the Bind method - you set the numbers object, but it is ofcourse empty bc you didnt do anything with it. in the view holder class, create -

private Numbers numbers;

and in the bind method write -

this.numbers = numbers;

otherwise, it will approach an empty object, and thus wont represent any data.

here is an example of a view holder class that i did:

//Recycler view holder

// ViewHolder is a wrapper for an item in the RecyclerView private class CrimeHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private TextView textTitle;
    private TextView textDate;
    private CheckBox checkSolved;

    private Crime crime;

    public CrimeHolder(View itemView) {
        super(itemView);
        // listen to clicks on the list item
        itemView.setOnClickListener(this);

        textTitle = (TextView) itemView.findViewById(R.id.list_item_crime_title_text_view);
        textDate = (TextView) itemView.findViewById(R.id.list_item_crime_date_text_view);
        checkSolved = (CheckBox) itemView.findViewById(R.id.list_item_crime_solved_check_box);

        checkSolved.setOnClickListener(this);
    }

    // how to bind data to the views
    public void bindCrime(Crime crime) {
        this.crime = crime;
        textTitle.setText(crime.getTitle());
        textDate.setText(crime.getDate().toString());
        checkSolved.setChecked(crime.isSolved());
    }

i will send you also an example of a recycler view adapter that i did as well that works :

//Recycler view adapter

private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder> {

    private ArrayList<Crime> crimes;

    public CrimeAdapter(ArrayList<Crime> crimes) {
        this.crimes = crimes;
    }


    @Override
    public CrimeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create the wrapper for each item
        LayoutInflater layoutInflater = (LayoutInflater)   getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.list_item_crime, parent, false);
        return new CrimeHolder(view);
    }

    @Override
    public void onBindViewHolder(CrimeHolder holder, int position) {
        // bind the data to the holder
        Crime crime = crimes.get(position);
        holder.bindCrime(crime);
    }

    @Override
    public int getItemCount() {
        return crimes.size();
    }
}

hope that helps :)

good luck! recycler view is great but is not easy to use, you need to practice it alot!