2012-06-23 281 views
1

你好,我正在尝试创建一个自定义的可扩展列表视图,但适配器上的文档非常混乱和多样化。和往常一样,我有一个散列表来读取json数组,然后把它放在列表中。每个孩子的小组在最后一个位置的密钥“GRUPO”中的同一个阵列中发送。有人可以给我适配器实现吗?从JSON的代码如下所示:可扩展的列表视图android 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); 
        } 

回答

1

This是什么让我了解到的扩展列表视图适配器。

在可扩展列表视图中,您始终有父母和子女。当你点击父母时,你会看到它的孩子。

所以适配器需要3件东西,主要是。 父母名单。 儿童列表(可以是例如数组数组) 如何绘制孩子以及如何绘制父母。

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

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 getGroupView(int groupPosition, boolean isExpanded, 
       View convertView, ViewGroup parent) { 
     //inflate a the layout and get the text view 

     tvParent.setText(parents[groupPosition]; 

} 

可以设置各种:

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]; 
..} 

同样可以对父类的方法可以说为父母和孩子设计的布局。只需使用充气器从布局文件夹中充入xml即可。然后使用资源ID来填充您的小部件的视图。

希望这可以帮助您调整您的数据到这个片段。

+0

我仍然无法理解。 ican在这里把我在图像适配器上得到的东西放在这里,我不知道如何在适配器上实现它。 – user1437481

+1

我试着解释每种方法。看看它是否可以帮助你。 –