gpt4 book ai didi

android - ListView 适配器 setResult() 和 Finish() 错误

转载 作者:搜寻专家 更新时间:2023-11-01 07:47:57 25 4
gpt4 key购买 nike

我想通过使用 startActivityForResult() 方法为结果启动 Activity。现在开始的 Activity 有一个带有适配器类的 ListView 。我已经在 Listview Adapter 之后写了,所以当用户选择任何项目时,它应该返回调用 Activity使用选定的项目名称。我无法调用 2 个方法

setResult() and finish() in following code

适配器代码:

view.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
SharedPreferences prefernces = mContext.getSharedPreferences("MyKey111", Context.MODE_PRIVATE);
SharedPreferences.Editor editor1 = prefernces.edit();
editor1.putString("Custname",customerpopulationlist.get(position).getName());
editor1.putString("let_id", customerpopulationlist.get(position).getLetId());
editor1.commit();

Intent intentMessage = new Intent();

// put the message in Intent
intentMessage.putExtra("MESSAGE", "hello");
intentMessage.putExtra("selected_refer", customerpopulationlist.get(position).getLetId());

setResult(RESULT_OK, intentMessage);
finish();

}
});

最佳答案

您必须持有对创建适配器的 Activity 的引用,然后将结果设置为该 Activity 。您正在做的事情是设置 OnClickListener 对象的结果!!

将一个 Activity 对象添加到您的适配器构造函数并将其保存在适配器中的局部变量中并调用它 act 然后调用 Activity 的 setResult() 如下所示:

view.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
SharedPreferences prefernces = mContext.getSharedPreferences("MyKey111", Context.MODE_PRIVATE);
SharedPreferences.Editor editor1 = prefernces.edit();
editor1.putString("Custname",customerpopulationlist.get(position).getName());
editor1.putString("let_id", customerpopulationlist.get(position).getLetId());
editor1.commit();

Intent intentMessage = new Intent();

// put the message in Intent
intentMessage.putExtra("MESSAGE", "hello");
intentMessage.putExtra("selected_refer", customerpopulationlist.get(position).getLetId());

//THESE TWO LINES NEED TO BE CHANGED
act.setResult(RESULT_OK, intentMessage);
act.finish();

}
});

关于android - ListView 适配器 setResult() 和 Finish() 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39943058/

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