ListView - 自定义适配器 - CursorIndexOutOfBound异常

时间:2013-03-17 03:35:33

标签: android android-listview custom-adapter

Android 2.3.3

我正在使用自定义适配器来填充列表视图。列表视图中的每一行都有3个文本视图(自定义布局),这些视图是从数据库中检索的数据中填充的。

问题是,第一次正确显示数据。但是当我向下滚动然后再次向上滚动列表视图时,我得到以下异常。

03-17 08:55:37.789: E/AndroidRuntime(574): FATAL EXCEPTION: main
03-17 08:55:37.789: E/AndroidRuntime(574): android.database.CursorIndexOutOfBoundsException: Index 7 requested, with a size of 7
03-17 08:55:37.789: E/AndroidRuntime(574):  at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
03-17 08:55:37.789: E/AndroidRuntime(574):  at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
03-17 08:55:37.789: E/AndroidRuntime(574):  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
03-17 08:55:37.789: E/AndroidRuntime(574):  at com.mobilevoiceapps.advancedvoicecalculatorfree.History$MyCustomAdapter.getView(History.java:111)
03-17 08:55:37.789: E/AndroidRuntime(574):  at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
03-17 08:55:37.789: E/AndroidRuntime(574):  at android.widget.AbsListView.obtainView(AbsListView.java:1588)
03-17 08:55:37.789: E/AndroidRuntime(574):  at android.widget.ListView.makeAndAddView(ListView.java:1772)
03-17 08:55:37.789: E/AndroidRuntime(574):  at android.widget.ListView.fillDown(ListView.java:695)
03-17 08:55:37.789: E/AndroidRuntime(574):  at android.widget.ListView.correctTooLow(ListView.java:1445)
03-17 08:55:37.789: E/AndroidRuntime(574):  at android.widget.ListView.fillGap(ListView.java:672)
03-17 08:55:37.789: E/AndroidRuntime(574):  at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4176)
03-17 08:55:37.789: E/AndroidRuntime(574):  at android.widget.AbsListView.onTouchEvent(AbsListView.java:2609)
03-17 08:55:37.789: E/AndroidRuntime(574):  at android.widget.ListView.onTouchEvent(ListView.java:3677)
03-17 08:55:37.789: E/AndroidRuntime(574):  at android.view.View.dispatchTouchEvent(View.java:3885)
03-17 08:55:37.789: E/AndroidRuntime(574):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
03-17 08:55:37.789: E/AndroidRuntime(574):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
03-17 08:55:37.789: E/AndroidRuntime(574):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
03-17 08:55:37.789: E/AndroidRuntime(574):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
03-17 08:55:37.789: E/AndroidRuntime(574):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1750)
03-17 08:55:37.789: E/AndroidRuntime(574):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1131)
03-17 08:55:37.789: E/AndroidRuntime(574):  at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
03-17 08:55:37.789: E/AndroidRuntime(574):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1734)
03-17 08:55:37.789: E/AndroidRuntime(574):  at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2200)
03-17 08:55:37.789: E/AndroidRuntime(574):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1884)
03-17 08:55:37.789: E/AndroidRuntime(574):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-17 08:55:37.789: E/AndroidRuntime(574):  at android.os.Looper.loop(Looper.java:130)
03-17 08:55:37.789: E/AndroidRuntime(574):  at android.app.ActivityThread.main(ActivityThread.java:3687)
03-17 08:55:37.789: E/AndroidRuntime(574):  at java.lang.reflect.Method.invokeNative(Native Method)
03-17 08:55:37.789: E/AndroidRuntime(574):  at java.lang.reflect.Method.invoke(Method.java:507)
03-17 08:55:37.789: E/AndroidRuntime(574):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
03-17 08:55:37.789: E/AndroidRuntime(574):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
03-17 08:55:37.789: E/AndroidRuntime(574):  at dalvik.system.NativeStart.main(Native Method)

以下是我正在使用的代码......

从数据库中检索..     public Cursor retrieve_history()         {

        c = db.rawQuery("select * from history", null);
        return c;
    }

自定义布局 - ListView行

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp" >

    <TextView
        android:id="@+id/txtView_History_Count"
        android:layout_width="0dip"
        android:layout_height="50dp"
        android:layout_weight="0.20"
        android:text="Number"
        android:textColor="#FFFFFF"
        android:textSize="22dp"
        android:paddingLeft="5dp"
        android:gravity="center" />

    <LinearLayout
        android:id="@+id/ll2"
        android:layout_width="0dip"
        android:layout_height="50dp"
        android:layout_weight="0.80"
        android:orientation="vertical"
        android:paddingLeft="5dp" >

        <TextView
            android:id="@+id/txtView_History_Result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Result"
            android:textColor="#316DA2"
            android:textSize="22dp" />

        <TextView
            android:id="@+id/txtView_History_Expression"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Expression"
            android:textColor="#FFFFFF"
            android:textSize="15dp" />
    </LinearLayout>

</LinearLayout>

自定义适配器

class MyCustomAdapter extends BaseAdapter{

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return c.getCount();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        LayoutInflater li = getLayoutInflater();
        convertView =  li.inflate(R.layout.history_row, null);

        TextView cnt = (TextView) convertView.findViewById(R.id.txtView_History_Count);
        TextView exp = (TextView) convertView.findViewById(R.id.txtView_History_Expression);
        TextView res = (TextView) convertView.findViewById(R.id.txtView_History_Result);

                cnt.setText(String.valueOf(count));     
                System.out.println(cnt.getText().toString());

                expression = c.getString(c.getColumnIndex("expression"));
                result = c.getString(c.getColumnIndex("result"));

                if(!c.isAfterLast())
                {
                    c.moveToNext();
                    count++;
                    //c.moveToFirst();
                }               


                exp.setText(expression);
                System.out.println(exp.getText().toString());
                res.setText(result);
                System.out.println(res.getText().toString());


        return convertView;
    }

}

提前致谢!!!

1 个答案:

答案 0 :(得分:0)

尝试使用

 c.moveToPosition(int position);// Move the cursor to an absolute position.
在getview方法中

,以便您可以正确设置listview行中的数据..