gpt4 book ai didi

android - 如何在 Android 中的 Listview 的 CustomListAdapter 类中捕获 Intent 的结果

转载 作者:行者123 更新时间:2023-11-29 01:45:17 25 4
gpt4 key购买 nike

我有一个 ArrayList,其中包含 Person 类型的对象。 Person 类具有名称、地址 1、地址 2、城市、州、邮政编码和国家/地区等字段。我希望能够编辑一个特定的人,然后更新更改,以便显示人员的 ListAdapter 显示更新的数据。这个ListView包含在RecipientActivity(Activity A)中

在自定义适配器中,我使用 TextView 的 onClick 事件中的 Intent 启动 Activity RecipientAddressActivity(Activity B):

holder.txtRecName.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent rec_Intent = new Intent(context,
RecipientAddressActivity.class);
rec_Intent.putExtra("Current_Recipient",
recipientArray.get(index));
rec_Intent.putExtra("RecipientIndex",index);
context.startActivity(rec_Intent);

}
});

在 Activity B 中,我在 onCreate() 事件中获取 Current_Recipient 和索引,如下所示:

current_rec = (Person) getIntent().getSerializableExtra(
"Current_Recipient");
Recipient_Index = getIntent().getIntExtra("RecipientIndex", 151);

在同一个 Activity 中,我有一个“保存”按钮,在它的 Onclick 上我创建了一个 Person 对象,它可以是一个新的 Person 也可以是一个正在编辑的旧的 person。

保存按钮的 onClick() 事件

{
Intent Recipient_info = new Intent();
Person recipient = new Person(edt_rec_name.getText().toString(),
edt_rec_addr1.getText().toString(),edt_rec_addr2.getText().toString(),
edt_rec_city.getText().toString(), edt_rec_state.getText().toString(),
edt_rec_pcode.getText().toString(), edt_rec_country.getText().toString());
Recipient_info.putExtra("Person", recipient);
Recipient_info.putExtra("RecipientIndex", Recipient_Index);
setResult(RESULT_OK, Recipient_info);
finish();
}

问题是有两种启动 Activity B 的方法。当使用 ListView 的适配器启动 Actvity B 时,我不知道在哪里或如何捕获结果。

请尽快帮助我。如果需要更多代码或解释,请告诉我。

谢谢。

最佳答案

我注意到在你的代码中:

您没有以 activityForResult 的身份开始 Activity 。

检查并立即尝试!如果您仍然遇到这个问题,那么这里有一些链接将对您的问题很有用:

calling onActivityResult from CustomArray adapter

onActivityResult in not called in the class extended from ArrayAdapter

How to add item in Custom listView in onActivityResult method?

How to Call onActivityResult in adapter listview?

关于android - 如何在 Android 中的 Listview 的 CustomListAdapter 类中捕获 Intent 的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21871020/

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