Why can't I call the bind() method?


#1

I want to ask the teacher why the onBindViewHolder() method that I automatically generate through Android Studio is different from the one in the book.It gave me an error.Take a look at my screenshot.I try to change it to the same code as the book, but @override turns red.I don’t know what to do?

  • Here’s my full code

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import static android.support.v7.widget.RecyclerView.*;

public class CrimeListFragment extends Fragment {

private RecyclerView mCrimeRecyclerView;
private CrimeAdapter mAdapter;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_crime_list, container, false);

    mCrimeRecyclerView = (RecyclerView) view.findViewById(R.id.crime_recycle_view);
    mCrimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    updateUI();

    return view;
}

private void updateUI() {
    CrimeLab crimeLab = CrimeLab.get(getActivity());
    List cirmes = crimeLab.getCrimes();

    mAdapter = new CrimeAdapter(cirmes);
    mCrimeRecyclerView.setAdapter(mAdapter);
}

private class CrimeHolder extends ViewHolder {

    private TextView mTitleTextView;
    private TextView mDateTextView;
    private Crime mCrime;

    public CrimeHolder(LayoutInflater inflater, ViewGroup parent) {
        super(inflater.inflate(R.layout.list_item_crime, parent, false));

        mTitleTextView = (TextView) itemView.findViewById(R.id.crime_title);
        mDateTextView = (TextView) itemView.findViewById(R.id.crime_date);
    }

    public void bind(Crime crime) {
        mCrime = crime;
        mTitleTextView.setText(mCrime.getTitle());
        mDateTextView.setText(mCrime.getDate().toString());
    }
}

private class CrimeAdapter extends Adapter {

    private List mCrimes;

    public CrimeAdapter(List crimes) {
        mCrimes = crimes;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());

        return new CrimeHolder(layoutInflater, parent);
    }

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

    /*
     * That's what it says in the book, but it's still wrong for me to copy it.
     *    
     */

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

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

}


#2
private class CrimeAdapter extends Adapter

should be

private class CrimeAdapter extends Adapter<CrimeHolder>

By the way, another suggestion is removing the line

import static android.support.v7.widget.RecyclerView.*;

and using

import android.support.v7.widget.RecyclerView;

RecyclerView.ViewHolder
RecyclerView.Adapter

instead