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()方法开始第二个活动.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...