2014-02-06 110 views
0

我想把JSON放到ListView。我從http://api.androidhive.info/contacts/獲取數據(僅使用名字字段),我能夠讓他們到陣列,但即時通訊無法把它們放進列表
JSON在android中的列表視圖

[注] 但是ListView爲13個條目(名稱數量)準確地13行,但行是空的

private class GetJidla extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     // Showing progress dialog 
     pDialog = new ProgressDialog(TableMenuActivity.this); 
     pDialog.setMessage("Please wait..."); 
     pDialog.setCancelable(false); 
     pDialog.show(); 

    } 

    @Override 
    protected Void doInBackground(Void... arg0) { 
     // Creating service handler class instance 
     ServiceHandler sh = new ServiceHandler(); 

     // Making a request to url and getting response 
     String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET); 

     Log.d("Response: ", "> " + jsonStr); 

     if (jsonStr != null) { 
      try { 
       JSONObject jsonObj = new JSONObject(jsonStr); 

       // Getting JSON Array node 
       jidla = jsonObj.getJSONArray(TAG_CONTACTS); 

       // looping through All Contacts 
       for (int i = 0; i < jidla.length(); i++) { 
        JSONObject c = jidla.getJSONObject(i); 

        // String id = c.getString(TAG_ID); 
        String name = c.getString(TAG_NAME); 


        // tmp hashmap for single contact 
        HashMap<String, String> contact = new HashMap<String, String>(); 

        // adding each child node to HashMap key => value 
        // contact.put(TAG_ID, id); 
        contact.put(TAG_NAME, name); 


        // adding contact to contact list 
        jidlaList.add(contact); 

       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } else { 
      Log.e("ServiceHandler", "Couldn't get any data from the url"); 
     } 



     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 
     // Dismiss the progress dialog 
     if (pDialog.isShowing()) 
      pDialog.dismiss(); 
     /** 
     * Updating parsed JSON data into ListView 
     * */ 
     ListAdapter adapter = new SimpleAdapter(
       TableMenuActivity.this, jidlaList, 
       android.R.layout.simple_list_item_1, new String[] {TAG_NAME}, new int[] {android.R.id.list, 
         }); 

     menu = (ListView)findViewById(android.R.id.list); 
     menu.setAdapter(adapter); 

    } 

和列表在這裏

<ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dip" > 



    </ListView> 

i need it to be on one screen, in one activity here is the image, the brown lsit is where i need it

Image

+0

您是否設置了日誌來檢查數據是否到來?如果數據來了,那麼你可以改變列表的背景爲黑色,因爲有時textview是白色的,文字看起來是不可見的 –

+0

是的,我可以得到數據,甚至把它們放入數組中,logcat:02-06 07:37 :05.895:D/jidla:(1487):[{name = Ravi Tamada},{name = Johnny Depp},{name = Leonardo Dicaprio},{name = John Wayne},{name = Angelina Jolie},{name =名字=阿德勒} {姓名=休傑克曼},{名字=威爾史密斯},{名字=克林特伊斯特伍德},{名字= Barack奧巴馬},{名字=凱特Winslet},{名字=阿姆} ] 這是日誌陣列,改變顏色沒有幫助 – user3185930

+0

然後你可以嘗試通過創建自己的自定義適配器? –

回答

1

我有個例做到這一點:

public class MainActivity extends Activity { 
    //json string 
    private String jsonString = "{\"employee\":[{\"emp_name\":\"employee1\",\"emp_no\":\"101700\"},{\"emp_name\":\"employee2\",\"emp_no\":\"101701\"},{\"emp_name\":\"employee3\",\"emp_no\":\"101702\"},"+ 
      "{\"emp_name\":\"employee4\",\"emp_no\":\"101703\"},{\"emp_name\":\"employee5\",\"emp_no\":\"101704\"},{\"emp_name\":\"employee6\",\"emp_no\":\"101705\"},"+ 
      "{\"emp_name\":\"employee7\",\"emp_no\":\"101706\"},{\"emp_name\":\"employee8\",\"emp_no\":\"101707\"},{\"emp_name\":\"employee9\",\"emp_no\":\"101708\"},"+ 
      "{\"emp_name\":\"employee10\",\"emp_no\":\"101709\"},{\"emp_name\":\"employee11\",\"emp_no\":\"101710\"},{\"emp_name\":\"employee12\",\"emp_no\":\"101711\"},"+ 
      "{\"emp_name\":\"employee13\",\"emp_no\":\"101712\"},{\"emp_name\":\"employee14\",\"emp_no\":\"101713\"},{\"emp_name\":\"employee15\",\"emp_no\":\"101712\"}]}"; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     initList(); 
     ListView listView = (ListView) findViewById(R.id.listView1); 
     SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList, android.R.layout.simple_list_item_1, new String[] {"employees"}, new int[] {android.R.id.text1}); 
     listView.setAdapter(simpleAdapter); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    List<Map<String,String>> employeeList = new ArrayList<Map<String,String>>(); 
    private void initList(){ 

     try{ 
     JSONObject jsonResponse = new JSONObject(jsonString); 
     JSONArray jsonMainNode = jsonResponse.optJSONArray("employee"); 

     for(int i = 0; i<jsonMainNode.length();i++){ 
     JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); 
     String name = jsonChildNode.optString("emp_name"); 
     String number = jsonChildNode.optString("emp_no"); 
     String outPut = name + "-" +number; 
     employeeList.add(createEmployee("employees", outPut)); 
     } 
    } 
     catch(JSONException e){ 
     Toast.makeText(getApplicationContext(), "Error"+e.toString(), Toast.LENGTH_SHORT).show(); 
     } 
    } 

    private HashMap<String, String>createEmployee(String name,String number){ 
     HashMap<String, String> employeeNameNo = new HashMap<String, String>(); 
     employeeNameNo.put(name, number); 
     return employeeNameNo; 
    } 

    } 

我用這個代碼它工作正常!

JSON Exemple