微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 从另一个活动中获取数据

还在研究我的 android技能.

我的问题是我的数据库中有一个标签,其中包含一个旋转器中的名称,当我点击标签时,会出现一个对话框并给出三个选择:
更新
2.删除.
3.取消
我完成了第二个和第三个选择,但在更新中遇到了这个问题;
我转到另一个具有editText和2个按钮,保存和取消的活动,我希望保存按钮从putExtra中的editText获取数据并将其发送回相同的先前活动并使用来自editText.

我感谢任何帮助.
提前致谢.

解决方法

在第二个活动中,您可以使用方法getIntent()获取一个活动的数据,然后使用getStringExtra(),getIntExtra()…

然后要返回到第一个活动,您必须使用setResult()方法将intent数据作为参数返回.

要在第一个活动中获取第二个活动的返回数据,只需覆盖onActivityResult()方法并使用intent获取数据.

第一项活动:

//In the method that is called when click on "update"
Intent intent = ... //Create the intent to go in the second activity
intent.putExtra("oldValue","valueYouWanttochange");
startActivityForResult(intent,someIntValue); //I always put 0 for someIntValue

//In your class
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
    super.onActivityResult(requestCode,resultCode,data);
    //Retrieve data in the intent
    String editTextValue = intent.getStringExtra("valueId");
}

第二项活动:

//When activity is created
String value = intent.getStringExtra("oldValue");
//Then change the editText value

//After clicking on "save"
Intent intent = new Intent();
intent.putExtra("valueId",value); //value should be your string from the edittext
setResult(somePositiveInt,intent); //The data you want to send back
finish(); //That's when you onActivityResult() in the first activity will be called

不要忘记使用startActivityForResult()方法开始第二个活动.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐