Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove default value in spinner android?

I am fetching the values from Database using JSON and inserting them into spinner. This process is running successfully. But I am receiving a Spinner hint two times, when I click on spinner; how can I remove the first default value from spinner...

This is snapshot :- hope you understand my problem :enter image description here

This is my code:-

private class GetCategories extends AsyncTask<Void, Void, Void> {
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        ServiceHandler jsonParser = new ServiceHandler();
        String json = jsonParser.makeServiceCall(URL_CATEGORIES,
                ServiceHandler.GET);
        if (json != null) {
            try {
                JSONObject jsonObj = new JSONObject(json);
                if (jsonObj != null) {
                    JSONArray categories = jsonObj
                            .getJSONArray("categories");
                    categoriesList.clear();
                    for (int i = 0; i < categories.length(); i++) {
                        JSONObject catObj = (JSONObject) categories.get(i);
                        Category cat = new Category(
                                catObj.getString("bus_type_id"),
                                catObj.getString("bus_type"));
                        categoriesList.add(cat);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("JSON Data", "Didn't receive any data from server!");
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        populateSpinner();
    }
}




private void populateSpinner() {
    List<String> lables = new ArrayList<String>();
    lables.add("Choose Type");
    for (int i = 0; i < categoriesList.size(); i++) {
        lables.add(categoriesList.get(i).getbus_type());
    }
    // Creating adapter for spinner
    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, lables);
    // Drop down layout style - list view with radio button
    spinnerAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // attaching data adapter to spinner
    typeSpinner.setAdapter(spinnerAdapter);
    typeSpinner.setOnItemSelectedListener(typeSelectedListener);
}

private OnItemSelectedListener typeSelectedListener = new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view,
            int position, long id) {
        if (typeSpinner.getSelectedItem().toString() == "Choose Type") {
            str = "";
            typeSpinnercity.getSelectedView();
            typeSpinnercity.setEnabled(false);
        } else {
            ok.setEnabled(true);
            str = typeSpinner.getSelectedItem().toString();
            typeSpinnercity.getSelectedView();
            typeSpinnercity.setEnabled(true);
            if (Utils.checkInternet(CutomerOfferActivity.this)) {
                new GetCategoriesCity().execute();
            }
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
    }
};
like image 830
Amardeepvijay Avatar asked Sep 22 '14 06:09

Amardeepvijay


People also ask

How to set initial value to spinner in android?

setAdapter(yourAdapter); spinner. setSelection(position); //position is integer starting from 0 to n-1 (n is number of items in the list or array). This will set nth item as the selected and visible item to the user. It should be spinner.

How do you make a spinner Unclickable?

Try removing the xml attribute android:clickable="true" from the Spinner widget. It could be that the entire spinner is registering the click event rather than the individual spinner items.


1 Answers

Use the ArrayAdapter and Override the method - getDropDownView..

ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, lables){
        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {             

            View v = null;

            if (position == 0) {
                TextView tv = new TextView(getContext());
                tv.setHeight(0);
                tv.setVisibility(View.GONE);
                v = tv;
            } 
            else { 

                v = super.getDropDownView(position, null, parent);
            } 

            parent.setVerticalScrollBarEnabled(false);
            return v;
        }
    };

Use the above mentioned code and remove the default value in the dropdown.

like image 122
Rahul Avatar answered Sep 20 '22 12:09

Rahul