2012-08-16 81 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他們通過更復雜的對象作爲臨時演員。 祝你好運!