【问题标题】:Filling Two-Dimensional String Array from Cursor从光标填充二维字符串数组
【发布时间】:2012-02-24 21:10:04
【问题描述】:

我在尝试将我的数据库加载到 Android ExpandableList 时遇到问题,我尝试使用 CursorTree 和 SimpleCursorTree 方法,但收效甚微,所以现在我正在尝试让它与 BaseExpandableListAdapter 一起使用。由于某种原因,我得到的只是类别,而不是类别的实际列表,我做错了什么?

public static String[][] sortSpellNames(){
    int groupnmbr = 0;
    int childnmbr = 0;
    String[] aGroups = {"Combat", "Detection", "Health", "Illusion", "Manipulation"};
    String[] Combat = new String[grabList("SHADOWRUNSPELLS",new String[]{"Name"}, "'Category' like 'Combat'").getCount()],
            Detection = new String[grabList("SHADOWRUNSPELLS",new String[]{"Name"}, "'Category' like 'Detection'").getCount()],
            Health = new String[grabList("SHADOWRUNSPELLS",new String[]{"Name"}, "'Category' like 'Health'").getCount()],
            Illusion = new String[grabList("SHADOWRUNSPELLS",new String[]{"Name"}, "'Category' like 'Illusion'").getCount()],
            Manipulation = new String[grabList("SHADOWRUNSPELLS",new String[]{"Name"}, "'Category' like 'Manipulation'").getCount()];

    Cursor cursor;
    while(groupnmbr < aGroups.length){
        childnmbr = 0;

        cursor = grabList("SHADOWRUNSPELLS",new String[]{"Name"}, "'Category' like '" + aGroups[groupnmbr] + "'");
        cursor.moveToFirst();

        while(cursor.moveToNext()){
            switch(groupnmbr){
            case 0: Combat[childnmbr] = cursor.getString(cursor.getColumnIndex("Name"));
                    break;
            case 1: Detection[childnmbr] = cursor.getString(cursor.getColumnIndex("Name"));
                    break;
            case 2: Health[childnmbr] = cursor.getString(cursor.getColumnIndex("Name"));
                    break;
            case 3: Illusion[childnmbr] = cursor.getString(cursor.getColumnIndex("Name"));
                    break;
            case 4: Manipulation[childnmbr] = cursor.getString(cursor.getColumnIndex("Name"));
                    break;
            }
            childnmbr++;
        }
        groupnmbr++;
    }

    String[][] aChildren = {Combat, Detection, Health, Illusion, Manipulation}; 

    return aChildren;
}

【问题讨论】:

  • 这个看起来很奇怪:"'Category' like '"。您正在将字符串“类别”(不是字段!)与另一个字符串进行比较。你确定这是你想要的吗?
  • Category 是我数据库中列的名称,这种搜索方法适用于程序的每个部分。

标签: android database cursor expandablelistadapter


【解决方案1】:

我刚刚解决了这个问题。首先,搜索需要 \" 围绕搜索,而不是 '。然后我遇到了一个空指针异常,该异常已通过更改解决

    String[] Combat = new String[grabList("SHADOWRUNSPELLS",new String[]{"Name"}, "'Category' like 'Combat'").getCount()]

    String[] Combat = new String[grabList("SHADOWRUNSPELLS",new String[]{"Name"}, "'Category' like 'Combat'").getCount() - 1]

并为每个 String[] 重复。希望这对遇到类似问题的其他人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    • 2015-05-22
    • 1970-01-01
    • 2013-05-06
    • 2017-06-29
    • 1970-01-01
    • 2021-12-08
    相关资源
    最近更新 更多