0

In my app i am having spinner inside the dialog box when user selects the value from the spinner it will goes to the asynctask and executed for that i am using onItemClickListener. But here the asynctask was executing continuously at the dialog popup time and at the time of the user selecting the spinner value also i debug it and i understood that it was going to the onItemclickListener at the starting and checking the values inside public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {});But i need to start the asynctask only when user selects the spinner values can any one tell me how to achieve this.

NOTE:Here this code i am using inside adapter which was using for list view.

This is my code inside on 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) {

            }
        });

This is my asynctask calling inside spinner

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();

            }
        }
    }
lakshman
  • 181
  • 8
  • 20

2 Answers2

0

Your onItemSelected is triggered without user interaction

you can avoid it by setting selection before setting item select listener

    // 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);

if you still feel trouble follow this link How to keep onItemSelected from firing off on a newly instantiated Spinner?

Community
  • 1
  • 1
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
0

Add Prompt attribute in your spinner.

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

Default prompt text is displaying in spinner.

Set your spinner adapter before set the listener.

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