【问题标题】:How to send the data which is sent from another activity by intent?如何通过意图发送从另一个活动发送的数据?
【发布时间】:2014-10-16 14:51:25
【问题描述】:

在列出项目之前,我有一个应用程序的功能来创建一个新数据,其中包含来自另一个活动的唯一 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));

是不是我在某处做错了什么?我的桌子上也有这个“sid”。

【问题讨论】:

    标签: php android mysql eclipse listview


    【解决方案1】:

    您必须执行以下操作 在第一个活动中

    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");
    }
    

    【讨论】:

    • 如果你知道你得到一个String作为额外的,你可以直接使用String extra = getIntent().getExtraString("sid");
    • if 条件只是额外的检查,有时你需要调用一个activity而不添加任何数据。
    猜你喜欢
    • 1970-01-01
    • 2020-04-17
    • 2017-08-14
    • 1970-01-01
    • 2020-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多