如何使用两个不同的活动将数据放在两个不同的表中

问题描述:

在我的第一个活动的应用程序中,我使用以下代码将数据存储到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(); 
    } 
} 

那么,你是调用上的代码insert1.php并在下面的代码insert11.php。如果第一个作品和第二个作品不一定与两个不同的PHP内部存在差异。

期待不同的代码库的不同结果:)