RecyclerView does not display the second inserted element

advertisements

I am using RecyclerView and using a custom row which has only one textView , on first call to the Adapter the textview is set properly in onBindViewHolder , on second insertion onBindViewHolder gets called succesfully and the text to be set also is available but when i setText on textview , it shows a blank line on recyclerview. it will take the space of the text but does not display the text, same thing happens on further insertions, any idea why is this happening?

public class MessagesAdapter extends RecyclerView.Adapter<MessagesAdapter.ViewHolder> {
private ArrayList<MessagesData> mDataset;
private Context context;
public MessagesAdapter(Context context, ArrayList<MessagesData> myDataset) {
    this.mDataset=myDataset;
    this.context=context;
}

public class ViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    public TextView textViewMsg;
    public TextView textViewDay;
    public TextView textViewTime;

    public ViewHolder(View view) {
        super(view);
        textViewMsg = (TextView) view.findViewById(R.id.mes);
    }
}

// Create new views (invoked by the layout manager)
@Override
public MessagesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                  int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.mesage, parent, false);
    // set the view's size, margins, paddings and layout parameters

    ViewHolder vh = new ViewHolder(v);
    return vh;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    Log.d("here",mDataset.get(position).getMessage());
    holder.textViewMsg.setText(mDataset.get(position).getMessage());

}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
    return mDataset.size();

}
}

The Log in onBindViewHolder shows correct result on each insertion, but just does not set the text from second insertion.

In this Image ,Hi how are you was the first message i sent after that i sent many three messages hence the blank space below the first message


Try to use notifyDataSetChanged() on your adapter after inserting a new item to the RecyclerView.