我有以下代码来获取整数数据计数。 getData(NewsSettings)
方法返回3.当我点击按钮,应用程序显示我No new news found
。Toast在调用方法之前运行
为什么它在getData之前运行敬酒?
修订
我已经加入了完整的onCreate
和getData
方法。当我运行,它让我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];
}
你尝试另一种方法调试它? dataCount必须<0才能写出敬酒“没有发现新消息” –
我不相信你。你有没有证据表明Toast是在getData运行之前发送的? –
你应该把“getData”的代码,并检查调试器。这在许多层面似乎都是错误的,因为我认为你过分简化了你的代码,并且这样做,删除了错误或隐藏了它。顺便说一句,只需尝试“int dataCount = 3;”只是为了检查,但我很确定问题是getData方法。 – Feuby