Android : Open new class/activity on button click in android

on Monday, November 3, 2014


How to open another activity on button click? So now my app has one main window ( when you run the app ). Then there is a menu whit some buttons and when I click on some button is open another activity for this button. So I have one form which user must enter his details but now I want to click on button continue and open another activity where he must enter some more info. Now when this button is clicked the info goes into database. I know how to add more activities on normal page like main where I have only buttons but on this one I don't really know. Here is the code



protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reservation);

editText1 = (EditText) findViewById(R.id.personName);
editText3 = (EditText) findViewById(R.id.personPhone);
editText2 = (EditText) findViewById(R.id.personEmail);
editText4 = (EditText) findViewById(R.id.personComment);
txtDate = (EditText) findViewById(R.id.datePicker);
//txtTime = (EditText) findViewById(R.id.txtTime);
numberOfPeoples = (EditText) findViewById(R.id.numberOfPeoples);

btnCalendar = (Button) findViewById(R.id.btnCalendar);
btnMenues = (Button) findViewById(R.id.continueWithReservation);


btnMenues.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Name = editText1.getText().toString();
Phone = editText3.getText().toString();
Email = editText2.getText().toString();
Comment = editText4.getText().toString();
DateTime = txtDate.getText().toString();
numberOfPeople = numberOfPeoples.getText().toString();
new SummaryAsyncTask().execute((Void) null);
}
});
}

class SummaryAsyncTask extends AsyncTask<Void, Void, Boolean> {

private void postData(String Name, String Email, String Phone,
String Comment, String DateTime, String numberOfPeople) {

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://link.to.php.file");

try {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("Name", Name));
nameValuePairs.add(new BasicNameValuePair("Email", Email));
nameValuePairs.add(new BasicNameValuePair("Phone", Phone));
nameValuePairs.add(new BasicNameValuePair("Comment", Comment));
nameValuePairs.add(new BasicNameValuePair("Date", DateTime));
nameValuePairs.add(new BasicNameValuePair("numberOfPeople", numberOfPeople));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
}
catch(Exception e)
{
Log.e("log_tag", "Error: "+e.toString());
}
}

@Override
protected Boolean doInBackground(Void... params) {
postData(Name, Email, Phone, Comment, DateTime, numberOfPeople );
return null;
}

}


So what I can put in setOnClickListener to go on next activity?


Update: If I change this



public void onClick(View v) {
Name = editText1.getText().toString();
Phone = editText3.getText().toString();
Email = editText2.getText().toString();
Comment = editText4.getText().toString();
DateTime = txtDate.getText().toString();
numberOfPeople = numberOfPeoples.getText().toString();
new SummaryAsyncTask().execute((Void) null);
}


to this



public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Contacts.class);
startActivity(intent);

}


How to keep the information that user is added so far for next activity and save in DB after he finish with second activity?


0 comments:

Post a Comment