在OptionItemSelected中调用的asyncTask在错误的Item中调用

时间:2015-09-20 13:44:00

标签: android android-asynctask progressdialog

我有一些带有AsyncTask的OptionItems(带有进度对话框),如图所示

 @Override
public boolean onOptionsItemSelected(MenuItem item) {

    super.onOptionsItemSelected(item);
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    switch (item.getItemId()) {

        case R.id.FilterSpeed:
            GetSpeed();
            break;
        case R.id.FilterEvent:
            GetEvents();
            break;
        case R.id.FilterDevice:
            GetDevices();
            break;
        case R.id.ClearFilter:
            ClearFilter();
            break;
    }
    new GetAllUserDevices(Dashboard.this, Dashboard.this).execute(API_ServiceDev_method_url);
    return true;

}

我在第三项单击(GetDevices)中访问Asynctask数据,但是在单击第二项(GetEvents)时我得到了进度对话框,即使此项与AsyncTask无关。

如何在正确的项目选项中显示进度对话框?

已添加:GetDevices

 public void GetDevices() {
    DevicesNames = GetAllUserDevices.DevicesNames;
    DevicesIds = GetAllUserDevices.DevicesIds;
    SelectedDevicesIds = new HashSet<>();
    SelectedDevices = new ArrayList<>();
    isCheckedDeviceList = new boolean[DevicesIds.size()];
    if (oldChecked.size() > 0) {
        for (int i = 0; i < DevicesIds.size(); i++) {
            isCheckedDeviceList[i] = Dashboard.oldChecked.get(i);
        }
    } else {
        for (int i = 0; i < DevicesIds.size(); i++) {
            oldChecked.add(i, Dashboard.isCheckedDeviceList[i]);
        }
    }
    CharSequence[] DevicesNamesInChar = DevicesNames.toArray(new CharSequence[DevicesNames.size()]);
    builder = new AlertDialog.Builder(Dashboard.this);
    builder.setTitle(getString(R.string.Devicename))
            .setMultiChoiceItems(DevicesNamesInChar, Dashboard.isCheckedDeviceList.length == DevicesIds.size() ? Dashboard.isCheckedDeviceList : null, new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    Dashboard.isCheckedDeviceList[which] = isChecked;
                    if (isChecked) {
                        Dashboard.SelectedDevices.add(which);
                    } else if (Dashboard.SelectedDevices.contains(which)) {
                        Dashboard.SelectedDevices.remove(Integer.valueOf(which));
                    }

                }
            })
            .setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {


                    for (int i = 0; i < Dashboard.isCheckedDeviceList.length; i++) {
                        boolean checked = Dashboard.isCheckedDeviceList[i];
                        Dashboard.oldChecked.set(i, checked);

                        if (checked) {
                            Dashboard.SelectedDevicesIds.add(String.valueOf(DevicesIds.get(i)));
                        }
                    }

                    Dashboard.UserEditor.putStringSet("DevicesIds", Dashboard.SelectedDevicesIds);
                    Dashboard.UserEditor.commit();
                    StopTimerTask();
                    StartTimer();
                }
            })

            .setCancelable(true).show();


}

在一个单独的类GetAllDevices(AsyncTask)中:

public class GetAllUserDevices extends AsyncTask<String, Void, String> {

//vars declaration
static SharedPreferences UserInfo;
static Context context;
static String UserToken;
static Activity activityTest;
InputToString converter;
public static Loading LoadingDialog;
static boolean IsArabic;
public static BufferedReader in;
static String inputLine;
static StringBuffer response;
public static Text_Value_Pair[] devicesList;
public static ArrayList<String> DevicesNames = new ArrayList<>();
public static ArrayList<Integer> DevicesIds = new ArrayList<>();
MoveTo moving;

public GetAllUserDevices(Context currentcontext, Activity currentActivity) {
    context = currentcontext;
    UserInfo = context.getSharedPreferences("Login_UserInfo", Context.MODE_PRIVATE);
    converter = new InputToString();
    activityTest = currentActivity;
    LoadingDialog = new Loading(currentcontext);
    moving = new MoveTo(context);


}

@Override
protected String doInBackground(String... urls) {
    return POSTJson(urls[0]);
}

protected void onPreExecute() {
    super.onPreExecute();

}

@Override
protected void onPostExecute(String result) {
}

public String POSTJson(String url) {
    //User Static Params
    UserToken = UserInfo.getString("Token", "NoToken");
    IsArabic = (new IsArabic(context).IsLangArabic());
    String result = "";
    URL obj;
    try {
        obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("Accept", "application/json");
        con.setRequestProperty("Content-type", "application/json");
        con.setRequestProperty("Token", UserToken);
        // Send post request
        con.setDoOutput(true);
        in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        result = response.toString();
    } catch (IOException e) {
        moving.LogOffNow();
        e.printStackTrace();
    }

    if (!result.endsWith("An error has occurred.\"}")) {
        Gson gson = new Gson();
        devicesList = gson.fromJson(result, Text_Value_Pair[].class);
    } else {
        devicesList = null;
    }

    if (devicesList != null) {
        DevicesNames.clear();
        DevicesIds.clear();
        int i = 0;
        while (i < devicesList.length) {
            DevicesNames.add(devicesList[i].getText());
            DevicesIds.add(devicesList[i].getValue());
            i++;
        }

    }

    return "Data Updated";

}

}

1 个答案:

答案 0 :(得分:0)

我猜进度对话框出现是因为GetAllUserDevices()在切换案例结束后正在执行AsyncTask ..

请检查是否要在所有情况下执行GetAllUserDevices()? 如果不是那么它应该在特定的开关案例中被调用。