8.16 Setting up the view for CrimeListFragment

I’m getting a cannot resolve symbol RecyclerView error. When I press (alt + enter) there are a list of options but the text never instructed me to choose either. My question is am I missing something in my code. I did the fragment_crime_list.xml correctly. This is what I have up to this point, although I will continue on with the code hoping things will come together.

package com.bignerdranch.android.criminalintent;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**

  • Created by JB on 12/12/2017.
    */

public class CrimeListFragment extends Fragment
{
/Modify CrimeListFragment to use this layout file and to find the
RecyclerView in the layout file.
------------------------------------------------------------------
Listing 8.16 - Setting up the view for CrimeListFragment
/
private RecyclerView mCrimeRecyclerView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.fragment_crime_list, container, false);
    mCrimeRecyclerView = (RecyclerView) view.findViewById(R.id.crime_recycler_view);
    mCrimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    return view;
}//end of onCreate

//Listing 8.17 the beginnings of a ViewHolder
private class CrimeHolder extends RecyclerView.ViewHolder
{
    public CrimeHolder(LayoutInflater inflater, ViewGroup parent)
    {
        super(inflater.inflate(R.layout.list_item_crime, parent, false));
    }//end of CrimeHolder()method
}//end of viewHolder

}//End of CrimeListFragment Class

A better look at my entire CrimeListFragment.Java code.

package com.bignerdranch.android.criminalintent;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**

  • Created by JB on 12/12/2017.
    */

public class CrimeListFragment extends Fragment
{
/Modify CrimeListFragment to use this layout file and to find the
RecyclerView in the layout file.
------------------------------------------------------------------
Listing 8.16 - Setting up the view for CrimeListFragment
/
private RecyclerView mCrimeRecyclerView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.fragment_crime_list, container, false);
    mCrimeRecyclerView = (RecyclerView) view.findViewById(R.id.crime_recycler_view);
    mCrimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    return view;
}//end of onCreate

//Listing 8.17 the beginnings of a ViewHolder
private class CrimeHolder extends RecyclerView.ViewHolder
{
    public CrimeHolder(LayoutInflater inflater, ViewGroup parent)
    {
        super(inflater.inflate(R.layout.list_item_crime, parent, false));
    }//end of CrimeHolder()method
}//end of viewHolder

//8.18 the beginnings of an adapter
private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder>
{}//incomplete

}//End of CrimeListFragment Class

I figured it out. I had to get the dependency correct. I thought I selected it the first time but never highlighted it and selected ok.

if you get a error for the RecyclerView. I think, you can check first the gradle you added the recycleview in project. And second is that, you should check the layout in the xml file. You must sure added correctly to recyclerview. I think, you don’t get error after you completed two condition. If you get, you should compare verison of recyclerview with the completeSdk version in gradle.

1 Like