【问题标题】:asyncTask called in OptionItemSelected called at the wrong Item在 OptionItemSelected 中调用的 asyncTask 在错误的项目中调用
【发布时间】:2015-09-20 13:43:59
【问题描述】:

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

 @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;

}

我在第 3 项单击 (GetDevices) 中访问 Asynctask 数据,但在单击第 2 项 (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";

}

}

【问题讨论】:

    标签: android android-asynctask progressdialog


    【解决方案1】:

    我猜由于 GetAllUserDevices() AsyncTask 在 switch case 结束后正在执行,所以出现了进度对话框。

    请检查是否要在所有情况下都执行 GetAllUserDevices()? 如果不是,则应在特定的 switch 案例中调用它。

    【讨论】:

    • 我也尝试在特定的 switch case 中调用它,但进度对话框出现了两次,一次是在菜单按钮上单击,一次是在单击所选项目时,这太烦人了,需要 0.5 秒的多个进度对话框
    • 您是否在以下方法中执行 AsyncTask 1)GetSpeed(); 2)获取事件(); 3)获取设备(); 4)清除过滤器(); ??
    • 你能把所有代码都放在GetDevice函数和GetAllUserDevices函数中吗
    • 在这里您在所有情况下都调用 GetAllDevices(),因为它写在 switch case.. 如果您希望在 case R.id.FilterDevice: 的情况下调用它:然后对您的代码进行以下更改. switch (item.getItemId()) { case R.id.FilterSpeed: GetSpeed();休息; case R.id.FilterDevice: new GetAllUserDevices(Dashboard.this, Dashboard.this).execute(API_ServiceDev_method_url);休息;并在 GetAllDevice.. 的后期执行中调用 GetDevice()。所以 GetAllDevice 只会在 FilterDevice 点击的情况下触发
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多