我有一些来自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 :(得分:0)
我不认为listviews可以有“列”,所以你应该连接strints。像:
listContents.add(json_data.getString("full_name")+ ' ' +json_data.getString("email"));
答案 1 :(得分: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);
}
}
现已开展活动:
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://www.helloandroid.com/tutorials/connecting-mysql-database 和 http://www.androidhive.info/2012/03/android-listview-with-load-more-button/