无法读取第0行 - 光标

时间:2013-05-09 17:26:39

标签: android arraylist cursor

在我的应用程序中,我想使用CustomAdapter设置ListView的布局。

这是我的错误日志:

05-09 19:17:50.187: E/AndroidRuntime(32063): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.unserekinder/com.example.unserekinder.Ereignisse}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
05-09 19:17:50.187: E/AndroidRuntime(32063):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-09 19:17:50.187: E/AndroidRuntime(32063):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-09 19:17:50.187: E/AndroidRuntime(32063):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-09 19:17:50.187: E/AndroidRuntime(32063):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-09 19:17:50.187: E/AndroidRuntime(32063):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 19:17:50.187: E/AndroidRuntime(32063):    at android.os.Looper.loop(Looper.java:137)
05-09 19:17:50.187: E/AndroidRuntime(32063):    at android.app.ActivityThread.main(ActivityThread.java:5041)
05-09 19:17:50.187: E/AndroidRuntime(32063):    at java.lang.reflect.Method.invokeNative(Native Method)
05-09 19:17:50.187: E/AndroidRuntime(32063):    at java.lang.reflect.Method.invoke(Method.java:511)
05-09 19:17:50.187: E/AndroidRuntime(32063):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-09 19:17:50.187: E/AndroidRuntime(32063):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-09 19:17:50.187: E/AndroidRuntime(32063):    at dalvik.system.NativeStart.main(Native Method)
05-09 19:17:50.187: E/AndroidRuntime(32063): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
05-09 19:17:50.187: E/AndroidRuntime(32063):    at android.database.CursorWindow.nativeGetString(Native Method)
05-09 19:17:50.187: E/AndroidRuntime(32063):    at android.database.CursorWindow.getString(CursorWindow.java:434)
05-09 19:17:50.187: E/AndroidRuntime(32063):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
05-09 19:17:50.187: E/AndroidRuntime(32063):    at com.example.unserekinder.Ereignisse.datenfuerlistviewereignis(Ereignisse.java:51)
05-09 19:17:50.187: E/AndroidRuntime(32063):    at com.example.unserekinder.Ereignisse.onCreate(Ereignisse.java:29)
05-09 19:17:50.187: E/AndroidRuntime(32063):    at android.app.Activity.performCreate(Activity.java:5104)
05-09 19:17:50.187: E/AndroidRuntime(32063):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-09 19:17:50.187: E/AndroidRuntime(32063):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-09 19:17:50.187: E/AndroidRuntime(32063):    ... 11 more

这是我的CustomAdapter,名为EreignislistAdapter:

public class EreignislistAdapter extends BaseAdapter 
{
    private final ArrayList<Ereignis> values;
    private final LayoutInflater inflator;

    public EreignislistAdapter (Context context, ArrayList<Ereignis> values){
        this.inflator = LayoutInflater.from(context);
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolderEreignis holder;

        if(convertView == null){
            convertView = inflator.inflate(R.layout.list_ereignis, parent, false);
            holder = new ViewHolderEreignis((TextView) convertView.findViewById(R.id.enullline), (TextView) convertView.findViewById(R.id.efirstLine));

            convertView.setTag(holder);
        }
        else{
            holder = (ViewHolderEreignis) convertView.getTag();
        }

        Ereignis ki = (Ereignis) getItem(position);
        holder.getEreignisname().setText(ki.getEreignisname());
        holder.getEreignisdatum().setText(ki.getEreignisZeit());


        return convertView;

    }

    @Override
    public int getCount() {
        return values.size();
    }


    @Override
    public Object getItem(int position) {
        return values.get(position);
    }


    @Override
    public long getItemId(int position) {
        return position;
    }



}

class ViewHolderEreignis {
    private TextView ereignisname = null; 
    private TextView ereignisdatum = null; 



    public ViewHolderEreignis(TextView ereignisname, TextView ereignisdatum) {
        this.ereignisname = ereignisname; 
        this.ereignisdatum = ereignisdatum; 

    }

    public TextView getEreignisname() {
        return this.ereignisname; 
    }
    public TextView getEreignisdatum(){
        return this.ereignisdatum;
    }    
}

这是我的DBHelper声明

public static final String ereignisid = "id";
    public static final String TABLE_NAME_EREIGNIS="ereignis";
    public static final String EREIGNISNAME = "ereignisname";
    public static final String EREIGNISZEIT = "ereigniszeit";
    public static final String EREIGNISGENAUERES = "ereignisgenaueres";
    public static final String KINDID = "kindid";

    private static final String TABLE_EREIGNIS_CREATE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME_EREIGNIS + " (" + ereignisid + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + EREIGNISNAME + " TEXT, " + EREIGNISZEIT + " TEXT, " + EREIGNISGENAUERES + " TEXT, " + KINDID + " INTEGER, FOREIGN KEY (" + KINDID + ") REFERENCES " + TABLE_NAME_Kind + "(" + id + "));";

我的适配器被调用如下:

    final ListView lv = (ListView) findViewById(R.id.lvEreignisse);
            ArrayList<Ereignis> kisi = datenfuerlistviewereignis();
            EreignislistAdapter kiadapter = new EreignislistAdapter(this, kisi);
            lv.setAdapter(kiadapter);



Here is `datenfuerlistviewereignis`:

private ArrayList<Ereignis> datenfuerlistviewereignis(){
        DBHelper db = new DBHelper(this);
        ArrayList<Ereignis> arr = new ArrayList<Ereignis>();
        ListView lv = (ListView) findViewById(R.id.lvEreignisse);
        id = getIntent().getIntExtra("kinderid", 0);
        Cursor c = db.selectEreignis(id);
        int count = c.getCount();
        System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS" +count);

        int i = 0;

        while(c.moveToNext())
        { 

            arr.add(new Ereignis(c.getString(c.getColumnIndex("ereigniszeit")), c.getString(c.getColumnIndex("ereignisname")) ));


            i++;
        }
        if(arr.size() > 0){
            return arr;
        }
        else{
            ArrayList<Ereignis> arrempty = new ArrayList<Ereignis>();
            arrempty.add(new Ereignis("Keine Daten vorhanden", " "));
            return arrempty;
        }
    }

所以,我知道当我尝试将Ereignis添加到ArrayList时,一定会出现问题。我也认为与create语句有联系,但我找不到我的错误。 有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

致电

c.moveToFirst();

开始使用光标

之前