我的代码可以完美运行,但前提是我两次单击该按钮。如果我只点击一次,它会返回“limit_customers”= 0。
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button_to_customers:
int limit_customers = 50;
// R1 = Radiobutton > inside RadioGroup
if(r1.isChecked()){
group_id = 0;
}else{ group_id = 1;}
if(the_list.get(0).getSelected() == true){
Log.e("list:", String.valueOf(the_list.get(0).getSelected()));
// This returns true at first click (it's correct)
if(group_id == 0){
Log.e("group:", String.valueOf(group_id));
// returns 0 at first click (it's correct)
limit_customers = get_customers_count();
Log.e("limit:", String.valueOf(limit_customers));
// returns 0 at first click (it's INCORRECT)
// at second click returns 3 (it's correct)
}
}
break;
}
}
这是返回 count() 的函数;
public int get_customers_count(){
ExecutorService mExec = Executors.newSingleThreadExecutor();
mExec.execute(new Runnable() {
@Override
public void run() {
try{
SoapObject request = new SoapObject(NAMESPACE, "get_count_customers");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(URL + "/get_count_customers", envelope);
SoapObject rep = (SoapObject) envelope.bodyIn;
JSONArray jr = new JSONArray(rep.getPropertyAsString(0));
JSONObject jb = (JSONObject) jr.get(0);
amount_customers = jb.getInt("count");
}catch (Exception e){
Log.e("Error:", e.toString());
}
}
});
return amount_customers;
}
任何人都知道它可能是什么?
编辑:我将所有答案都标记为正确,因为您的所有答案都是正确的,我根据它们解决了我的问题。谢谢你。