gpt4 book ai didi

android - 如何从使用 getIntent() 接收数据的 Activity 发送数据?

转载 作者:行者123 更新时间:2023-11-29 00:51:27 28 4
gpt4 key购买 nike

我有一个使用 Adapter 和 RecyclerView 向用户显示多个联系人的 Activity ( Activity A)。用户可以点击联系人来查看其信息( Activity B)。然后他们可以选择编辑该信息;为此,用户被带到 EditContact( Activity C)。

是否可以使用 startActivityForResult()在 Activity B 中编辑 Activity C 中的数据库信息并在 RESULT_OK 上更新 Activity B 的数据库和布局?

编辑为清楚起见添加代码 fragment :

新建/编辑联系人 Activity :

// Pass the data to the ViewContact activity
Intent addNewContact = new Intent();
addNewContact.putExtra(EXTRA_NAME, name);
addNewContact.putExtra(EXTRA_SURNAME, surname);
addNewContact.putExtra(EXTRA_COMPANY, company);
addNewContact.putExtra(EXTRA_PHONE_NUMBER, phoneNumbers);
addNewContact.putExtra(EXTRA_PHONE_TYPE, phoneTypes);
addNewContact.putExtra(EXTRA_EMAIL, emails);
addNewContact.putExtra(EXTRA_EMAIL_TYPE, emailTypes);
addNewContact.putExtra(EXTRA_ADDRESS, addresses);
addNewContact.putExtra(EXTRA_POSTCODE, postcode);
[...]

if (name.length() > 0 && phoneNumbers.length() > 0) {
if (id != -1) {
addNewContact.putExtra(EXTRA_ID, id);
}
setResult(RESULT_OK, addNewContact);
onBackPressed(); // or finish()
} else {
Toast.makeText(this, "Please enter at least a name and phone no.",
Toast.LENGTH_LONG).show();
}

查看联系人 Activity :

// On successful result, add the new contact & update the UI
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == EDIT_CONTACT_REQUEST && resultCode == RESULT_OK) {

int id = data.getIntExtra(EXTRA_ID, -1);
// Check ID != -1 just in case, which shouldn't happen
if (id == -1) {
Toast.makeText(this, "Contact not updated", Toast.LENGTH_SHORT).show();
return;
}

String name = data.getStringExtra(EXTRA_NAME);
String surname = data.getStringExtra(EXTRA_SURNAME);
String company = data.getStringExtra(AddNewContact.EXTRA_COMPANY);
String phoneNumber = data.getStringExtra(AddNewContact.EXTRA_PHONE_NUMBER);
String phoneType = data.getStringExtra(AddNewContact.EXTRA_PHONE_TYPE);
String email = data.getStringExtra(AddNewContact.EXTRA_EMAIL);
String emailType = data.getStringExtra(AddNewContact.EXTRA_EMAIL_TYPE);
String address = data.getStringExtra(AddNewContact.EXTRA_ADDRESS);
String postcode = data.getStringExtra(AddNewContact.EXTRA_POSTCODE);

Contact contact =
new Contact(
name,
surname,
company,
phoneNumber,
phoneType,
email,
emailType,
address,
postcode);
contact.setId(id);

// TODO - this does not work, must use LiveData?
// Update the database
ContactRepository contactRepository = new ContactRepository(getApplication());
contactRepository.update(contact);

Toast.makeText(this, "Contact updated", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Contact not updated", Toast.LENGTH_SHORT).show();
}

最佳答案

是的,这是可能的。在 Activity B 中,您可以按如下方式启动 EditContact Activity :

Intent intent = new Intent(this, EditContact.class);
intent.putExtra(EditContact.EXTRA_CONTACT_ITEM, contactItem);
startActivityForResult(intent, EDIT_CONTACT_REQUEST_CODE);

当您在 EditContact Activity 中完成联系人编辑后,返回 RESULT_OK 以及已编辑的 contactItem 并完成 Activity :

Intent resultIntent = new Intent();
resultIntent.putExtra(EXTRA_CONTACT_ITEM, contactItem);
setResult(RESULT_OK, resultIntent);
finish();

然后在Activity B中,对结果进行如下处理:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == EDIT_CONTACT_REQUEST_CODE) {
if(resultCode == Activity.RESULT_OK) {
ContactItem contactItem= data.getParcelableExtra(EXTRA_CONTACT_ITEM);
//Store item in database
}
}
}

关于android - 如何从使用 getIntent() 接收数据的 Activity 发送数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59245232/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com