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 Fragments Introducing Fragments Making a List

listAdapter is Abstract? Cannot instantiate

@Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    //second is the view group where the fragment_list is being added
    //Fragments can be added in the XML of an activity or use the view group as a place holder
    View view = inflater.inflate(R.layout.fragment_list, container, false);

    RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.listRecyclerView);
    ListAdapter listAdapter = new ListAdapter();
    recyclerView.setAdapter(listAdapter);

   return view;

I am following the instructions in the video. I am getting the message that the listAdapter cannot be instantiated because its abstract? Why is that the case?

2 Answers

Seth Kroger
Seth Kroger
56,414 Points

There are 2 possibilities that come to mind. 1) Since your custom adapter class is called ListAdapter and there is also an Android OS class/interface called ListAdapter it's possible you imported the wrong one. 2) Your ListAdapter extends an abstract class and you need to override all the required methods or your ListAdapter class will be abstract as well, though that should show an error in ListAdapter.

Hello, I'm pretty sure I implemented the right ListAdapter.

And all the required methods are overwritten. This is what I have for the listAdapter class

public class ListAdapter extends RecyclerView.Adapter { @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    //False - We dont want to attach the view to the parentview group.
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
    return new listViewHolder(view);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    ((listViewHolder) holder).bindView(position);

}

@Override
public int getItemCount() {

    //Recipes array objects are static and will never change so it's ok to use the names to get the array length
    return Recipes.names.length;
}

private class listViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private TextView mTextView;
    private ImageView mImageView;

    public listViewHolder(View itemView) {
        super(itemView);

        mTextView = (TextView) itemView.findViewById(R.id.itemText);
        mImageView = (ImageView) itemView.findViewById(R.id.itemImage);
        itemView.setOnClickListener(this);
    }

    public void bindView(int position) {


        mTextView.setText(Recipes.names[position]);
        mImageView.setImageResource(Recipes.resourceIds[position]);

    }

    @Override
    public void onClick(View v) {

    }
}

}

I figured it out. Thanks :)