App crashes on trying to access elements from the cursor in onBindViewHolder()

40 Views Asked by At

Here is my code

    @Override
    public void onBindViewHolder(@NonNull MyRecyclerViewForFav.MyViewHolder holder, int position) {
        data d = new data();

//        if(  cursor.moveToNext()  )
       // Log.d("this2",);
        cursor.moveToNext();
        Log.d("this2",cursor.getInt(1)+"");
        
        String temp = d.Convert(str, position);
        holder.textView.setText(temp);
        holder.rowNumber.setText((position + 1) + ".");
    }

if i print the size of cursor using getCount() it works fine but when trying to access the elements app crashes

1

There are 1 best solutions below

0
Peppe L-G On

Having cursor.moveToNext() in onBindViewHolder() does not make sense. It would only work if the user scrolled down, it won't work when the user scrolls up. Rather use moveToPosition(position).