質問

I am presently working with a database that contains three tables, each table populating a ListFragment housed inside of a tab. Each ListFragment is populated with "categories" from the database. ie I have a couple hundred items, but a number of those items belong to a category such as "science" or "history". The ListView groups the repeat items so it only shows one item for each category.

When one of those categories is clicked, I need to requery the database, retrieve the rows from the appropriate table, checking against the "category" column for only the rows that contain "science" or "history" per selected category.

Here is what I have tried. Here is my onListItemClick

    @Override
public void onListItemClick(ListView lv, View view, final int position,
                            long id){
    super.onListItemClick(lv, view, position, id);

    Cursor c = myDB.getRow(position);
    String category = c.getString(c.getColumnIndex(DataBaseHelper.KEY_CATEGORY));

    c = null;
    c = myDB.getRows(category);

    Fragment contentList = new FragmentContentList(parentListGetter(c), childListGetter(c));
    getFragmentManager().beginTransaction()
            .addToBackStack(null)
            .add(android.R.id.content, contentList)
            .commit();

}

public String[] parentListGetter(Cursor c){
    String[] parentValues = new String[]{
            c.getString(c.getColumnIndex(DataBaseHelper.KEY_REF))
    };
    return parentValues;
}

public String[][] childListGetter(Cursor c){
    String[][] childValues = new String[][]{
            {c.getString(c.getColumnIndex(DataBaseHelper.KEY_CONTENT))}
    };
    return childValues;
}

My cursor class to retrieve rows in the DataBaseHelper

    public Cursor getRows(String category){
    String where = KEY_CATEGORY + "=" + category;
    Cursor c = myDataBase.query(true, DB_TABLE, ALL_KEYS,
            where, null, null, null, null, null, null);
    if (c != null)
        c.moveToFirst();
    return c;
}

I'm working to populate the ExpandableListView per https://www.youtube.com/watch?v=ys8bvCDAXxQ I'm open to a better way of there be one for what I am trying to do. Both obtaining a selection of rows based off column content is new to me as well as the ExpandableListView.

Presently, I get the following error when calling getRows.

05-02 09:21:07.225  30967-30967/promises.scriptures.mymodule.app2 E/SQLiteLog﹕ (1) near "Covenant": syntax error
05-02 09:21:07.245  30967-30967/promises.scriptures.mymodule.app2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: promises.scriptures.mymodule.app2, PID: 30967
    android.database.sqlite.SQLiteException: near "Covenant": syntax error (code 1): , while compiling: SELECT DISTINCT _id, category, reference, content FROM blessings WHERE category=Abrahamic Covenant
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java)
            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java)
            at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java)
            at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java)
            at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java)
            at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java)
            at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java)
            at promises.scriptures.mymodule.app2.DataBaseHelper.getRows(DataBaseHelper.java:160)
            at promises.scriptures.mymodule.app2.FragmentCategoryList.onListItemClick(FragmentCategoryList.java:85)
            at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
            at android.widget.AdapterView.performItemClick(AdapterView.java)
            at android.widget.AbsListView.performItemClick(AbsListView.java)
            at android.widget.ListView.performItemClick(ListView.java)
            at android.widget.AbsListView$PerformClick.run(AbsListView.java)
            at android.widget.AbsListView$3.run(AbsListView.java)
            at android.os.Handler.handleCallback(Handler.java)
            at android.os.Handler.dispatchMessage(Handler.java)
            at android.os.Looper.loop(Looper.java)
            at android.app.ActivityThread.main(ActivityThread.java)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
            at dalvik.system.NativeStart.main(Native Method)

FYI, when the row is selected I am not currently getting the correct item returned. ie, I get the position for the grouped listview and but it equates it to the position of the ungrouped listview when calling the category.

Thank you in advance

役に立ちましたか?

解決

Use the built-in parameterized query syntax.

public Cursor getRows(String category){
    String where = KEY_CATEGORY + "=?";
    Cursor c = myDataBase.query(true, DB_TABLE, ALL_KEYS,
        where, new String[] { category }, null, null, null, null, null);
    if (c != null)
        c.moveToFirst();
    return c;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top