我有2项活动。第一个活动具有形式,用户可以插入一些信息。然后有一个按钮,它导致第二个活动,其中用户插入一些更多的信息,然后保存在MySQL数据库中。
我使用intent.putExtra
但不保存数据库中的任何内容。因为我是android的新手,所以我不确定这是否正确。我已将此帖How to get extra data from intent in android?用作参考
这是第一项活动
public class Reservation extends Activity {
static String Name;
static String Email;
static String Phone;
static String Comment;
static String DateTime;
static String numberOfPeople;
private EditText editText1, editText3, editText2, editText4, txtDate, editText5;
private Button btnMenues, btnCalendar, btnTimePicker;
@Override
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);
editText5 = (EditText) findViewById(R.id.editText5);
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();
//DateTime = txtTime.getText().toString();
//numberOfPeople = numberOfPeoples.getText().toString();
//new SummaryAsyncTask().execute((Void) null);
Intent intent = new Intent(Reservation.this, ReservationSecond.class);
String Name = editText1.getText().toString();
String Email = editText2.getText().toString();
String Phone = editText3.getText().toString();
String Comment = editText4.getText().toString();
String DateTime = txtDate.getText().toString();
String numberOfPeople = editText5.getText().toString();
intent.putExtra(Name, Name.getBytes().toString());
intent.putExtra(Email, Email.getBytes().toString());
intent.putExtra(Phone, Phone.getBytes().toString());
intent.putExtra(Comment, Comment.getBytes().toString());
intent.putExtra(DateTime, DateTime.getBytes().toString());
intent.putExtra(numberOfPeople, numberOfPeople.getBytes().toString());
startActivity(intent);
}
});
}
}
这是第二个必须接收数据并保存在BD中的活动
public class ReservationSecond extends Activity {
String getName;
String getEmail;
String getPhone;
String getComment;
String getDateTime;
String getnumberOfPeople;
private Button btnMenues;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reservation_second);
btnMenues = (Button) findViewById(R.id.finish);
btnMenues.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle extras = getIntent().getExtras();
if (extras != null) {
getName = extras.getString("Name");
getEmail = extras.getString("Email");
getPhone = extras.getString("Phone");
getComment = extras.getString("Comment");
getDateTime = extras.getString("DateTime");
getnumberOfPeople = extras.getString("numberOfPeople");
}
new SummaryAsyncTask().execute((Void) null);
startActivity(getIntent());
}
});
}
class SummaryAsyncTask extends AsyncTask<Void, Void, Boolean> {
private void postData(String getNameToData, String getEmailData, String getPhoneData,
String getCommentData, String getDateTimeData, String getnumberOfPeopleData) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://link/saveReservation.php");
try {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("Name", getNameToData));
nameValuePairs.add(new BasicNameValuePair("Email", getEmailData));
nameValuePairs.add(new BasicNameValuePair("Phone", getPhoneData));
nameValuePairs.add(new BasicNameValuePair("Comment", getCommentData));
nameValuePairs.add(new BasicNameValuePair("Date", getDateTimeData));
nameValuePairs.add(new BasicNameValuePair("numberOfPeople", getnumberOfPeopleData));
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(getName, getEmail, getPhone, getComment, getDateTime, getnumberOfPeople );
return null;
}
}
}
更新。在这样的第二次活动中?
if (extras != null) {
extras.getString("Name");
extras.getString("Email");
extras.getString("Phone");
extras.getString("Comment");
extras.getString("DateTime");
extras.getString("numberOfPeople");
}
而不是
if (extras != null) {
String Name = extras.getString("Name");
String Email = extras.getString("Email");
String Phone = extras.getString("Phone");
String Comment = extras.getString("Comment");
String DateTime = extras.getString("DateTime");
String numberOfPeople = extras.getString("numberOfPeople");
}
这是getReservation.php
$icon = mysql_connect("localhost","user","password");
if(!$icon)
{
die('Could not connect : ' . mysql_erroe());
}
mysql_select_db("database", $icon)or die("database selection error");
echo json_encode($data);
$Name=$_POST['Name'];
$Phone=$_POST['Phone'];
$Email=$_POST['Email'];
$Comment=$_POST['Comment'];
$DateTime=$_POST['DateTime'];
$numberOfPeople=$_POST['numberOfPeople'];
mysql_query("INSERT INTO reservation (Name, Phone, Email, Comment, DateTime, numberOfPeople)
VALUES ('$Name', '$Phone', '$Email', '$Comment', NOW(), '$numberOfPeople')",$icon);
mysql_close($图标);
答案 0 :(得分:1)
请参阅the documentation了解putExtra()的工作原理以及您必须将其作为输入
为了获得这样的额外费用
extras.getString("Name");
你必须像这样把它们放在
intent.putExtra("Name", Name.getBytes().toString());
注意名称
周围的额外引号另外,因为你传递的是字符串,所以你可以这样做
intent.putExtra("Name", Name);
- &gt; bytes - &gt;字符串转换是多余的
答案 1 :(得分:1)
您将值移交为键和值。这不起作用,因为更改的值也会更改密钥并阻止您在第二个Activity
中获取您的信息。
试试这个:
intent.putExtra("Name", Name.getBytes().toString());
intent.putExtra("Email", Email.getBytes().toString());
intent.putExtra("Phone", Phone.getBytes().toString());
intent.putExtra("Comment", Comment.getBytes().toString());
intent.putExtra("DateTime", DateTime.getBytes().toString());
intent.putExtra("numberOfPeople", numberOfPeople.getBytes().toString());
答案 2 :(得分:1)
您将用户输入的值作为附加项中的键传递。因此,如果你试图通过&#34; Name&#34;那么它将不会有任何价值。键。
使用
intent.putExtra("Name", Name.getBytes().toString());
而不是
intent.putExtra(Name, Name.getBytes().toString());
同样在第二个活动中使用您声明的全局变量而不是创建本地变量。在传递全局变量并初始化本地变量时。
使用
if (extras != null) {
Name = extras.getString("Name");
Email = extras.getString("Email");
Phone = extras.getString("Phone");
Comment = extras.getString("Comment");
DateTime = extras.getString("DateTime");
numberOfPeople = extras.getString("numberOfPeople");
}