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

problem with RecyclerView

Hello everybody,

I try to do recyclerView, but I get some error in android studio. I hope if somone can help me about that, my code is:

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

    View view = inflater.inflate(R.layout.activity_list, container, false);
    RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
    NewsListAdapter adapter = new NewsListAdapter(this, mNews);
    recyclerView.setAdapter(adapter);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);

and I get error on Line: (this, mNews) , but if I do ALT+ENTER the anroid studio tell me if I want to Create constructor or to change one parameter of method 'NewsListAdapter ' in from Context.. I try to do the two options but Nothing worked out..

Thanks for help :)

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

Since you are using onCreateView() you using a Fragment, yes? Fragments don't inherit from Context, so this of a Fragment can't be substituted for one. Activities do, and you would use getActivity() or getContext() when you need to pass the Context to a method from a Fragment.

thank you very much, it's very help to me :)