当我为我的 isDeleted boolean 值调用 getExtras.getBoolean(key) 时,它一直将自己设置为 false,即使我传递的是 true。关于为什么会发生这种情况的任何见解?我尝试了很多其他方法,但未能成功保持 boolean 值 TRUE。
其他 Activity :
public void deleteWorkout(View view)
{
intent.putExtra("listPosition", intent.getExtras().getInt("position"));
intent.putExtra("isDeleted", true);
setResult(RESULT_OK, intent);
finish();
}
主要 Activity :
case(List): {
if(resCode == Activity.RESULT_OK)
{
boolean isDeleted = intent.getExtras().getBoolean("isDeleted");
int listPosition = intent.getExtras().getInt("listPosition");
if(isDeleted)
{
adapter.remove(workoutList.get(listPosition));
adapter.notifyDataSetChanged();
}
}
}
default:
break;
}
最佳答案
有两种方式将数据从一个 Activity 传递/获取到另一个 Activity 。
1.向 Intent 添加数据。
如何放置:
intent.putExtra("listPosition", intent.getExtras().getInt("position"));
intent.putExtra("isDeleted", true);
获取方式:
int listPosition = getIntent().getIntExtra("listPosition",0);
boolean isDeleted = getIntent().getBooleanExtra("isDeleted",false);
2.将数据添加到bundle并将bundle添加到intent。
如何放置:
Bundle bundle = new Bundle();
bundle.putExtra("listPosition", intent.getExtras().getInt("position"));
bundle.putExtra("isDeleted", true);
intent.putExtras(bundle)
获取方式:
int listPosition = getIntent().getExtras().getInt("listPosition",0);
boolean isDeleted = getIntent().getExtras().getBoolean("isDeleted",false);
关于android - 调用 getExtra 时 boolean 值将自身重置为 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26861117/