我正在开发一个应用程序,它从 MYSQL 数据库返回数据并在列表视图中显示结果。这由名称、地址和编号组成。当列表视图中的一个项目被选中时,我希望它打开另一个页面,显示您单击的项目列表的详细信息。我该怎么做呢?我知道我将不得不使用 onListItemClick 方法,但是如何创建一个页面模板,该模板将加载您单击的列表中任何项目的信息?谢谢
这是我用来连接数据库并查询它然后在列表视图中显示结果的代码:
public class HttpExample extends ListActivity {
TextView httpStuff;
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// setContentView(R.layout.httpex);
setContentView(R.layout.listplaceholder);
httpStuff = (TextView) findViewById(R.id.tvHttp);
GetMethodEx test = new GetMethodEx();
String returned;
try {
returned = test.getInternetData();
httpStuff.setText(returned);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(this, mylist, R.layout.main,
new String[] { "name", "address", "number" }, new int[] {
R.id.item_title, R.id.item_subtitle, R.id.item_number });
setListAdapter(adapter);
}
public class GetMethodEx {
public String getInternetData() throws Exception {
BufferedReader in = null;
String data = "";
String returnString = "";
// httpGet
try {
HttpClient client = new DefaultHttpClient();
URI website = new URI("http://192.168.0.10/connect.php");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while ((l = in.readLine()) != null) {
sb.append(l + nl);
}
in.close();
data = sb.toString();
// return data;
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
// parse json data
try {
JSONArray jArray = new JSONArray(data);
for (int i = 0; i < jArray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject json_data = jArray.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name",
"ShopName:" + json_data.getString("shopname"));
map.put("address",
"Address: " + json_data.getString("address"));
map.put("number", "Number: " + json_data.getInt("number"));
mylist.add(map);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return returnString;
}
}
}