Java Toast在调用方法之前运行
我有以下代码来获取整数数据计数。Java Toast在调用方法之前运行,java,android,Java,Android,我有以下代码来获取整数数据计数。getData(newsettings)method返回3。当我点击按钮时,应用程序显示我没有发现新消息 为什么它在获取数据之前运行toast 已更新 我添加了完整的onCreate和getData方法。当我运行时,它向我显示未找到新消息然后内部响应然后数据计数=3 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState
getData(newsettings)
method返回3。当我点击按钮时,应用程序显示我没有发现新消息
为什么它在获取数据之前运行toast
已更新
我添加了完整的onCreate
和getData
方法。当我运行时,它向我显示未找到新消息
然后内部响应
然后数据计数=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];
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
断言getSupportActionBar()!=null;
getSupportActionBar().setTitle(R.string.action_profile);
pref=PreferenceManager.GetDefaultSharedReferences(getApplicationContext());
Button Button=(Button)findViewById(R.id.buttonBuilder);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
newsSettings=新的newsSettings();
setPreferredCity(pref.getString(“prefCity”)和;
int dataCount=getData(新闻设置);
如果(数据计数>0)
Toast.makeText(getApplicationContext(),dataCount+“新发现的新闻”,Toast.LENGTH\u SHORT.show();
其他的
Toast.makeText(getApplicationContext(),“未发现新消息”,Toast.LENGTH_SHORT.show();
}
});
}
私有int getData(新闻设置新闻设置){
最终int[]数据={0};
RequestInterface RequestInterface=RequestHelper.getInstance().getRequest();
ServerRequest=newserverrequest();
请求设置操作(“数据计数”);
request.setNewsSettings(新闻设置);
调用响应=请求接口.操作(请求);
response.enqueue(新回调(){
@凌驾
公共void onResponse(调用,
(2.回应){
ServerResponse resp=response.body();
Toast.makeText(getApplicationContext(),“内部响应”,Toast.LENGTH\u SHORT.show();
if(resp.getResult().equals(Constants.SUCCESS)){
数据[0]=响应getNewsSettings().getDataCount();
Toast.makeText(getApplicationContext(),“数据计数=“+Data[0],Toast.LENGTH\u LONG).show();
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.d(Constants.TAG,“failed”);
Toast.makeText(getApplicationContext(),t.getLocalizedMessage(),Toast.LENGTH_LONG).show();
}
});
返回数据[0];
}
确保返回值dataCount
大于0。调试它或删除if-else并使用单个makeToast方法显示数据计数。它返回正确的值,即小于或等于0。为什么?因为,当您单击按钮时,它将调用getData()
方法,该方法包含一个改造的异步网络调用方法。其中,data[0]
变量在调用getonResponse()
方法后填充值
因此,同时程序游标完成了它的第一次执行,这是在您单击某个按钮时开始的
您可以这样修改代码
@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();
}
});
}
你试过调试它吗?数据计数必须小于0才能写祝酒词“未发现新消息”,我不相信你。你有证据表明Toast是在getData运行之前发送的吗?你应该输入“getData”的代码,然后检查调试器。在很多层面上,这似乎是错误的,因为我认为您过度简化了代码,这样做会删除或隐藏错误。顺便说一句,只需尝试使用“int dataCount=3;”进行检查,但我很确定问题出在getData方法上。我已经更新了代码@feubyth,然后谁调用makeText使其可以运行?就像你之前所说的那样。没有,如果不是,但我添加了更多的澄清
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();
}