0

我正在开发一个应用程序,它从 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;

    }

}

}
4

2 回答 2

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 培训网站上的相关课程,名为“开始另一个活动”

于 2012-08-16T17:20:19.400 回答
0

你有一些选择。你可以通过intent extras传递你需要传递的相关数据

Activity 接收 Intent Extras 的一种常见模式是创建一个静态方法来让该 Activity 创建 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;
    }

您可以将更复杂的对象作为附加对象通过序列化打包来传递。祝你好运!

于 2012-08-16T17:23:28.043 回答