0
votes

Am trying to create a recycler view that show a picture under it's name as stories in instagram.. i have finished everything and still the code doesnt run and tell me an error at com.example.recyclerview.Adapter.onBindViewHolder(Adapter.java:36) at com.example.recyclerview.Adapter.onBindViewHolder(Adapter.java:14)

public class Adapter extends RecyclerView.Adapter{

List<Story> storiesList;

public Adapter(List<Story> itemsList) {
    this.storiesList = itemsList;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

 View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main, parent, false);
    return new ViewHolder(view) ;

}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {

    Story story = storiesList.get(position);
    Log.d("street",story.getName());
    viewHolder.name.setText(story.getName());


}

@Override
public int getItemCount() {
    if (storiesList == null)return 0;
    return storiesList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    TextView name;
    ImageView sora;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        name = (TextView) itemView.findViewById(R.id.nameofuser);
        sora = itemView.findViewById(R.id.imageN);


    }
}

}

1
do your activity_main contains that TextView? - Saurav Kumar

1 Answers

0
votes

I am not sure, but it seems you want to inflate wrong layout in your adapter

LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main, parent, false);

You are trying to inflate R.layout.activity_main when, I suppose, there should be item view layout something like R.layout.item_view

I may be wrong though because I cannot see your R.layout.activity_main.

Hope it helps.