我正在開發一個應用程序,從MYSQL數據庫返回數據並在列表視圖中顯示結果。這包括名稱,地址和號碼。當選擇列表視圖中的項目時,我希望它打開另一個頁面,顯示您單擊的項目列表的詳細信息。我將如何去解決這個問題?我知道我將不得不使用onListItemClick方法,但是如何創建一個頁面模板,該模板將從您點擊的列表中的任何項目中加載信息?由於如何從ListView填充活動
這裏是我用來連接到數據庫和查詢,然後顯示結果在ListView代碼:你想做什麼
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;
}
}
}
添加您的代碼的詳細信息,並解釋請 – 2012-08-16 17:18:52
您剛剛添加我的代碼 – DMC 2012-08-16 17:21:34