Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView "cannot resolve symbol" errors - Android Studio

I am getting cannot resolve symbol errors on all my RecyclerView's. What is going on? Because I have an error with RecyclerView, I also have errors on LayoutManager. My last four Override statements are in the wrong place and I don't know where they go. I am a beginner and have a very basic knowledge of programming so I don't know how to fix this. I am taking a class but the professor isn't helpful at all.

package com.bignerdranch.android.criminalintent;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;


public class CrimeListFragment extends Fragment {

    private RecyclerView mCrimeRecyclerView;
    private CrimeAdapter mAdapter;

    @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;
    }

    @Override
    public void onResume() {
        super.onResume();
        updateUI();
    }

    private void updateUI(){
        CrimeLab crimeLab = CrimeLab.get(getActivity());
        List<Crime> crimes = crimeLab.getCrimes();

        if (mAdapter == null) {
            mAdapter = new CrimeAdapter(crimes);
            mCrimeRecyclerView.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
    }
        private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder> {
            private List<Crime> mCrimes;
            public CrimeAdapter(List<Crime> crimes) {
                mCrimes = crimes;
            }
        }
        private class CrimeHolder extends RecyclerView.ViewHolder
                implements View.OnClickListener {
            private TextView mTitleTextView;
            private TextView mDateTextView;
            private CheckBox mSolvedCheckBox;
            private Crime mCrime;

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

                mTitleTextView = (TextView)
                        itemView.findViewById(R.id.list_item_crime_title_text_view);
                mDateTextView = (TextView)
                        itemView.findViewById(R.id.list_item_crime_date_text_view);
                mSolvedCheckBox = (CheckBox)
                        itemView.findViewById(R.id.list_item_crime_solved_check_box);
            }

            public void bindCrime(Crime crime) {
                mCrime = crime;
                mTitleTextView.setText(mCrime.getTitle());
                mDateTextView.setText(mCrime.getDate().toString());
                mSolvedCheckBox.setChecked(mCrime.isSolved());
            }

            @Override
            public CrimeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
                View view = layoutInflater.inflate(R.layout.list_item_crime, parent, false);
                return new CrimeHolder(view);
            }

            @Override
            public void onBindViewHolder(CrimeHolder holder, int position) {
                Crime crime = mCrimes.get(position);
                holder.bindCrime(crime);
            }

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

            @Override
            public void onClick(View v) {
                Intent intent = CrimeActivity.newIntent(getActivity(), mCrime.getId());
                startActivity(intent);
            }
        }
}
like image 493
rpach17 Avatar asked Sep 21 '16 14:09

rpach17


1 Answers

These are your import statements,

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;

Looks like you haven't imported RecyclerView, LayoutManager and anything which you are using inbuilt in Android. You just need to import the classes that you are seeing red lines under.

How to Import?

Press alt + Enter on windows, or alt + return on mac to import. You should have your cursor at the end of class.

If you haven't imported library, then add this to build.gradle file under dependancies.

compile 'com.android.support:recyclerview-v7:21.0.+' 

Update :

Latest recycler library is this :

  1. With latest gradle version. 3.0 or above.

     implementation 'com.android.support:recyclerview-v7:28.0.0'
    
  2. with old gradle version

     compile 'com.android.support:recyclerview-v7:28.0.0'
    

Update: latest Androidx Jetpack dependencies

dependencies {
    implementation "androidx.recyclerview:recyclerview:1.1.0"
    // For control over item selection of both touch and mouse driven selection
    implementation "androidx.recyclerview:recyclerview-selection:1.1.0"
}

source : https://developer.android.com/jetpack/androidx/releases/recyclerview

like image 102
Ajay P. Prajapati Avatar answered Sep 21 '22 05:09

Ajay P. Prajapati