如何在android

时间:2016-01-22 10:11:36

标签: android android-asynctask

在我的应用程序中,当用户从微调器中选择值时,我在对话框中有微调器,它将转到asynctask并执行,因为我正在使用onItemClickListener。但是这里asynctask在对话框弹出时间连续执行,当用户选择微调器值时,我也调试它,我知道它在启动和检查时会转到onItemclickListener public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {});内的值但是我只需要在用户选择微调器值时启动asynctask,任何人都可以告诉我如何实现这一点。

注意:这里的代码我使用的是适用于列表视图的适配器。

这是onItemclickListener

中的代码
leaves_type.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {

            //  leaves_type_string = leaves_type.getSelectedItem().toString();
                new update_leave_entry_breakup_values().execute("UpdateValues",
                        "1082", id_string, leaves_type.getSelectedItem().toString(), username, "",
                        "", "", "", "", "", "");

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

这是我在调整者内部asynctask调用

class update_leave_entry_breakup_values extends
            AsyncTask<String, integer, String> {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

            progressdialog_view.setMessage("Updating leaves...");
            progressdialog_view.show();

        }

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            String TempMethod = params[0];
            String Flag = params[1];
            String value1 = params[2];
            String value2 = params[3];
            String value3 = params[4];
            String value4 = params[5];
            String value5 = params[6];
            String value6 = params[7];
            String value7 = params[8];
            String value8 = params[9];
            String value9 = params[10];
            String value10 = params[11];

            try {
                SoapObject request = new SoapObject(NAME_SPACE, TempMethod);
                request.addProperty("Flag", Flag);
                request.addProperty("value1", value1);
                request.addProperty("value2", value2);
                request.addProperty("value3", value3);
                request.addProperty("value4", value4);
                request.addProperty("value5", value5);
                request.addProperty("value6", value6);
                request.addProperty("value7", value7);
                request.addProperty("value8", value8);
                request.addProperty("value9", value9);
                request.addProperty("value10", value10);

                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                        SoapEnvelope.VER11);
                envelope.dotNet = true;
                envelope.setOutputSoapObject(request);
                HttpTransportSE Android_HttpTransport = new HttpTransportSE(URL);
                Android_HttpTransport.debug = true;
                Android_HttpTransport.call(NAME_SPACE + TempMethod, envelope);
                String responseXml = envelope.getResponse().toString();
                return responseXml;
            } catch (Exception ex) {
                ex.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
             super.onPostExecute(result);

            progressdialog_view.dismiss();

            if (result == null) {
                Toast.makeText(context, "Error while reading data",
                        Toast.LENGTH_SHORT).show();
            } else if (result.equals("0")) {
                Toast.makeText(context, "Error while Updating data",
                        Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "Update Success",
                        Toast.LENGTH_SHORT).show();

            }
        }
    }

2 个答案:

答案 0 :(得分:0)

您的onItemSelected在没有用户互动的情况下被触发

您可以在设置项目选择监听器之前设置选择来避免它

    // Spinner element
    Spinner spinner = (Spinner) findViewById(R.id.spinner);



    // Spinner Drop down elements
    List<String> categories = new ArrayList<String>();
    categories.add("Automobile");
    categories.add("Business Services");
    categories.add("Computers");
    categories.add("Education");
    categories.add("Personal");
    categories.add("Travel");

    // Creating adapter for spinner
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);

    // Drop down layout style - list view with radio button
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // attaching data adapter to spinner
    spinner.setAdapter(dataAdapter);

    //setitem selection without animation
    spinner.setSelection(0, false);

    // Spinner click listener
    spinner.setOnItemSelectedListener(this);

如果您仍感到麻烦,请点击此链接How to keep onItemSelected from firing off on a newly instantiated Spinner?

答案 1 :(得分:0)

在微调器中添加Prompt属性。

<Spinner
    android:id="@+id/spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:prompt="@string/spinner_title"
/>

默认prompt文本显示在微调器中。

在设置侦听器之前设置微调器适配器。

Spinner s = (Spinner) (Spinner) findViewById(R.id.spinner);
    s.setAdapter(adapter);    
    s.setOnItemSelectedListener(listener);