2012-08-16 82 views
0

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

    } 

} 

} 
+0

添加您的代码的详细信息,并解释请 – 2012-08-16 17:18:52

+0

您刚刚添加我的代码 – DMC 2012-08-16 17:21:34

回答

3

是通过意图传递数据。

从onListItemClick方法,有下面的代码:

Intent intent = new Intent(getContext(), NewActivity.class); 
intent.putExtra("NAME", name); 
intent.putExtra("ADDRESS", address); 
// etc 

startActivity(intent) 

那么,在新的活动的onCreate()方法,请执行以下操作:

Intent intent = getIntent(); 
String name = intent.getStringExtra("NAME"); 
// ...etc 

如需进一步信息,有一个教训在Android培训网站上,调用Starting Another Activity

+0

很酷的感谢。我会放弃这一点。 – DMC 2012-08-16 17:23:31

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他们通过更复杂的对象作为临时演员。 祝你好运!