动态填充可扩展ListView

发布于 2024-11-28 13:01:55 字数 1823 浏览 1 评论 0原文

所以我想做的就是创建一个动态的可扩展列表视图,如果我只做groupViews,它就可以工作。当我必须填充这些 groupView 的子级时,问题就出现了。我不知道我是否做错了什么,或者是否有其他更好的方法来做到这一点。如果有人知道请告诉我。我对任何事情都持开放态度。

目前,我正在从服务器上提取数据,收到的错误是 java 空指针异常。所以我想这可能与我指定的数组大小有多大有关?

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

这是我尝试填充视图时的其余代码。

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());
    }
    }

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);
                    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());
    }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海螺姑娘 2024-12-05 13:01:55

对我来说,它看起来像是循环中的错误 - 当您正在阅读另一行而不检查它是否为空时,

您的 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++;         
 }          

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++;         
 }          
墨落画卷 2024-12-05 13:01:55

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

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 ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文