Android 我们可以从适配器调用 startActivityForResult 吗?如何获得响应?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20015950/
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
can we call startActivityForResult from adapter?How to get the response?
提问by user
is it possible to have method startActivtyForResult
within an adapter?Then how to get the response? Where to execute the call back function?
是否可以startActivtyForResult
在适配器中使用方法 ?那么如何获得响应?在哪里执行回调函数?
回答by Douglas Drumond Kayama
Yes, it's possible. You need a reference for the Context
in the adapter and call the activity:
是的,这是可能的。您需要Context
对适配器中的进行引用并调用活动:
Intent intent = new Intent(context, TargetActivity.class);
((Activity) context).startActivityForResult(intent, REQUEST_FOR_ACTIVITY_CODE);
Beware that context must be an activity context or this code will fail.
请注意,上下文必须是活动上下文,否则此代码将失败。
You get the result in the enclosing activity using onActivityResult
as usual.
您可以onActivityResult
像往常一样在封闭活动中获得结果。
So, for example:
因此,例如:
In your adapter:
在您的适配器中:
MyAdapter(Context context) {
mContext = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
…
open.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
…
Activity origin = (Activity)mContext;
origin.startActivityForResult(new Intent(mContext, SecondActivity.class), requestCode);
}
});
…
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("MyAdapter", "onActivityResult");
}
In your second activity, do as usual with setResult
and finish
.
在您的第二个活动中,像往常一样使用setResult
和finish
。
In your main activity, capture the result and pass to the adapter callback:
在您的主要活动中,捕获结果并传递给适配器回调:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
mAdapter.onActivityResult(requestCode, resultCode, data);
}
回答by sanjaymith
Yes.You can call startactivityforresult() from adapter.
是的。您可以从适配器调用 startactivityforresult()。
There are two case- 1.Calling adapter from activity and need onActivityResult in activity. 2.Calling adapter from Fragment and need onActivityResult in fragment.
有两种情况 - 1.从活动中调用适配器并在活动中需要 onActivityResult。2.从 Fragment 调用适配器,需要在 Fragment 中调用 onActivityResult。
Case 1:Get OnActivityResult in Activity then pass the reference of activity to adapter constructor
案例1:在Activity中获取OnActivityResult然后将Activity的引用传递给适配器构造函数
public MyAdapter(Activity pActivity, List<MyBean> pList) {
mList = pList;
mActivity = pActivity;
}
Now startActivityForResult
现在开始ActivityForResult
Intent intent = new Intent(context, TargetActivity.class);
mActivity.startActivityForResult(intent, REQUEST_FOR_ACTIVITY_CODE);
Case 2: Get OnActivityResult in Fragment then pass the reference of fragment to adapter constructor
案例二:在 Fragment 中获取 OnActivityResult 然后将 Fragment 的引用传递给适配器构造函数
public MyGamesAdapter(Fragment pContext, List<MyBean> pList,) {
mList = pList;
mMyFragment =pContext;
}
Intent intent = new Intent(context, TargetActivity.class);
mMyFragment.startActivityForResult(intent, REQUEST_FOR_ACTIVITY_CODE);
Now in activity or fragment override OnActivityResult and get result.
现在在活动或片段中覆盖 OnActivityResult 并获得结果。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
mAdapter.onActivityResult(requestCode, resultCode, data);
}
回答by saly
write a functon in activity class like this
在这样的活动类中编写一个函数
public void startCommentActivity(Intent i){
startActivityForResult(i, 100);
}
call it in adapter class
在适配器类中调用它
mActivity.startCommentActivity(intent);