如何根据位置查询以根据位置显示“编辑视图”

时间:2015-02-26 13:11:28

标签: android mysql

我正在尝试构建一个具有基本CRUD操作的应用程序。我能够显示详细视图w.r.t从列表视图中选择的项目的位置。但是当我尝试从详细信息视图显示编辑视图时,用于显示详细信息视图的相同查询产生异常。所以我试图读取所有列并按位置显示它们。

我使用下面显示的查询来显示编辑视图。但是应用程序只显示表格第一行的内容,而不管点击的位置如何:

我的查询是:

db = helper.getReadableDatabase();

        String[] columns = { SQLiteHelper.UID,
                SQLiteHelper.TASK_NAME, SQLiteHelper.CONTACTS, SQLiteHelper.DESCRIPTION, SQLiteHelper.REMARKS, SQLiteHelper.DATETIME, SQLiteHelper.ESTIMATED_COMPLETION_TIME,
                SQLiteHelper.ACTUAL_COMPLETION_TIME, SQLiteHelper.NOTIFY_TIME};
        Cursor cursor = db.query(SQLiteHelper.TABLE_NAME,columns , null  ,
                null, null, null, null, null);

        if (cursor != null) {

            cursor.moveToFirst();


            task = new Task(cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5),
                    cursor.getString(6), cursor.getString(7), cursor.getString(8));



        }


        return task; 

请帮助我,并告诉我如何更新我的查询以实现我的目标。

当我在列表视图上单击Task5时,它会显示Task5的详细信息,但是当我选择Edit按钮时,我将获得第一行的Task1的Edit视图。请帮我。我是Android新手。请教我。

MY ADB记录评论中提到的错误:

02-26 18:59:51.985    1504-1504/com.ms.t.tms D/AndroidRuntime﹕ Shutting down VM
02-26 18:59:52.000    1504-1504/com.ms.t.tms E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.ms.t.tms, PID: 1504
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ms.t.tms/com.ms.t.tms.EditScreen}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
            at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
            at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
            at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
            at com.ms.t.tms.SQLiteDataBaseAdapter.getTask1(SQLiteDataBaseAdapter.java:146)
            at com.ms.t.tms.EditScreen.onCreate(EditScreen.java:70)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我的表结构:

public static final String TABLE_NAME = "DataTable";
    public static final String UID = "_id";
    public static final String TASK_NAME = "Task_name";
    public static final String CONTACTS = "Contacts";
    public static final String DESCRIPTION = "Description";
    public static final String REMARKS = "Remarks";
    public static final String DATETIME = "Date";
    public static final String ESTIMATED_COMPLETION_TIME = "EstCompTime";
    public static final String ACTUAL_COMPLETION_TIME = "ActCompTime";
    public static final String NOTIFY_TIME = "NotifyTime";
    public static final int DATABASE_VERSION = 2;
    public static final String CREATE_TABLE = " CREATE TABLE " + TABLE_NAME +
            "(" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + TASK_NAME + " VARCHAR(250)," + CONTACTS + " VARCHAR(250)," + DESCRIPTION + " VARCHAR(250), " + REMARKS + " VARCHAR(250),"
            + DATETIME + " VARCHAR(250)," + ESTIMATED_COMPLETION_TIME + " VARCHAR(250), "
            + ACTUAL_COMPLETION_TIME + " VARCHAR(250), " + NOTIFY_TIME + " VARCHAR(250));";

2 个答案:

答案 0 :(得分:0)

您可以使用Raw查询! 游标c = db.rawQuery(“SELECT * FROM SOMETHING WHERE name =?”,args);

阅读更多内容 http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

答案 1 :(得分:0)

  1. 首先,您要调用sqlite查询一次并获取所有数据。如果是,那么您必须将所有数据存储在arraylist中。
  2. 然后,您可以在选择项目时使用这些数据。如果您选择的位置是5,那么从数据的arraylist的第5个位置获取数据。

    在位置更改时再次在edittext中设置数据。

    1. 另一个假设, 如果在选择位置时调用sqlite查询,则必须在sqlite查询中进行修改。添加where子句条件。您将获得所选位置的数据,然后在edittext中设置。
    2. 我希望你明白。