RecyclerView Adapter gives error "RecycleAdapter hierarchy is incompatible"

I am trying to add a new one RecyclerViewto my project inside Fragement. I follow the video tutorial on Link1 . When I try to create an adapter for RecyclerView, I start getting this error. I am using Eclipse Juno. I can not understand the problem. Please, help

My code is:

public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.MyViewHolder> {

private LayoutInflater inflator;
List <Item> data = Collections.EMPTY_LIST;

public RecycleAdapter(Context context, List<Item> data){
    inflator = LayoutInflater.from(context);
}

@Override
public int getItemCount() {
    // TODO Auto-generated method stub
    return data.size();
}


@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int arg1) {
    View view = inflator.inflate(R.layout.row_staggered_new, parent, false);
    MyViewHolder holder = MyViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Item current = data.get(position);
    holder.text.setText(current.title);
    holder.image.setImageResource(current.image);

}

public static class MyViewHolder extends RecyclerView.ViewHolder{

    public ImageView image;
    public TextView text;

    public MyViewHolder(View itemView) {
        super(itemView);
        image = (ImageView) itemView.findViewById(R.id.imageView1);
        text = (TextView) itemView.findViewById(R.id.textview1);
    }
}
}

Both MyViewHolderand RecycleAdapterwill generate an error "The hierarchy of the type RecycleAdapter is inconsistent". The extended part RecycleAdapterdisplays an error "Bound mismatch: The type RecycleAdapter.MyViewHolder is not a valid substitute for the bounded parameter <VH extends RecyclerView.ViewHolder> of the type RecyclerView.Adapter<VH>".

Any pointers?

+4
source share
4 answers

. , v4, android.support.v4.view.ScrollingView. v4 , !

+10

. , v4, android.support.v4.view.ScrollingView. v4 , !

, :

Eclipse "appcompat_v7", libs.

+1

onBindViewHolder RecyclerView.ViewHolder MyViewHolder, .

,

public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
    MyViewHolder holder = (MyViewHolder)viewHolder;
    ...
}

, , , , . :

extends RecyclerView.Adapter<RecyclerView.ViewHolder>
0
source

another solution:

  • Delete jar file from your library.
  • copy android-support-v7-appcompatto workspace from sdk -> extra -> android -> support-v7 -> appcompat
  • you will find v4 library in appcompat
  • Right click on appcompat and mark it as a library if it is not
  • Now right click on your project, go to java build path, add appcompat as a library.

enjoy

0
source

Source: https://habr.com/ru/post/1584086/


All Articles