Android:视图类和活动类的 startActivityForResult 和 setResult

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22553672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:10:28  来源:igfitidea点击:

Android: startActivityForResult & setResult for a view class and an activity class

androidandroid-intentviewandroid-activity

提问by user3306996

I am confused and have no idea on how to use the startActivityResults and setResults to get data from previous activity. I have a view class and a activity class.

我很困惑,不知道如何使用 startActivityResults 和 setResults 从以前的活动中获取数据。我有一个视图类和一个活动类。

Basically in my view class i have this dialog and it will actually start the activity class called the colorActivity class. When user selects yes also it will pass the name of the selected circle to the colorActivity class. At the colorActivity class, users are allowed to enter color code for a particular circle and i would like to pass the color code back to the view class. I have problems passing values from activity back to view using the startActivityForResult and setResult method. Adding on, how to make use of the fetched data afterthat?

基本上在我的视图类中,我有这个对话框,它实际上会启动名为 colorActivity 类的活动类。当用户选择是时,它也会将所选圆圈的名称传递给 colorActivity 类。在 colorActivity 类中,允许用户输入特定圆圈的颜色代码,我想将颜色代码传递回视图类。我在使用 startActivityForResult 和 setResult 方法将值从活动传递回视图时遇到问题。另外,之后如何使用获取的数据?

my code are as follows

我的代码如下

Ontouchevent code from my view class:

我的视图类中的 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;
    }

In the 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

        });
        }

回答by donnadulcinea

After correcting the other code so that you can run the program, you can retrieve parameters back from your activity colorActivityin this way:

在更正其他代码以便您可以运行程序后,您可以通过colorActivity以下方式从您的活动中检索参数:

Step1: return some value from colorActivity

步骤 1:从 colorActivity 返回一些值

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

Step 2: collect data from the Main Activity

第 2 步:从 Main Activity 收集数据

Overriding @onActivityResult(...).

覆盖@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");

References

参考

回答by J.Ajendra

try using

尝试使用

ActivityName.this.startActivityForResult(intent,int)

Oh, and 1 small thing, in your code you have used

哦,还有一件小事,在您使用的代码中

startActivityForResults(intent,int) ..replace that with

startActivityForResults(intent,int) ..用

startActivityForResult(intent,int)

startActivityForResult(intent,int)