0

我有一些来自 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")); 

给我错误。

谢谢您的帮助!

4

2 回答 2

0

我不认为列表视图可以有“列”,所以你应该连接这些字符串。喜欢:

listContents.add(json_data.getString("full_name")+ ' ' +json_data.getString("email"));
于 2012-05-22T03:07:26.420 回答
0

定义数组适配器

           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); 

                    }
                }

现在执行事件:

 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/

于 2012-05-22T04:13:35.780 回答