从另一项活动获取数据数据

2023-09-07 04:43:13 作者:人不轻狂枉少年#

仍然工作在我的技能在android系统。

在这里,我的问题是,我已经从我的数据库包含名称是在旋转,当我在标签上点击一个标签,一个对话框出现,给你三个选择:1.更新。2.删除。3.取消。我通过第二和第三选择得到的,但在更新正在面临这样的问题;我去有一个EditText和2个按钮,另一个活动,保存和取消,我想保存按钮来从EDITTEXT在putExtra数据并发送回同样的previous活动,改变旧标签使用从EDITTEXT的数据。

我AP preciate任何帮助。先谢谢了。

解决方案

在你的第二个活动,你可以从该方法的第一个活动获取数据 getIntent()然后 getStringExtra() getIntExtra() ...

然后返回到你的第一个活动,你必须使用的setResult()法的意图数据即可返回作为参数。

要从你的第二个活动得到返回的数据在你的第一个活动,只是重写的onActivityResult()方法和使用意图获取数据。

第一项活动:

  //在被调用的方法时,点击更新意向意图= ... //创建的意图在第二个活动去intent.putExtra(属性oldValue,valueYouWantToChange);startActivityForResult(意向,someIntValue); //我总是把0 someIntValue//在你的类@覆盖保护无效的onActivityResult(INT申请code,INT结果code,意图数据){    super.onActivityResult(要求code,结果code,数据);    //获得在意向数据    串editTextValue = intent.getStringExtra(VALUEID);} 
如何提升数据质量 附实战文档

第二项活动:

  //当创建活动字符串值= intent.getStringExtra(属性oldValue);//然后,更改EDITTEXT值//点​​击后拯救意向意图=新的Intent();intent.putExtra(VALUEID,值); //值应该是从你的EditText串的setResult(somePositiveInt,意向); //数据要发送回完(); //那就是当你的onActivityResult()中的第一个活动将被称为 

不要忘了用 startActivityForResult()方法来启动你的第二个活动。

Still working on my skills in android.

My problem here is that i have a label from my database that contain a name that is in a spinner, when i click on the label, a dialog comes and give you three choices: 1. update. 2. delete. 3. cancel. I got through the second and the third choices, but in the update am facing this problem; i go to another activity that has an editText and the 2 Buttons, save and cancel, i want the save button to get the data from the editText in putExtra and send it back to the same previous activity and change the old label with the data from the editText.

I appreciate any help. Thanks in advance.

解决方案

In your second activity, you can get the data from the first activity with the method getIntent() and then getStringExtra(), getIntExtra()...

Then to return to your first activity you have to use the setResult() method with the intent data to return back as parameter.

To get the returning data from your second activity in your first activity, just override the onActivityResult() method and use the intent to get the data.

First Activity:

//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");
}

Second Activity:

//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

Don't forget to start your second activity with the startActivityForResult() method.