从Cursor填充二维字符串数组

时间:2012-02-24 21:10:04

标签: android database cursor expandablelistadapter

我在尝试将我的数据库加载到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;
}

1 个答案:

答案 0 :(得分:0)

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

    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 []。希望这对遇到类似问题的其他人有帮助。