我正在开发一个应用程序,它返回来自MYSQL数据库的数据并在listview中显示结果。这包括姓名,地址和号码。当列表视图中的项目被选中时,我希望它打开另一个页面,显示您单击的项目列表的详细信息。我将如何解决这个问题?我知道我将不得不使用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;
}
}
}
答案 0 :(得分:3)
您要做的是通过Intents传递数据。
从onListItemClick方法,具有以下代码:
Intent intent = new Intent(getContext(), NewActivity.class);
intent.putExtra("NAME", name);
intent.putExtra("ADDRESS", address);
// etc
startActivity(intent)
然后,在新的Activity的onCreate()方法中,执行以下操作:
Intent intent = getIntent();
String name = intent.getStringExtra("NAME");
// ...etc
有关详细信息,请在Android培训网站上对此进行一课,名为Starting Another Activity
答案 1 :(得分:0)
你有一些选择。 您可以通过intent extras
传递您需要传递的相关数据接收意图附加活动的常见模式是创建一个静态方法,让该活动创建附加组件。像这样的东西:
public static Intent createIntent(Activty activity, String myExtra)
{
Intent intent = new Intent(activity, MyActivity.class);
if(myExtra != null) intent.putExtra(MY_EXTRA, myExtra);
return intent;
}
您可以通过serializing或parceling将更复杂的对象作为附加对象传递。 祝你好运!