"Build a Weather App (2015)" was retired on June 6, 2018. You are now viewing the recommended replacement.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Lists and Adapters (2015) Lists with RecyclerViews Adding Methods for a RecyclerView Adapter

Zubeyr Aciksari
Zubeyr Aciksari
21,074 Points

Let's implement the methods we need for our RecyclerView adapter. We'll start with an easy one: getItemCount(). Change i

Hi, i am stuck in here, can u please send the answer? Thanks!

"Let's implement the methods we need for our RecyclerView adapter. We'll start with an easy one: getItemCount(). Change it to return the number of items this adapter is adapting."

GameAdapter.java
public class GameAdapter extends RecyclerView.Adapter<GameAdapter.GameViewHolder> {

    private Game[] mGames;

    public GameAdapter(Game[] games) {
        mGames = games;
    }

    @Override
    public GameViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.game_list_item, parent, false);
        // Task 2
        return null;
    }

    @Override
    public void onBindViewHolder(GameViewHolder holder, int position) {
        // Task 3
    }

    @Override
    public int getItemCount() {
        // Task 1
        return 0;
    }

    /* Add your code here */
    public class GameViewHolder extends RecyclerView.ViewHolder {
        public TextView mTitleLabel;

        public GameViewHolder(View v) {
            super(v);
            mTitleLabel = (TextView)v.findViewById(R.id.titleLabel);
        }

        public void bindGame(Game game) {
            mTitleLabel.setText(game.getTitle());
        }
    }
}

6 Answers

What you're wanting to do in the getItemCount() method is return the total number of elements in the collection(in this case the array mGames). Arrays have the parameter length that lets you find out how large they are for example:

int arrayLength = array.length;

would store the number of elements in the array called array into the int variable arrayLength.

In the getItemCount() method you need to return mGames.length

This code should pass task 1-3. Happy coding

public class GameAdapter extends RecyclerView.Adapter<GameAdapter.GameViewHolder> {

private Game[] mGames;

public GameAdapter(Game[] games) {
    mGames = games;
}

@Override
public GameViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.game_list_item, parent, false);
   GameViewHolder viewHolder = new GameViewHolder(view);  // Task 2
    return viewHolder;
}

@Override
public void onBindViewHolder(GameViewHolder holder, int position) {
  holder.bindGame(mGames[position]); // Task 3
}

@Override
public int getItemCount() {
    // Task 1
    return mGames.length;
}

/* Add your code here */
public class GameViewHolder extends RecyclerView.ViewHolder {
    public TextView mTitleLabel;

    public GameViewHolder(View v) {
        super(v);
        mTitleLabel = (TextView)v.findViewById(R.id.titleLabel);
    }

    public void bindGame(Game game) {
        mTitleLabel.setText(game.getTitle());
    }
}

}

Hello,

Were you not able to get this going with my answer to this question when you asked it here? If so, let me know what the problem is so I can help further.

Zubeyr Aciksari
Zubeyr Aciksari
21,074 Points

Hi James, sorry for asking it twice, the fact is i didn't understand this getItemCount() method and confused about what to do next, can u please send the answer? Thanks!

Zubeyr Aciksari
Zubeyr Aciksari
21,074 Points

Thanks a lot James, i worked :)

You're welcome. Good luck with Android programming.