开发者

Dynamically populating an expandableListView

So All I'm trying to do is create a dynamic expandableListView Currently It works if I just do the groupViews. The problem comes in when I have to populate the children of those groupViews.. I don't know if I'm doing something wrong, or if theres another better way to do it. If anyone knows please let me know. I'm open to anything.

Currently I'm pulling my data off a server and the error I'm getting is java null pointer exception. So I'm thinking it might have something to do with how big I specified my array sizes?

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

Here is the rest of the code when I try to populate the 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);
            开发者_如何学JAVA        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());
    }
    }


to me it looks like an error in the loop - as you are reading another line without checking is it null

your while loop should look something like this methinks:

// 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++;         
 }          


I would first replace strings array to some 2d collection for example arraylist2d ( you can google it ) so you could easally add and remove data from list. If you created adapter that extends BaseExpandableListAdapter everything should be handled without any problems. About NULLPointer, could you paste stacktrace or more info on which line it occurs ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜