Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

RecyclerView "cannot resolve symbol" errors - Android Studio

Writer Sophia Terry

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); } }
}
4

6 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 :

6

For the latest version of Android Studio 3:

Find your gradle file under Gradle Scripts, build.gradle (Module)

Add dependency:

dependencies {
... implementation 'com.android.support:cardview-v7:27.0.+' implementation 'com.android.support:recyclerview-v7:27.0.+'
}

Sync your gradle file (File / Sync Project with gradle files).

Go back to your code and hit ALT+Enter on the missing reference.

That should be it.

After updating Gradle to 4.4

I find that my import is complaining about the RecyclerView

it turns out that I no longer need to import separate RecyclerView dependency

Previously:

compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:design:26.1.0'
compile 'com.android.support:recyclerview-v7:26.1.0'

After update:

implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:design:26.1.0'

On Android Studio 3.2.1, build on October 8 2018, for a new project, all I need is to add

implementation "com.android.support:design:$support_version"

in app build.gradle. The support_version is the same as the one in appcompat-v7 generated by New Project wizard.

This solution works:

  1. Add the dependency
  2. Clean and rebuild the project

Back to main-activity in red error press alt+enter android suggest to import library and it works. This was not there before clean and rebuild the project.

For API 28 or higher:

import androidx.recyclerview.widget.RecyclerView;

Maybe this helps someone.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy