2012-05-22 72 views
0

我從我的手機應用程序檢索到的一些數據。用多個數據填充列表視圖的最佳方式是什麼?我有一個像這樣簡單的東西:Android:從MySQL數據庫填充列表視圖?

try { 
     JSONArray jArray = new JSONArray(result); 
     int jArrayLength = jArray.length(); 
     List<String> listContents = new ArrayList<String>(jArrayLength); 

     for(int i =0; i<jArray.length(); i++){ 

      JSONObject json_data = jArray.getJSONObject(i); 
      listContents.add(json_data.getString("full_name")); 

     } 

     ListView myListView = (ListView) findViewById(R.id.front_page_listview); 
     myListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listContents)); 

    }catch(JSONException e){ 
     Log.e("log_tag","Error parsin data "+e.toString()); 
    } 

我可以添加名字就好了,但如果我想同一行內的更多信息?即:電子郵件,生日等...

listContents.add(json_data.getString("full_name"),json_data.getString("email")); 

給我的錯誤。

謝謝你的幫助!

回答

0

我不認爲listviews可以有「列」,所以你應該連接strints而不是。像:

listContents.add(json_data.getString("full_name")+ ' ' +json_data.getString("email")); 
0

定義arrayadapter

  ArrayList<String> al = new ArrayList<String>(); 
      String fullName; 

通過HTTP然後得到的價值和陣列適配器(A1)的參數設置:

嘗試{

    jArray = new JSONArray(result); 
        JSONObject json_data = null; 
        for (int i = 0; i < jArray.length(); i++) { 
         json_data = jArray.getJSONObject(i); 
         targetamount=json_data.getString("full_name"); 

         al.add(fullName); 

        } 
       } 

現在excuted事件:

protected void onPostExecute(final Void unused) { 
       this.progressDialog.dismiss(); 
       try{ 
         ListView listview = (ListView) findViewById(R.id.listView1); 
         this.progressDialog.dismiss(); 
         listview.setAdapter(new DataAdapter(DemoActivity.this,al.toArray(new String[al.size()]))); 

        } 
        catch(Exception e){ 

         Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show(); 
        } 

      } 

想了解這個問題,然後轉到相關鏈接初級講座的詳細信息:

http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/

http://fahmirahman.wordpress.com/2011/04/21/connection-between-php-server-and-android-client-using-http-and-json/

http://www.helloandroid.com/tutorials/connecting-mysql-databasehttp://www.androidhive.info/2012/03/android-listview-with-load-more-button/