2013-04-08 37 views
0

在我的第一個活動的應用程序中,我使用以下代碼將數據存儲到mysql中。 Java代碼:如何使用兩個不同的活動將數據放在兩個不同的表中

public class MainActivity extends Activity { 

EditText et; 
Button b; 
InputStream is; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    et = (EditText) findViewById(R.id.editText1); 
    b = (Button) findViewById(R.id.button1); 

    b.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 

      String name = et.getText().toString(); 
      ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("name", name)); 


      try { 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost("http://10.0.2.2/insert1.php"); 
       httppost.setEntity(new UrlEncodedFormEntity(params)); 
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity(); 
       is = entity.getContent(); 
       Toast.makeText(getBaseContext(), "Congrats! ur added", Toast.LENGTH_LONG).show(); 

       Intent mainpage = new Intent("com.example.test.PLACE"); 
       startActivity(mainpage); 
      } catch (Exception e) { 
       e.printStackTrace(); 
       Toast.makeText(getBaseContext(), "Not connected to DataBase", Toast.LENGTH_LONG).show(); 
      } 

     } 
    }); 

} 

爲了使簡單我剛剛只用一個字段在這裏。現在我試圖在其他活動中從用戶那裏獲得另一個字段,並且我嘗試使用上述代碼的相同概念將其存儲在不同的表中。但它沒有被儲存。任何人都可以幫助我解決這個問題..我試了很長時間沒有工作。請幫助我。 我在其他使用的其他Java代碼如下..我想要的是我需要獲取名稱和地點在不同的活動,我想將它們存儲在不同的表中。請幫助我..

protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.place); 

    t = (EditText) findViewById(R.id.editText1); 

    tv = (TextView) findViewById(R.id.textView1); 

    View bb = (Button) findViewById(R.id.button1); 
    bb.setOnClickListener(this); 

} 



@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    String place = t.getText().toString(); 
    ArrayList<NameValuePair> namevalueparams = new ArrayList<NameValuePair>(); 
    namevalueparams.add(new BasicNameValuePair("place", place)); 


    try { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://10.0.2.2/insert11.php"); 
     httppost.setEntity(new UrlEncodedFormEntity(namevalueparams)); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     iss = entity.getContent(); 
     Toast.makeText(getBaseContext(), "Congrats! ur added", Toast.LENGTH_LONG).show(); 



    } catch (Exception e) { 
     e.printStackTrace(); 
     Toast.makeText(getBaseContext(), "Not connected to DataBase", Toast.LENGTH_LONG).show(); 
    } 
} 

回答

0

那麼,你是調用上的代碼insert1.php並在下面的代碼insert11.php。如果第一個作品和第二個作品不一定與兩個不同的PHP內部存在差異。

期待不同的代碼庫的不同結果:)

相關問題