0

我正在为Android开发一个应用程序,我正在使用ExpandableListActivity

我正在从以下格式的 sqlite db 中提取数据:

Id | Category | Name 

这里的CODE
是抓取数据的代码:

List<String> groups = new ArrayList<String>();
List<String> books = new ArrayList<String>();
List<String> elecs = new ArrayList<String>();
List<List<String>> children = new ArrayList<List<String>>();

 Cursor c = mDbHelper.fetchAllItems();

        startManagingCursor(c);

        // for all rows
        for(int i=0; i<c.getCount(); i++)
        {
            c.moveToNext();
            String t = c.getString(c.getColumnIndex("category"));

            switch (getCat(t))
            {
            case Books:
                books.add(c.getString(c.getColumnIndex("name")));
                Log.d("FROM: ", c.getString(c.getColumnIndex("name")));
                if(!(groups.contains("Books"))) groups.add("Books");

                break;
            case Electronics:

                elecs.add(c.getString(c.getColumnIndex("name")));               
                Log.d("FROM: ", c.getString(c.getColumnIndex("name")));
                if(!(groups.contains("Electronics"))) groups.add("Electronics");

                break;
            default:
                break;
            }   // end switch
        } // end for loop

        children.add(books);
        children.add(elecs);

当我运行这个应用程序时的输出
是屏幕的样子:

Books
Electronics

当我点击书籍或电子产品时,它会显示所有内容。像这样:

Books
 [c programming, perl]
 [cd, laptop, psp]
Electronics
 [c programming, perl]
 [cd, laptop, psp]

ISSUE
我认为问题来自这种方法(getChild):

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

        List<String> GRP;
        List<List<String>> CHLD;

        public MyExpandableListAdapter(List<String> grps, List<List<String>> chldrn) {
            GRP = grps;
            CHLD = chldrn;
        }
...
...
public Object getChild(int groupPosition, int childPosition) {
    // CHLD[groupPosition][childPosition]; ORIGINAL, where CHLD was a 2d array
     return CHLD.get(childPosition);   MODIFIED
}

我必须将我的字符串列表转换为二维数组吗?

4

1 回答 1

1

我看不到你如何以及在哪里使用你的 ominous getChild(),我不知道是什么CHLD,以及使用了什么“原始”,因为起始 [XXX][XXX] 不是有效的 java 语法。另外,您确定电子产品清单与书籍清单相同吗?这可能暗示您的数据解析错误,或者数据库包含无效数据。

但是,以下内容可能对您有所帮助:

  • 在您的第一个样本children.get(i)中将返回一个List<String>
  • 我认为你的方法是有原因groupPosition的。但是您当前的实现完全忽略了。childPositiongetChild()groupPosition

在不知道究竟是什么问题的情况下无法进一步帮助您。

根据您的新编辑添加:

CHLD是一个字符串列表的列表。正确的实现如下:

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

        List<String> GRP;
        List<List<String>> CHLD;

        public MyExpandableListAdapter(List<String> grps, List<List<String>> chldrn) {
            GRP = grps;
            CHLD = chldrn;
        }

    public Object getChild(int groupPosition, int childPosition) {
         return CHLD.get(groupPosition).get(childPosition);
    }
}
于 2012-07-26T23:01:23.503 回答