使用Intent从SQLite数据库传递数据

时间:2013-01-29 12:17:54

标签: android database sqlite listview android-intent

在我的应用程序中,我有一个SQLite数据库,我想知道如何将一些数据(如id,title等)传输到另一个活动。

到目前为止,这是我的代码:

ListView lv = getListView();

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() 
{
    public void onItemClick(AdapterView<?> av, View v, int pos, long id) 
    {
        onListItemClick(v,pos,id);
    }
    protected void onListItemClick(View v, int pos, long id) 
    {
        Intent i = new Intent(context, Table.class);
        i.putExtra("Value1", c.getString(c.getColumnIndex("title")));
        startActivity(i); 

    }
});

但我收到了错误。

01-29 12:25:46.511: E/AndroidRuntime(26478): FATAL EXCEPTION: main
01-29 12:25:46.511: E/AndroidRuntime(26478): android.database.CursorIndexOutOfBoundsException: Index 50 requested, with a size of 50
01-29 12:25:46.511: E/AndroidRuntime(26478):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
01-29 12:25:46.511: E/AndroidRuntime(26478):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
01-29 12:25:46.511: E/AndroidRuntime(26478):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
01-29 12:25:46.511: E/AndroidRuntime(26478):    at com.example.studentdatabase.MainActivity$2.onListItemClick(MainActivity.java:90)
01-29 12:25:46.511: E/AndroidRuntime(26478):    at com.example.studentdatabase.MainActivity$2.onItemClick(MainActivity.java:87)
01-29 12:25:46.511: E/AndroidRuntime(26478):    at android.widget.AdapterView.performItemClick(AdapterView.java:284)
01-29 12:25:46.511: E/AndroidRuntime(26478):    at android.widget.ListView.performItemClick(ListView.java:3513)
01-29 12:25:46.511: E/AndroidRuntime(26478):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
01-29 12:25:46.511: E/AndroidRuntime(26478):    at android.os.Handler.handleCallback(Handler.java:587)
01-29 12:25:46.511: E/AndroidRuntime(26478):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-29 12:25:46.511: E/AndroidRuntime(26478):    at android.os.Looper.loop(Looper.java:123)
01-29 12:25:46.511: E/AndroidRuntime(26478):    at android.app.ActivityThread.main(ActivityThread.java:3683)
01-29 12:25:46.511: E/AndroidRuntime(26478):    at java.lang.reflect.Method.invokeNative(Native Method)
01-29 12:25:46.511: E/AndroidRuntime(26478):    at java.lang.reflect.Method.invoke(Method.java:507)
01-29 12:25:46.511: E/AndroidRuntime(26478):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-29 12:25:46.511: E/AndroidRuntime(26478):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-29 12:25:46.511: E/AndroidRuntime(26478):    at dalvik.system.NativeStart.main(Native Method)

请帮忙, 感谢。

2 个答案:

答案 0 :(得分:0)

你提出的代码不是这里的错误。它是光标所指向的位置。在您的Cursor声明之后,您应该调用moveToFirst()。

cursor.moveToFirst()

答案 1 :(得分:0)

你必须指向光标上的位置

cursor.moveToFirst()

还尝试验证游标中的数据以避免进一步的异常。见this