2011-12-30 46 views
-3

{「name」:[「bineesh」,「Administrator」,「binu」,「binu」,「bijith」,「prem」,「rahul 「],」 電子郵件 「:」 [email protected] 「 」[email protected]「, 」[email protected]「, 」[email protected]「, 」[email protected]「,」 gdfgd電話號碼:[「345366」,「12345」,「0」,「0」,「0」,「979797」,「7655775」]}我無法得到正確的電子名片,電話號碼

使用JSON解析我檢索所有的名稱值。我在列表視圖中顯示該名稱,但是當我點擊名字時,我無法獲得正確的電子郵件和電話號碼

請幫忙。

  JSONObject myJson = new JSONObject(result); 
      JSONArray ja = myJson.getJSONArray("name"); 
      JSONArray je = myJson.getJSONArray("email"); 
      JSONArray jp = myJson.getJSONArray("phone"); 


      int length=ja.length(); 
      List<String> listContents = new ArrayList<String>(length); 
      for (int i = 0; i < length; i++) { 
      // employeeText.setText(ja.getString(i)); 
       System.out.println(ja.getString(i)); 


       listContents.add(ja.getString(i)); 
      } 



      ArrayAdapter<String> a=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listContents); 
      setListAdapter(a); 




} 

      catch(JSONException e1){ 
       Toast.makeText(getBaseContext(), "No name found" ,Toast.LENGTH_LONG).show(); 
      } catch (ParseException e1) { 
       e1.printStackTrace(); 


      } 




     } 
@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    // TODO Auto-generated method stub 
    super.onListItemClick(l, v, position, id); 
    if(id==0) 
    { 

     for (int i = 0; i < je.length(); i++) { 
      // employeeText.setText(ja.getString(i)); 
       try { 
       System.out.println(je.getString(0)); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

    } 

    Intent i=new Intent(Employeedetail.this,details.class); 
    startActivity(i); 
+1

請在發佈之前重新閱讀您的消息並修復最糟糕的錯別字,以表明對那些試圖幫助您的人的尊重。此外,使用文本輸入區域上方的** [?] **鏈接,瞭解如何正確格式化代碼等。 – 2011-12-30 12:49:51

回答

0

相反,我會建議你創建一個JSON數組與對象。

一個對象具有名稱,電子郵件和電話值。

因爲你只需要從特定位置獲取對象。這也會減少這種比較的負荷。

+0

例如我clik「bineesh」(名稱),然後顯示列表中的所有電子郵件,但我想要bineesh的正確的電子郵件ID如何? – Bineesh 2011-12-30 13:39:58

相關問題