gpt4 book ai didi

java - Toast 在调用方法之前运行

转载 作者:行者123 更新时间:2023-11-29 02:41:48 28 4
gpt4 key购买 nike

我有以下代码来获取整数数据计数。getData(NewsSettings) 方法返回 3。当我单击该按钮时,应用程序显示未找到新新闻.

为什么它在 getData 之前运行 toast?

已更新

我添加了完整的 onCreategetData 方法。当我运行时,它显示 No new news found 然后是 Inside Response 然后是 Data count = 3

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
assert getSupportActionBar() != null;
getSupportActionBar().setTitle(R.string.action_profile);

pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

Button button = (Button) findViewById(R.id.buttonBuilder);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
newsSettings = new NewsSettings();
newsSettings.setPreferredCity(pref.getString("prefCity", ""));
int dataCount = getData(newsSettings);
if(dataCount > 0)
Toast.makeText(getApplicationContext(), dataCount + " new news found", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(), "No new news found", Toast.LENGTH_SHORT).show();
}
});
}

private int getData(NewsSettings newsSettings) {
final int[] data = {0};

RequestInterface requestInterface = RequestHelper.getInstance().getRequest();

ServerRequest request = new ServerRequest();
request.setOperation("dataCount");
request.setNewsSettings(newsSettings);

Call <ServerResponse> response = requestInterface.operation(request);

response.enqueue(new Callback <ServerResponse>() {
@Override
public void onResponse(Call <ServerResponse> call,
retrofit2.Response <ServerResponse> response) {
ServerResponse resp = response.body();
Toast.makeText(getApplicationContext(), "Inside Response", Toast.LENGTH_SHORT).show();

if(resp.getResult().equals(Constants.SUCCESS)) {
data[0] = resp.getNewsSettings().getDataCount();
Toast.makeText(getApplicationContext(), "Data count = " + data[0], Toast.LENGTH_LONG).show();
}
}

@Override
public void onFailure(Call <ServerResponse> call, Throwable t) {
Log.d(Constants.TAG,"failed");
Toast.makeText(getApplicationContext(), t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
});
return data[0];
}

最佳答案

它返回正确的值,即小于或等于 0。为什么?因为,当您单击按钮时,它会调用 getData() 方法,该方法包含 Retrofit 的异步网络调用方法。其中 data[0] 变量在调用 get onResponse() 方法后被填充值。

因此,与此同时,程序光标完成了它的第一次执行,该执行是在您单击某个按钮时开始的。

您可以像这样修改您的代码。

@Override
protected void onCreate(Bundle savedInstanceState) {
....
....

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
newsSettings = new NewsSettings();
newsSettings.setPreferredCity(pref.getString("prefCity", ""));

/** here is the modification **/
getData(newsSettings);
}

}

并在您的 getData() 方法中删除 int 返回类型并使用 void

private void getData(NewsSettings newsSettings) {  // remove int return type

.....
.....

response.enqueue(new Callback <ServerResponse>() {
@Override
public void onResponse(Call <ServerResponse> call,
retrofit2.Response <ServerResponse> response) {
ServerResponse resp = response.body();
Toast.makeText(getApplicationContext(), "Inside Response", Toast.LENGTH_SHORT).show();

if(resp.getResult().equals(Constants.SUCCESS)) {
data[0] = resp.getNewsSettings().getDataCount();

/** here is the modification ***/
displayDataCount(data[0]); // call to displayCount() method

Toast.makeText(getApplicationContext(), "Data count = " + data[0], Toast.LENGTH_LONG).show();
}
}

@Override
public void onFailure(Call <ServerResponse> call, Throwable t) {
Log.d(Constants.TAG,"failed");
Toast.makeText(getApplicationContext(), t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
});

}

然后像这样创建另一个名为 displayDataCount() 的方法,您可以在其中显示数据计数

private void displayDataCount(int dataCount) {
if(dataCount > 0)
Toast.makeText(getApplicationContext(), dataCount + " new news found", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(), "No new news found", Toast.LENGTH_SHORT).show();

}

关于java - Toast 在调用方法之前运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43566382/

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