我收到分配给int的json服务器响应。我正在使用putExtra(也尝试共享首选项)将此int传递给下一个活动。当我加载下一个活动时,int不会第一次显示在屏幕上。我必须按下后退按钮,然后再次加载活动以显示int值。我无法弄清楚为什么该值第一次没有正确通过,为什么在第二次重载时它成功通过了。我完全不知所措,我想它要么成功要么失败,否则第一次将失败,并且每次都会传递一次重载。
第一个活动进行插入并捕获行ID,将行ID传递给第二个活动,第二个活动用于使用传递的行ID进行更新。
请参阅代码段获取上下文。
带补液呼叫的第一项活动:
case R.id.nextBtnMoodPage:
if (hasSelected) {
moodPreferences();
backgroundSelector();
Call<ReadCbtId> call = RetrofitClient
.getInstance()
.getApi()
.insertLog(userId, therapistId, moodBefore, automaticThoughtString, distortions, challengeThoughtString, alternativeThoughtString, moodAfter, posted);
call.enqueue(new Callback<ReadCbtId>() {
@Override
public void onResponse(Call<ReadCbtId> call, Response<ReadCbtId> response) {
cbtId = response.body().getCbtId();
Toast.makeText(getContext(),String.valueOf(cbtId), Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<ReadCbtId> call, Throwable t) {
Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_LONG).show();
}
});
//editorWorkout.putInt("cbtId", cbtId);
//editorWorkout.commit();
Intent intent = new Intent(getActivity(), WorkoutAutomaticThoughtActivity.class);
intent.putExtra("cbtId", cbtId);
startActivity(intent);
}
第二活动:
/**
* Ints which are used to store numeric values related to the workout activity.
*/
private int userId, therapistId, cbtId;
private TextView title;
/**
* @param savedInstanceState
*/
@SuppressLint("ResourceType")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_workout_automatic_thought);
Bundle extras = getIntent().getExtras();
cbtId = extras.getInt("cbtId");
getSharedPreferences();
moodPreferences();
backgroundSelector();
initialiseViews();
setListeners();
}
/**
* Method which gets the users shared preferences. loginPref stores the users ID and workoutPref stores the users mood
* which they selected at the beginning of the Workout Activity.
*/
private void getSharedPreferences() {
// Shared preferences track which mood the user selected
loginPref = getSharedPreferences("loginPref", Context.MODE_PRIVATE);
workoutPref = getSharedPreferences("workoutPref", Context.MODE_PRIVATE);
// Users login preferences logged from the login activity.
userId = loginPref.getInt("userId", 0);
therapistId = loginPref.getInt("therapistId", 0);
// cbtId = workoutPref.getInt("cbtId", 0);
}
/**
*
*/
private void initialiseViews() {
// Initialising editText form the xml file
automaticThoughtET = findViewById(R.id.automaticThoughtInput);
// Initialising the nextButton form the xml file
nextButton = findViewById(R.id.DistortedNextBtn);
title = findViewById(R.id.automaticThoughtTitle);
title.setText("Test" + cbtId);
}
private void setListeners() {
// Setting an onClick listener to the nextButton
nextButton.setOnClickListener(new View.OnClickListener() {
/**
* Method which opens a new activity when the next nextButton is clicked. This method also passed variables through
* to the next activity as a string.
* @param v
*/
@Override
public void onClick(View v) {
if (!validateText()) {
return;
}
Call<ResponseBody> call = RetrofitClient
.getInstance()
.getApi()
.updateAutomaticThought(cbtId, automaticThoughtString);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
// Creating a new intent to move from the WorkoutAutomaticThoughtActivity to the WorkoutDistortedThoughtsActivity
Intent intent = new Intent(WorkoutAutomaticThoughtActivity.this, WorkoutDistortedThoughtsActivity.class);
/* // Passing variables as Strings to the next activity
automaticThoughtString = automaticThoughtET.getText().toString();
intent.putExtra("AthoughtKey", automaticThoughtString);*/
startActivity(intent);
finish();
}
});
答案 0 :(得分:2)
在获得结果之前,您正在调用下一个活动。
Intent intent = new Intent(getActivity(), WorkoutAutomaticThoughtActivity.class);
intent.putExtra("cbtId", cbtId);
startActivity(intent);
此代码应在
内部调用 @Override
public void onResponse(Call<ReadCbtId> call, Response<ReadCbtId> response) {
cbtId = response.body().getCbtId();
Toast.makeText(getContext(),String.valueOf(cbtId), Toast.LENGTH_SHORT).show();
}
所以它看起来像这样:
@Override
public void onResponse(Call<ReadCbtId> call, Response<ReadCbtId> response) {
Intent intent = new Intent(getActivity(), WorkoutAutomaticThoughtActivity.class);
intent.putExtra("cbtId", response.body().getCbtId());
Toast.makeText(getContext(),String.valueOf(response.body().getCbtId()), Toast.LENGTH_SHORT).show();
startActivity(intent);
}