2012-07-13 48 views
0

我已經開發到一級屬性顯示在列表視圖中,但我想顯示http://hopscriber.com/log.php中的所有屬性。安卓Json內容只顯示一個屬性

[ 
    { 
     "uname": "test", 
     "pwd": "test" 
    }, 
    { 
     "uname": "faith", 
     "pwd": "3220" 
    }, 
    { 
     "uname": "losh", 
     "pwd": "wick" 
    }, 
    { 
     "uname": "loshi", 
     "pwd": "wick" 
    } 
] 

以下是我的代碼,它只顯示pwd的結果。

public class Menu extends Activity { 

    TextView result; 
    Intent intent; 

    // JSON Node names 
    private static final String TAG_Name = null; 
    private static final String TAG_Pass = null; 

    // contacts JSONArray 
    JSONArray contacts = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_menu); 

     // Name of the User 
     result = (TextView) findViewById(R.id.result); 
     intent = getIntent(); 
     String Naming = intent.getStringExtra("name1"); 
     result.setText("Hey " + Naming); 

     ListView list = (ListView) findViewById(R.id.list); 

     try { 
      HttpParams params = new BasicHttpParams(); 
      HttpConnectionParams.setSoTimeout(params, 0); 
      HttpClient httpClient = new DefaultHttpClient(params); 

      // prepare the HTTP GET call 
      HttpGet httpget = new HttpGet("http://hopscriber.com/log.php"); 
      // get the response entity 
      HttpEntity entity = httpClient.execute(httpget).getEntity(); 

      if (entity != null) { 
       // get the response content as a string 
       String response = EntityUtils.toString(entity); 
       // consume the entity 
       entity.consumeContent(); 

       // When HttpClient instance is no longer needed, shut down the 
       // connection manager to ensure immediate deallocation of all 
       // system resources 
       httpClient.getConnectionManager().shutdown(); 

       // return the JSON response 
       ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>(); 

       JSONArray jsonArray = new JSONArray(response); 

       if (jsonArray != null) { 
        for (int i = 0; i < jsonArray.length(); i++) { 
         JSONObject object = (JSONObject) jsonArray.get(i); 

         HashMap<String, String> map = new HashMap<String, String>(); 

         map.put(TAG_Name, object.getString("uname")); 
         map.put(TAG_Pass, object.getString("pwd")); 

         contactList.add(map); 

        } 
       } 

       ListAdapter adapter = new SimpleAdapter(this, contactList, 
         R.layout.menu_list_row, new String[] { TAG_Name, 
           TAG_Pass }, new int[] { R.id.LR_Name, 
           R.id.LR_date }); 
       list.setAdapter(adapter); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     // Launching new screen on Selecting Single ListItem 
     list.setOnItemClickListener(new OnItemClickListener() { 

      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
        long arg3) { 
       Toast.makeText(getBaseContext(), "Booyah", Toast.LENGTH_SHORT) 
         .show(); 
      } 
     }); 
    } 

} 

回答

0

您需要初始化您的json節點名稱。

例如:

// JSON Node names 
private static final String TAG_Name = "name"; 
private static final String TAG_Pass = "pwd"; 

這樣做將輸出所期望的結果。