0

i have this code for the recycler view adapter

public class RvAdapter  extends RecyclerView.Adapter<RvAdapter.CardHolder> {
@Override
public CardHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_item,parent,false);
    return new CardHolder(v);
}

@Override
public void onBindViewHolder(CardHolder holder, int position) {

}

@Override
public int getItemCount() {
    return 10;
}

public class CardHolder extends RecyclerView.ViewHolder {
    ImageView bg;
    public CardHolder(View itemView) {
        super(itemView);
        bg=(ImageView)itemView.findViewById(R.id.rvItemImage);
    }
}

}

and in the MainActivity

// the recycle view
    rv=(RecyclerView)findViewById(R.id.mainBgRv);
    adapter=new RvAdapter();
    RecyclerView.LayoutManager linearLayoutManager=new LinearLayoutManager(MainActivity.this,LinearLayoutManager.HORIZONTAL,false);
    try{
        rv.setLayoutManager(linearLayoutManager);
        rv.setAdapter(adapter);

    }catch (Exception e){
        Log.i("ERROR_TAG",e.toString());
    }
    //

when i'm using the the layout manager the app stops and not throwing any exception ... when i remove it its just not showing any item from the rv

1 Answers1

0

Your adapter is not doing everything what it should. See this post here:

Simple Android RecyclerView example where you will find an example of Adapter.

The thing is that you have to pass a list of items inside of Adapter and in the method onBindViewHolder you have to run a code, that will fill the ViewHolder with data.

In other words - RecyclerViewAdapter has two roles - it creates item views (ViewHolder pattern) and fills them with data as necessary.

You forgot about that second thing.

muminers
  • 1,190
  • 10
  • 19