【发布时间】:2014-02-04 22:39:45
【问题描述】:
我是安卓新手。最近我正在尝试开发一个可以从 php 获取数据并将其显示到 android listview 中的应用程序。我已经成功地做到了。但问题是将数据显示到列表视图中。我已经成功地每行显示一个项目。但我需要显示两个或更多。我看过很多教程,但我没有意识到这些教程,因为我是 android 新手。我的代码如下,请帮助我现在该怎么办。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new TheTask().execute();
}
class TheTask extends AsyncTask<Void,Void,String>
{
@Override
protected String doInBackground(Void... params) {
String str = null;
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/BSDI/show.php");
HttpResponse response = httpclient.execute(httppost);
str = EntityUtils.toString(response.getEntity());
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
//public void execute() {
// TODO Auto-generated method stub
//}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
String response = result.toString();
try {
ArrayList<String> stringArray = new ArrayList<String>();
JSONArray new_array = new JSONArray(response);
for(int i = 0, count = new_array.length(); i< count; i++)
{
try {
JSONObject jsonObject = new_array.getJSONObject(i);
stringArray.add(jsonObject.getString("title").toString());
}
catch (JSONException e) {
e.printStackTrace();
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,R.layout.test_tuh,stringArray);
ListView list= (ListView) findViewById(R.id.listView1);
list.setAdapter(adapter);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
tv.setText("error2");
}
}
这是我的 json 响应
[{"title":"notice1","notice":"details..."},{"title":"exam","notice":"Our exam will be held on 20th january..."}]
【问题讨论】:
-
String result.toString() ???
标签: android listview android-listview