java android如何在适配器内调用startActivityForResult

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14804374/
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-10-31 17:26:45  来源:igfitidea点击:

android how to call startActivityForResult inside an adapter

javaandroidandroid-activityandroid-adapterandroid-adapterview

提问by user2059935

I have an adapter class :

我有一个适配器类:

public class AdapterAllAddress extends BaseExpandableListAdapter {
private Context context;
    public AdapterAllAddress(Context context,
            ArrayList<AllAddressesGroup> groups) {
        // TODO Auto-generated constructor stub
        this.context = context;
    }
}

I want to call startActivityForResultwhen a button click , I know I can call startActivitylike this:

我想在startActivityForResult单击按钮时调用,我知道我可以这样调用startActivity

context.startActivity() 

but i am looking for activity with results, how please ?

但我正在寻找有结果的活动,请问如何?

回答by William Kinaan

yourButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
        Intent intent = new Intent(context, YourNewActivity.class);
        ((Activity) context).startActivityForResult(intent, resultCode);
    }
});

回答by katmanco

I just wanted to point a detail which i faced in my case E/ActivityThread(31584): Performing stop of activity that is not resumed: {com.example.test/activities.MainActivity}most probably you are passing getApplicationContext() to the adapter's constructor . In order to avoid this you must provide "CallingActivity.this" to the adapter's constructor as the contextobject , keep this in mind .

我只是想指出我在E/ActivityThread(31584) 中遇到的一个细节 :执行未恢复的活动停止:{com.example.test/activities.MainActivity}很可能你正在将 getApplicationContext() 传递给适配器的构造函数。为了避免这种情况,您必须将“ CallingActivity.this”作为上下文对象提供给适配器的构造函数,请记住这一点。