I am developing an application the returns data from a MYSQL database and displays the results in a listview. This consists of a Name, Address and a Number. When an item in the listview is selected i want it to open another page that displays the details of the item list you clicked on. How would I go about this?I know I will have to use the onListItemClick method but how can I create a page template that will be loaded with the information from whatever item in the list you clicked on? Thanks
Here is the code I use to connect to the database and query it and then display the results in a 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;
}
}
}
What you want to do is pass data via Intents.
From the onListItemClick method, have the following code:
Then, in the new Activity’s onCreate() method, do the following:
For further info, there’s a lesson on this in the Android Training site, called Starting Another Activity