2012-06-23 144 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

我試着解釋每種方法。看看它是否可以幫助你。 –