如何通过意图发送从另一个活动发送的数据?

时间:2014-10-16 14:51:25

标签: php android mysql eclipse listview

在列出项目之前,我有一个应用程序的功能来创建一个新数据,其中包含来自另一个活动的唯一ID。唯一的数据是" sid"。我使用PHP连接到phpmyadmin数据库。

<?php

// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['foodName']) && isset($_POST['foodPrice']) && isset($_POST['foodType']) && isset($_POST['sid']) {

$foodName = $_POST['foodName'];
$foodPrice = $_POST['foodPrice'];
$foodType = $_POST['foodType'];
$sid = $_POST['sid'];

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// mysql inserting a new row
$result = mysql_query("INSERT INTO food(foodName, foodPrice, foodType, sid) VALUES('$foodName', '$foodPrice', '$foodType', '$sid')");

// check if row inserted or not
if ($result) {
    // successfully inserted into database
    $response["success"] = 1;
    $response["message"] = "Food successfully created.";

    // echoing JSON response
    echo json_encode($response);
} else {
    // failed to insert row
    $response["success"] = 0;
    $response["message"] = "Oops! An error occurred.";

    // echoing JSON response
    echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";

// echoing JSON response
echo json_encode($response);
}
?>

这是从其他活动接收数据的意图:

Intent i = getIntent();

    // getting product id (sid) from intent
    sid = i.getStringExtra(TAG_SID);

最后,我在课堂上执行此操作:

protected String doInBackground(String... args) {

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("foodName", getFoodName));
        params.add(new BasicNameValuePair("foodPrice", getFoodPrice));
        params.add(new BasicNameValuePair("foodType", getFoodType));
        params.add(new BasicNameValuePair("sid", sid));

有什么地方我做错了吗?在我的桌子上,我有这个&#34; sid&#34;太

1 个答案:

答案 0 :(得分:0)

你必须做以下事情 在第一个活动中

Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("sid", VALUE);
startActivity(intent);

在第二项活动中

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("sid");
}