问题描述
我有这个code:
意向意图=新的意图();
intent.setAction(Intent.ACTION_PICK);
intent.setData(ContactsContract.Contacts.CONTENT_URI);
intent.putExtra(EXTRA_ONLINE_ID,(字符串)v.getTag());
startActivityForResult(意向,PICK_CONTACT);
然后在响应:
公共无效onActivityResult(INT REQ code,INT结果code,意图数据){
开关(REQ code){
案例(PICK_CONTACT):
如果(结果code == Activity.RESULT_OK){
尝试 {
乌里联系数据= data.getData();
字符串onlineid = data.getStringExtra(EXTRA_ONLINE_ID);
}赶上(例外五){
e.printStackTrace();
}
}
打破;
}
super.onActivityResult(REQ code,因此code,数据);
}
在 onlineid
变量为空。我如何传递一个值,然后接受它了?
修改
我甚至尝试,
捆绑额外= data.getExtras(); //返回null
这是由设计完成的;系统的活动将不会发送回来,他们是所谓的群众演员,所以你必须在其他地方管理数据。
幸运的是,结果code
参数完全由自己控制的,这意味着你可以用它来索引你的数据。
私人最终诠释PICK_CONTACT = 0;
私人捆绑[] myDataTransfer = {空};
...
捆绑的myData =新包();
myData.putString(EXTRA_ONLINE_ID,(字符串)v.getTag());
myDataTransfer [PICK_CONTACT] = myData的;
//创建意图和所有
startActivityForResult(意向,PICK_CONTACT);
...
公共无效onActivityResult(INT REQ code,INT结果code,意图数据){
如果(结果code == PICK_CONTACT){
捆绑的myData = myDataTransfer [结果code]。
字符串onlineid = myData.getString(EXTRA_ONLINE_ID);
}
}
我不是一个Java程序员,必须有实现捆绑的地图一个更好的办法,但这样工作的:)
I have this code:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
intent.setData(ContactsContract.Contacts.CONTENT_URI);
intent.putExtra(EXTRA_ONLINE_ID, (String) v.getTag());
startActivityForResult(intent, PICK_CONTACT);
Then on response:
public void onActivityResult(int reqCode, int resultCode, Intent data) {
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
try {
Uri contactData = data.getData();
String onlineid = data.getStringExtra(EXTRA_ONLINE_ID);
} catch (Exception e) {
e.printStackTrace();
}
}
break;
}
super.onActivityResult(reqCode, resultCode, data);
}
the onlineid
variable is null. How can I pass a value and then to receive it back?
EDIT
I even tried,
Bundle extras = data.getExtras(); // returns null
This is done by design; system activities will not send back the extras with which they're called, so you have to manage the data elsewhere.
Luckily, the resultCode
parameter is fully controlled by yourself, which means that you can use it to index your data.
private final int PICK_CONTACT = 0;
private Bundle[] myDataTransfer = { null };
...
Bundle myData = new Bundle();
myData.putString(EXTRA_ONLINE_ID, (String) v.getTag());
myDataTransfer[PICK_CONTACT] = myData;
// create intent and all
startActivityForResult(intent, PICK_CONTACT);
...
public void onActivityResult(int reqCode, int resultCode, Intent data) {
if (resultCode == PICK_CONTACT) {
Bundle myData = myDataTransfer[resultCode];
String onlineid = myData.getString(EXTRA_ONLINE_ID);
}
}
I'm not a Java programmer, there must be a nicer way to implement a map of Bundles, but this works :)
这篇关于startActivityForResult和意图附加功能,似乎演员不是推后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!