Search code examples
androidlistviewsimplecursoradapterandroid-cursoradapterandroid-cursor

swapCursor() causes CursorIndexOutOfBoundsException, behaves abnormally


I got this problem several days ago, I'm working on a program where a ListView takes data from a database via a custom SimpleCursorAdapter, I used to use listView.setAdapter(adapter)to update the data in the ListView every time the data is changed. But that causes a problem, that everytime you update the data, the listView will automatically scroll to the top, so I decided to use swapCursor() instead, like this:

Cursor cursor = database.getData();
    if(listView.getAdapter() == null){
        //Toast.makeText(getApplicationContext(),"null",Toast.LENGTH_SHORT).show();
        adapter = (new CustomSimpleCursorAdapter(this,R.layout.todolist,cursor,new String[] {LIST_TEXT},new int[]{R.id.text}));
        listView.setAdapter(adapter);
    }else {
        Runnable runnable =new Runnable() {
            @Override
            public void run() {
                Cursor finalCs = database.getData();
                todoListAdapter.swapCursor(finalCs);
                todoListAdapter.notifyDataSetChanged();
            }
        };
        runOnUiThread(runnable);
    }

But then the error appears every time I try to add a data to the database and display it in the listView:

**CursorIndexOutOfBoundsException: Index 12 requested, with a size of 12**

(Note that the data is added to the database, and no problem has been found previously using listView.setAdapter(adapter), I tried to remove todoListAdapter.notifyDataSetChanged();, but that didn't help at all. Also, when I try to remove data from the database and also update the listView with the change, it's always the last item in the listView got removed no matter which one I selected (the data by now is actually correctly removed from the database), and when I try to add data again, I can now add the equal amount of data like the previously deleted data amount(if I removed 4 item, then I can only add 4 item after that or the error will appear). Anyways, swapCursor()seems pretty broken as of my use of it, did I do something wrong? or I need something else for swapCursor() to work? Thanks in advance!

Update: Error traceback context:

 android.database.CursorIndexOutOfBoundsException: Index 13 requested, with a size of 13
                                                                   at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
                                                                   at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
                                                                   at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
                                                                   at android.support.v4.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:139)

Update: Custom SimpleCursorAdapter:

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import java.util.ArrayList;
public class TodoListAdapter extends SimpleCursorAdapter {
private Cursor c;
private Context context;
private ArrayList<Long> itemChecked = new ArrayList<>();
protected int[] mFrom;
protected int[] mTo;

LayoutInflater inflater;
private int mStringConversionColumn = -1;
private CursorToStringConverter mCursorToStringConverter;
private ViewBinder mViewBinder;
MainActivity main;
String[] mOriginalFrom;

public TodoListAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
    super(context, layout, c, from, to,1);
    this.c = c;
    this.context = context;
    mTo = to;
    mOriginalFrom = from;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public ViewBinder getViewBinder() {
    return mViewBinder;
}

public ArrayList returnSelected(){
    return itemChecked;
}

public interface MyInterface{
    public void foo();
}

public void setViewBinder(ViewBinder viewBinder) {
    mViewBinder = viewBinder;
}

public View getView(final int pos, View inView, ViewGroup parent) {
    c.moveToPosition(pos);
    if (inView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inView = inflater.inflate(R.layout.todolist, null);
    }
    final TextView todoText = (TextView) inView.findViewById(R.id.titleText);
    final CheckBox cBox = (CheckBox) inView.findViewById(R.id.multiSelectionBox); 
    System.out.println(c.getCount());
    newView(context,c,parent);
    bindView(inView,context,c);
    final long id = getItemId(pos);
    cBox.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            CheckBox cb = (CheckBox) v.findViewById(R.id.multiSelectionBox);
            if (cb.isChecked()) {
                if(context.toString().contains("MainActivity")){
                    ((MainActivity)context).addSelectedId(id);
                }else if(context.toString().contains("HistoryActivity")){
                    ((HistoryActivity)context).addSelectedId(id);
                }
                System.out.println("checked " + id);
            } else if (!cb.isChecked()) {
                System.out.println("unchecked " + id);
                if(context.toString().contains("MainActivity")){
                    ((MainActivity)context).removeSelectedId(id);
                }else if(context.toString().contains("HistoryActivity")){
                    ((HistoryActivity)context).removeSelectedId(id);
                }                    
            }
        }
    });
    return inView;
}

}


Solution

  • I finally got it, it's simply because I didn't change the cursor in the adapter to the passed in cursor, override bindView method and add this.c = cursor into the original code, then when calling getView, the cursor will be the new cursor. That's just a stupid mistake I made by myself.

    Special thanks to @pskink for helping me to realize that, I don't need to override bindView or newView after all, but you mentioned the c.moveToPosition() made me realize the mistake.