【问题标题】:expandable listview android json可扩展的listview android json
【发布时间】:2012-06-23 09:02:45
【问题描述】:

您好我正在尝试创建一个自定义的可扩展列表视图,但是适配器上的文档非常混乱和多样化。像往常一样,我有一个 hashmap 来读取 json 数组,然后我把它放在列表中。每个孩子的组在最后一个位置的键“GRUPO”中的相同数组中发送。有人可以给我适配器实现吗? json中的代码如下:

              if (tipopergunta.contentEquals("BOOLEANO") || tipopergunta.contentEquals("ESCMULTIPLA") || tipopergunta.contentEquals("ESCUNICA")){
                    HashMap<String,String> childdados = new HashMap<String,String>();
                    childdados.put("GRUPO", "Dados");
                    childdados.put("TIPOPERGUNTA", tipopergunta);
                    childdados.put("PERGUNTA", pergunta);
                    childdados.put("BREVEDESIGNACAO", brevedesc);
                    childdados.put("ESTADO",estado);
                    childdados.put("INSTRUCOES",instrucoes);
                    childdados.put("IDPERGUNTA",idpergunta);
                    Log.d("childdados",""+childdados.toString());
                    list.add(childdados);
                }


             if (tipopergunta.contentEquals("OPINIAO")){
                        HashMap<String,String> childdados = new HashMap<String,String>();
                        childdados.put("GRUPO", "Opinião");
                        childdados.put("TIPOPERGUNTA", tipopergunta);
                        childdados.put("PERGUNTA", pergunta);
                        childdados.put("BREVEDESIGNACAO", brevedesc);
                        childdados.put("ESTADO",estado);
                        childdados.put("INSTRUCOES",instrucoes);
                        childdados.put("IDPERGUNTA",idpergunta);

                        Log.d("opiniaolist",childdados.toString());
                        list.add(childdados);
            }  

           if (tipopergunta.contentEquals("FOTOGRAFIA")){
                        HashMap<String,String> childdados = new HashMap<String,String>();
                        childdados.put("GRUPO", "Fotografia");
                        childdados.put("TIPOPERGUNTA", tipopergunta);
                        childdados.put("PERGUNTA", pergunta);
                        childdados.put("BREVEDESIGNACAO", brevedesc);
                        childdados.put("ESTADO",estado);
                        childdados.put("INSTRUCOES",instrucoes);
                        childdados.put("IDPERGUNTA",idpergunta);

                        Log.d("fotolist",childdados.toString());
                        list.add(childdados);
                    }

【问题讨论】:

    标签: android expandablelistview expandablelistadapter


    【解决方案1】:

    This 帮助我理解了可扩展列表视图适配器。

    在可扩展的列表视图中,您始终拥有父母和孩子。当您单击父项时,您会看到其子项。

    所以适配器主要需要三样东西。 父母名单。 子项列表(例如,可以是数组数组) 如何画孩子,如何画父母。

    你应该有一组父母:String[] parents = {"parent1, "parent2"); 和一个子数组数组,其中每个数组都是一个父数组的子数组。

    String [][] children = {{child_A_Parent1, child_B_Parent1}, {child_A_Parent2, child_B_parent2}};
    

    在 getGroup 方法上,您应该返回给定行所需的父级:

    public Object getGroup(int groupPosition) {
            return parents[groupPosition];
        }
    

    在 getChild 方法上,您应该返回您想要的孩子:

    public Object getChild(int groupPosition, int childPosition) {
    
            return children[groupPosition][childPosition];
        }
    

    在 getChildView() 方法上,您应该扩展您想要的布局(例如带有 textview 的 xml)并使用相应的子项设置文本。请注意,在这里您不必担心它已经在参数上传递了哪个孩子:

    public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
       //inflate a the layout and get the text view
       tv.setText(children[grouposition][childPosition];
    ..}
    

    父方法也是如此:

        public View getGroupView(int groupPosition, boolean isExpanded,
                    View convertView, ViewGroup parent) {
            //inflate a the layout and get the text view
    
           tvParent.setText(parents[groupPosition];
    
    }
    

    您可以为父母和孩子设置各种布局。只需使用充气机从布局文件夹中充气 xml。然后使用资源 ID 用您的小部件填充视图。

    希望这可以帮助您调整数据以适应此 sn-p。

    【讨论】:

    • 我仍然无法理解。我可以把我在图像适配器上得到的东西放在这里,我不知道如何在适配器上实现它。
    • 我试图解释每种方法。看看对你有没有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-23
    • 1970-01-01
    相关资源
    最近更新 更多