我很困惑,不知道如何使用startActivityResults和setResults从以前的活动中获取数据。我有一节观景课和一节活动课。
基本上,在我的视图类中,我有这个对话框,它将实际启动名为colorActivity类的活动类。当用户选择Yes时,它也会将所选圆圈的名称传递给ColorActivity类。在colorActivity类中,允许用户输入特定圆的颜色代码,我想将颜色代码传递回view类。我在使用startActivityForresult和setResult方法将值从活动传递回视图时遇到问题。再加上,之后如何利用获取的数据?
我的代码如下
我的视图类中的ontouchevent代码:

            @Override
            public boolean onTouchEvent(MotionEvent event) {

                x = event.getX();
                y = event.getY();


                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:


                    for (int i = 0; i < circles.size(); i++) {


                        if (circles.get(i).contains(x, y)) {
                            circleID = i;

            Handler handler = new Handler();
                                handler.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        AlertDialog.Builder builder = new Builder(
                                                getContext());
                                        final EditText text = new EditText(getContext());

                                        builder.setTitle("Adding colors to circles").setMessage(
                                                "Proceed to Enter color");
                                        builder.setPositiveButton("Yes",
                                                new DialogInterface.OnClickListener() {

                                                    public void onClick(DialogInterface di,
                                                            int i) {

                                                        Intent intent = new Intent(
                                                                getContext(),
                                                                colorActivity.class);

                                                         intent.putExtra("circlename", circleNameList.get(circleID));


    startActivityForResults(intent, 1); // error incurred here : The method startActivityForResult(Intent, int) is undefined for the type new DialogInterface.OnClickListener(){}
                                                    }

                                                });
                                        builder.setNegativeButton("No",
                                                new DialogInterface.OnClickListener() {

                                                    public void onClick(DialogInterface di,
                                                            int i) {
                                                    }

                                                });

                                        builder.create().show();
                                    }
                                }, 3000);
    break;

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) { // Please, use a final int instead of hardcoded
                                // int value
            if (resultCode == RESULT_OK) {
                 ccode = (String) data.getExtras().getString("colorcode");
        }

        }
    }

public static String getColorCode() {
        return ccode;
    }

在ColorActivity中:
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_ecolor);


        circlenametextview = (TextView)findViewById(R.id.circlenametextview);


        String circlename = super.getIntent().getStringExtra("circlename");
          circlenametextview.setText(circlename);//get the circle name


savebutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                 Intent intent = new Intent(colorActivity.this, ?????);//how to return back to the view class?


               colorcode = colorEditText.getText().toString();// I am able to get value right up till this point
              Intent resultIntent = new Intent();
                   resultIntent.putExtra("colorcode", colorcode );

                   setResult(Activity.RESULT_OK, resultIntent);
                   finish();
            }// onclick

        });
        }

最佳答案

在更正其他代码以便运行程序之后,您可以通过以下方式从activitycolorActivity中检索参数:
步骤1:从colorActivity返回一些值

Intent resultIntent = new Intent();
resultIntent.putExtra("NAME OF THE PARAMETER", valueOfParameter);
...
setResult(Activity.RESULT_OK, resultIntent);
finish();

步骤2:从主活动收集数据
覆盖@onActivityResult(...)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) { // Please, use a final int instead of hardcoded int value
    if (resultCode == RESULT_OK) {
        String value = (String) data.getExtras().getString("NAME OF THE PARAMETER");

工具书类
http://developer.android.com/training/basics/intents/result.html
How to manage `startActivityForResult` on Android?
http://steveliles.github.io/returning_a_result_from_an_android_activity.html

07-24 09:47