How to set Spinner default value to null?

后端 未结 10 1643
天命终不由人
天命终不由人 2020-11-29 02:36

I\'m trying to get a Spinner to load up with no selected value. Once the user selects a value it then takes them to another page.

This is proving to be a problem be

10条回答
  •  無奈伤痛
    2020-11-29 03:03

    Base on @Jonik answer I have created fully functional extension to ArrayAdapter

    class SpinnerArrayAdapter : ArrayAdapter {
    
        private val selectText : String
        private val resource: Int
        private val fieldId : Int
        private val inflater : LayoutInflater
    
        constructor(context: Context?, resource: Int, objects: Array, selectText : String? = null) : super(context, resource, objects) {
            this.selectText = selectText ?: context?.getString(R.string.spinner_default_select_text) ?: ""
            this.resource = resource
            this.fieldId = 0
            this.inflater = LayoutInflater.from(context)
        }
        constructor(context: Context?, resource : Int, objects: List, selectText : String? = null) : super(context, resource, objects) {
            this.selectText = selectText ?: context?.getString(R.string.spinner_default_select_text) ?: ""
            this.resource = resource
            this.fieldId = 0
            this.inflater = LayoutInflater.from(context)
        }
        constructor(context: Context?, resource: Int, textViewResourceId: Int, objects: Array, selectText : String? = null) : super(context, resource, textViewResourceId, objects) {
            this.selectText = selectText ?: context?.getString(R.string.spinner_default_select_text) ?: ""
            this.resource = resource
            this.fieldId = textViewResourceId
            this.inflater = LayoutInflater.from(context)
        }
        constructor(context: Context?, resource : Int, textViewResourceId: Int, objects: List, selectText : String? = null) : super(context, resource, textViewResourceId,  objects) {
            this.selectText = selectText ?: context?.getString(R.string.spinner_default_select_text) ?: ""
            this.resource = resource
            this.fieldId = textViewResourceId
            this.inflater = LayoutInflater.from(context)
        }
    
        override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup?): View {
            if(position == 0) {
                return initialSelection(true)
            }
    
            return createViewFromResource(inflater, position -1, convertView, parent, resource)
        }
    
        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
            if(position == 0) {
                return initialSelection(false)
            }
            return createViewFromResource(inflater, position -1, convertView, parent, resource)
        }
    
        override fun getCount(): Int {
            return super.getCount() + 1 // adjust for initial selection
        }
    
        private fun initialSelection(inDropDown: Boolean) : View {
            // Just simple TextView as initial selection.
            val view = TextView(context)
            view.setText(selectText)
            view.setPadding(8, 0, 8, 0)
    
            if(inDropDown) {
                // Hide when dropdown is open
                view.height = 0
            }
            return view
        }
    
        private fun createViewFromResource(inflater: LayoutInflater, position: Int,
                                           @Nullable convertView: View?, parent: ViewGroup?, resource: Int): View {
            val view: View
            val text: TextView?
    
            if (convertView == null || (convertView is TextView)) {
                view = inflater.inflate(resource, parent, false)
            } else {
                view = convertView
            }
    
            try {
                if (fieldId === 0) {
                    //  If no custom field is assigned, assume the whole resource is a TextView
                    text = view as TextView
                } else {
                    //  Otherwise, find the TextView field within the layout
                    text = view.findViewById(fieldId)
    
                    if (text == null) {
                        throw RuntimeException("Failed to find view with ID "
                                + context.getResources().getResourceName(fieldId)
                                + " in item layout")
                    }
                }
            } catch (e: ClassCastException) {
                Log.e("ArrayAdapter", "You must supply a resource ID for a TextView")
                throw IllegalStateException(
                        "ArrayAdapter requires the resource ID to be a TextView", e)
            }
    
            val item = getItem(position)
            if (item is CharSequence) {
                text.text = item
            } else {
                text.text = item!!.toString()
            }
    
            return view
        }
    

提交回复
热议问题