2012-08-15 142 views
0

我想從解析的JSON文件填充ExpandableListView來自JSON的動態ExpandableListView

的目標是把第一個條目(kontakt_titel)作爲該組的標題和所有其它的兒童...

JSON文件看起來是這樣的:

{ 
"contact":[ 
    { 
    "kontakt_titel":"Berlin", 
    "adresse_strasse":"Hauptstrasse 1", 
    "adresse_plzort":"4000 Berlin", 
    "telefon":"123456789", 
    "fax":"123456780", 
    "email":"[email protected]", 
    "website":"www.berlin.123.com" 
    }, 
    { 
    "kontakt_titel":"London", 
    "adresse_strasse":"East Highway 1", 
    "adresse_plzort":"London", 
    "telefon":"123456789", 
    "fax":"123456780", 
    "email":"[email protected]", 
    "website":"www.london.123.com" 
    }, 
    { 
    "kontakt_titel":"New York", 
    "adresse_strasse":"Time Square 1", 
    "adresse_plzort":"12345", 
    "telefon":"123456789", 
    "fax":"123456780", 
    "email":"[email protected]", 
    "website":"www.newyork.123.com" 
    } 
] 
} 

這在這裏我想分析這些元素融入到ExpandableListView代碼:

try { 
     JSONArray jContact = json.getJSONArray("contact"); 
     for (int i = 0; i < jContact.length(); i++) { 
      JSONObject c4 = jContact.getJSONObject(i); 

      String contact_title = c4.getString("kontakt_titel"); 
      String adresse_strasse = c4.getString("adresse_strasse"); 
      String adresse_plzort = c4.getString("adresse_plzort"); 
      String telefon = c4.getString("telefon"); 
      String fax = c4.getString("fax"); 
      String email = c4.getString("email"); 
      String website = c4.getString("website"); 

      if (contact_title != null && !contact_title.equals("")) { 
       contactTitles.add(contact_title); 
      } else { 
       contactTitles.add(""); 
      } 
      if (adresse_strasse != null && !adresse_strasse.equals("")) { 
       contactAddressStreet.add(adresse_strasse); 
      } else { 
       contactAddressStreet.add(""); 
      } 
      if (adresse_plzort != null && !adresse_plzort.equals("")) { 
       contactAddressPlzOrt.add(adresse_plzort); 
      } else { 
       contactAddressPlzOrt.add(""); 
      } 
      if (telefon != null && !telefon.equals("")) { 
       contactTelephone.add(telefon); 
      } else { 
       contactTelephone.add(""); 
      } 
      if (fax != null && !fax.equals("")) { 
       contactFax.add(fax); 
      } else { 
       contactFax.add(""); 
      } 
      if (email != null && !email.equals("")) { 
       contactEmail.add(email); 
      } else { 
       contactEmail.add(""); 
      } 
      if (website != null && !website.equals("")) { 
       contactWebsite.add(website); 
      } else { 
       contactWebsite.add(""); 
      } 
     } 
    } catch (JSONException e) { 
     errorHappened = true; 
    } catch (NullPointerException npe) { 
     errorHappened = true; 
    } 

我想這是所有工作...但我覺得這裏是我的錯誤的地方......這是我的適配器方法延伸BaseExpandableListAdapter

@Override 
    public View getChildView(int groupPosition, int childPosition, 
      boolean isLastChild, View convertView, ViewGroup parent) { 

     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) myContext 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.row_contact, null); 
     } 

     TextView tvPlayerName = (TextView) convertView 
       .findViewById(R.id.tvPlayerName); 

     switch (childPosition) { 
      case 0: 
       tvPlayerName 
         .setText(MainActivity.contactAddressStreet.get(childPosition)); 
       break; 
      case 1: 
       tvPlayerName 
         .setText(MainActivity.contactAddressPlzOrt.get(childPosition)); 
       break; 
      case 2: 
       tvPlayerName 
         .setText(MainActivity.contactTelephone.get(childPosition)); 
       break; 
      case 3: 
       tvPlayerName 
         .setText(MainActivity.contactFax.get(childPosition)); 
       break; 
      case 4: 
       tvPlayerName 
         .setText(MainActivity.contactEmail.get(childPosition)); 
       break; 
      case 5: 
       tvPlayerName 
         .setText(MainActivity.contactWebsite.get(childPosition)); 
       break; 
      case 6: 
       tvPlayerName 
         .setText(MainActivity.contactOpening.get(childPosition)); 
       break; 
     } 

     return convertView; 
    } 

的問題是,它始終以同一個項目的所有組中的所有兒童。它需要所有孩子的第一個入口(柏林)作爲第一,第二個(東1號高速公路)的第二入口作爲第二個等所有孩子。

回答

1

該問題是由您建立數據的方式ExpandableListView以及您試圖在getChildView()方法中顯示它的方式。

你解析那個JSON數據的大小相等的列表,其中列表中的每個位置都是一個數據對應的一個JSON對象。在getChildView方法中,您使用childPosition從右列表中獲取該孩子的數據。但使用childPosition是錯誤的,因爲您會看到當前的行爲。

組1(柏林)

  • childPosition == 0(使用第一位置從contactAddressStreet獲取數據("Hauptstrasse 1"))
  • childPosition == 1(使用來自contactAddressPlzOrt第二位置以獲得的數據("London")))

組2(倫敦)

  • childPosition == 0(使用第一位置從contactAddressStreet獲取數據("Hauptstrasse 1"))
  • childPosition == 1(使用來自contactAddressPlzOrt所述第二位置,以獲得數據("London")))

正如你所看到的,你不會考慮這個孩子被請求的小組,並且無論如何你都會得到相同的數據。

要使它工作,使用childPosition使用正確的數據列表和groupPosition來從列表中的實際值:

// ... 
case 0: // child 0 
    tvPlayerName.setText(MainActivity.contactAddressStreet.get(groupPosition)); 
    break; 
// ... 

另一種方法是解析的數據JSON對象結構更適合於ExpandableListView

+0

非常感謝!很好,自我解釋的答案... – user754730 2012-08-16 06:48:34