2013-11-29 29 views
0

我想知道如何將多個值放入ArrayList<NameValuePair>。這是我的代碼,但它不起作用。有人幫我解釋一下ArraysList<NameValuePair>。 這裏是我的代碼:如何將2個或更多的值放入ArrayList <NameValuePair>

public void OnClickForSave(View v) throws Exception { 
    String st = null; 
    String str = null; 
    String response = null; 
    String memberID = "000000007"; 
    String teamID = "000007"; 
    ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
    postParameters.add(new BasicNameValuePair("userID", memberID)); 
    postParameters.add(new BasicNameValuePair("teamID", teamID)); 

    if (v.getId() == (R.id.imgbtn_layout2)) { 
     Intent intent = new Intent(this, IntentTesting.class); 
     startActivity(intent); 
    } 

    if (v.getId() == (R.id.imgbtn_lsave)) { 
     if (txt_teamID.getText().toString().trim().equals(null) 
       || txt_teamID.getText().toString().trim().equals("")) { 
      AlertDialog empty = createDialogEmpty(this, "Enter Team ID"); 
      empty.show(); 
      txt_teamID.selectAll(); 
      txt_teamID.requestFocus(); 
     } 
     else if (txt_teamID.getText().toString().trim().length() > 6) { 
      AlertDialog notFormat = createDialogEmpty(this, "More Than 6 Characters"); 
      txt_teamID.selectAll(); 
      txt_teamID.requestFocus(); 
      notFormat.show(); 
     } else { 
      try { 
       st = CustomHttpClient.executeHttpGet("http://192.168.200.14/football365/DBConnection.php"); 
       str = CustomHttpClient.executeHttpGet("http://192.168.200.14/football365/responseJson.php"); 
       response = CustomHttpClient.executeHttpPost("http://192.168.200.14/football/joinTeam.php", 
         postParameters); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 
+0

什麼是'CustomHttpClient.executeHttpPost'?一個標準庫或你自己的類和方法? – ADTC

回答

0
ArrayList<BasicNameValuePair> postParameters = new ArrayList<BasicNameValuePair>(); 

您需要使用該列表將存儲的數據類型來初始化你的ArrayList。

在此之後只需使用:

ArrayList.add(element e); 

您可以根據需要調用此方法多次。

+0

你介意重寫我的代碼嗎?我不知道。真的很抱歉 – user3032822

+0

感謝foyour患者:) – user3032822

+0

BasicNameValuePair類擴展了類NameValuePair嗎?我不熟悉Android應用程序。 – SpiderShlong

0

嘗試THI代碼如下

List<NameValuePair> postParameters = new ArrayList<NameValuePair>(2); postParameters.add(new BasicNameValuePair("userID",memberID)); postParameters.add(new BasicNameValuePair("teamID",teamID));

它爲我工作。

1

Sup buddy? 在這種情況下,只需使用params的add函數即可。使用特定的大小進行初始化並不是強制性的。

List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("Key1", "Value1")); params.add(new BasicNameValuePair("Key2", "Value"));

你有這樣的事情

鍵1 =值; 鍵2 =值

希望工程

相關問題