动态填充expandableListView

时间:2011-08-08 07:58:37

标签: android dynamic expandablelistview children

所以我要做的就是创建一个动态的expandableListView目前它可以工作,如果我只是做groupViews。当我必须填充那些groupViews的孩子时,问题出现了。我不知道我做错了什么,或者是否还有另一种更好的方法。如果有人知道请告诉我。我对任何事都持开放态度。

目前我正在从服务器上取出数据,而我得到的错误是java null指针异常。所以我认为它可能与我指定数组大小有多大关系?

private static String[][] children = new String[7][4];
private static String[] groups =  new String[7];

当我尝试填充View时,这是代码的其余部分。

public void getData(){ 
    try {
        int tempGroupCount = 0;
        URL food_url = new URL (Constants.SERVER_DINING);
        BufferedReader my_buffer = new BufferedReader(new InputStreamReader(food_url.openStream()));
        temp = my_buffer.readLine();
        // prime read
        while (temp != null ){
            childrenCount = 0;
            // check to see if readline equals Location
            //Log.w("HERasdfsafdsafdsafE", temp);
            // start a new location
            if (temp.equalsIgnoreCase("Location"))
            {
                temp = my_buffer.readLine();
                groups[tempGroupCount] = temp;
                tempGroupCount++;
                Log.w("HERE IS TEMP", temp);
            }
            temp = my_buffer.readLine();
                while (temp.equalsIgnoreCase("Location") == false){
                    Log.w("ONMG HEHREHRHERHER", temp);
                    children[groupCount][childrenCount] = "IAJHSDSAD";
                    childrenCount++;
                    temp = my_buffer.readLine();
                }
                groupCount++;
        }

        my_buffer.close();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.e("IO EXCEPTION", "Exception occured in MyExpandableListAdapter:" + e.toString());
    }
    }

2 个答案:

答案 0 :(得分:1)

对我而言,它看起来像循环中的错误 - 因为你正在读取另一行而不检查它是否为空

你的while循环应该看起来像这样的方法:

// prime read         
while (temp != null ){             
    int childrenCount = 0;             
    // check to see if readline equals Location       
    // start a new location 
    //Log.w("HERasdfsafdsafdsafE", temp);            
    if (temp.equalsIgnoreCase("Location"))             
    {                 
        temp = my_buffer.readLine();                 
        groups[tempGroupCount] = temp;                 
        tempGroupCount++;                 
        Log.w("HERE IS TEMP", temp);             
    }
    //>>remove following line as that one isn't checked and
    //>>you are loosing on a line that is potentialy a child
    //temp = my_buffer.readLine();
    //>>check do you have first item to add subitems
    else if (tempGroupCount>0){
        while (temp.equalsIgnoreCase("Location") == false){                     
        Log.w("ONMG HEHREHRHERHER", temp);  
        children[tempGroupCount-1][childrenCount] = "IAJHSDSAD";                     
        childrenCount++;                     
        temp = my_buffer.readLine();                 
    } 
    //>>next counter is probably not need but can't see if you're using it somewhere else                
    //groupCount++;         
 }          

答案 1 :(得分:0)

我首先将字符串数组替换为某些2d集合,例如arraylist2d(你可以谷歌),这样你就可以轻松地从列表中添加和删除数据。如果您创建了扩展BaseExpandableListAdapter的适配器,则应该毫无问题地处理所有内容。 关于NULLPointer,你可以粘贴堆栈跟踪或更多信息吗?