0

我可以从一个 Activity(EditActivity.java) 编辑和保存数据,但是当单击 Save 按钮时,更新的数据不会显示(或结转)到下一个 Activity(ViewActivity.java)。如果返回 EditActivity 页面,我可以看到 EditText 字段上的更改。

编辑活动.java

btnSave.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            // starting background task to update request
            new SaveRequestDetails().execute();
        }
    });

    class SaveRequestDetails extends AsyncTask<String, String, String> {


    protected String doInBackground(String... args) {

        // getting updated data from EditTexts
        String request_title = txtTitle.getText().toString();
        String request_date = txtSdate.getText().toString();
        String reqEndDate = txtEdate.getText().toString();
        String hours = txtHours.getText().toString();
        String reason = txtReason.getText().toString();
        String explanation = txtExp.getText().toString();


        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair(TAG_ID, request_id));
        params.add(new BasicNameValuePair(TAG_TITLE, request_title));
        params.add(new BasicNameValuePair(TAG_SDATE, request_date));
        params.add(new BasicNameValuePair(TAG_EDATE, reqEndDate));
        params.add(new BasicNameValuePair(TAG_HOURS, hours));
        params.add(new BasicNameValuePair(TAG_REASON, reason));
        params.add(new BasicNameValuePair(TAG_EXP, explanation));


        JSONObject json = jsonParser.makeHttpRequest(url_update_request,
                "POST", params);


        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {

                Intent i = getIntent();
                // send result code 100 to notify about request update
                setResult(100, i);
                finish();
            } else {

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

ViewActivity.java

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_request);

        Intent i = getIntent();

        // getting request id (rid) from intent
        request_id = i.getStringExtra(TAG_ID);

        // Getting complete request details in background thread
        new GetRequestDetails().execute();

        btnEdit = (Button) findViewById(R.id.btnEdit);

        btnEdit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // Starting new intent
                Intent in = new Intent(getApplicationContext(), EditActivity.class);
                // sending rid to next activity
                in.putExtra(TAG_ID, request_id);
                startActivity(in);

            }
        });

    }

    // Response from Edit Activity
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // if result code 100
        if (resultCode == 100) {
            // if result code 100 is received 
            // means user edited/deleted request
            // reload this screen again
            Intent intent = getIntent();
            finish();
            startActivity(intent);
        }

    }
4

1 回答 1

3

您的标题说, onActivityResult() 没有被调用。

使用 startActivityForResult(in, 55)代替startActivity(in)

//编辑:顺便说一下,检查结果代码应该使用 RESULT_OK / RESULT_CANCELED 常量来完成。您也可以考虑检查请求代码(在我的示例代码中为 55)

于 2012-08-02T19:14:00.007 回答