如何将微调器中的第一个值(默认值)设置为空

时间:2015-09-15 11:45:05

标签: android android-spinner default-value dropdown

如何将微调器中的第一个值(默认值)设置为空。意味着,直到用户从列表中选择值,微调器才会没有值。我试过下面的

list.add(""); 
但是,它不会起作用。以下是我的代码:

if(values.contains("options")){
       spinner.setOnItemSelectedListener(MyClass.this);
       List<String> list = new ArrayList<String>();
       list.add("");
       list.add("Test");
       list.add("Working");
       ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item, list) {};

      adapter.setDropDownViewResource(R.layout.custom_spinner_list);
      spinner.setAdapter(adapter);     
   }
 }

5 个答案:

答案 0 :(得分:6)

我发现了路。这是我们如何做到的,让最后一行是

list.add("");

并添加以下代码:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item, list) {

                        @Override
                        public View getView(int position, View convertView, ViewGroup parent) {

                                View v = super.getView(position, convertView, parent);
                                if (position == getCount()) {
                                    ((TextView)v.findViewById(android.R.id.text1)).setText("");
                                    ((TextView)v.findViewById(android.R.id.text1)).setHint(getItem(getCount())); //"Hint to be displayed"
                                }

                                return v;
                            }       

                            @Override
                            public int getCount() {
                                return super.getCount()-1;            // you don't display last item. It is used as hint.
                            }

                    };


                    adapter.setDropDownViewResource(R.layout.custom_spinner_list);
                    spinner.setAdapter(adapter);
                    spinner.setSelection(adapter.getCount());        //set the hint the default selection so it appears on launch.
                    spinner.setOnItemSelectedListener(this);
                    notifyDataSetChanged();        

答案 1 :(得分:1)

public class ModelTestRequest
{
    List<ModelTestChild> GoodList { get; set; }

}
public class ModelTestChild
{
    public int id { get; set; }
    public string description { get; set; }
}
public class Modeltestresponse
{
    public List<ModelTestChild> GoodList2 { get; set; }
} 


public Modeltestresponse SpecialName(ModelTestRequest model)
{

}

答案 2 :(得分:1)

在SearchableSpinner(com.toptoche.searchablespinnerlibrary.SearchableSpinner)中,有一个名为“ app:hintText”的参数-您可以根据需要使用它。例如:

<com.toptoche.searchablespinnerlibrary.SearchableSpinner
            android:id="@+id/vehicle_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/spinner_background"
            android:layout_marginStart="20dp"
            android:layout_marginEnd="20dp"
            android:layout_marginBottom="20dp"
            app:hintText=" "/>

从微调框列表中选择某项后,提示将消失。

答案 3 :(得分:0)

感谢上面的用户45678,我写了这个

public class SpinnerNoDefault extends Spinner {

public void initialize(List<String> items) {
    initialize(items, android.R.layout.simple_spinner_item, android.R.layout.simple_spinner_dropdown_item);
}

public void initialize(List<String> items, @LayoutRes int mItemResId, @LayoutRes int dropdownResId) {
    List<String> itemsWithEmpty = new ArrayList<>(items);
    itemsWithEmpty.add("");

    String[] choicesArray = itemsWithEmpty.toArray(new String[itemsWithEmpty.size()]);
    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getContext(), mItemResId, choicesArray) {
        @NonNull
        @Override
        public View getView(int position, View convertView, @NonNull ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
            if (position == getCount()) {
                ((TextView)v.findViewById(android.R.id.text1)).setText(null);
                ((TextView)v.findViewById(android.R.id.text1)).setHint(parent.getContext().getString(R.string.spinner_no_default_hint));
            }

            return v;
        }

        @Override
        public int getCount() {
            // Don't display last item. It is used as hint.
            return super.getCount()-1;
        }
    };

    spinnerArrayAdapter.setDropDownViewResource(dropdownResId);
    setAdapter(spinnerArrayAdapter);
    setSelection(itemsWithEmpty.size()-1);
}
}

答案 4 :(得分:0)

首先。初始化微调器时,添加

spinner.setSelection(0, false)

我用选定的项目创建了此适配器:

abstract  class SpinnerSelectAdapter<T>(
    context: Context,
    private val resource: Int,
    @ColorRes private  val unselectedColorResource: Int,
    @ColorRes private val selectedColorResource: Int,
    open var objects: List<T>
) :
    ArrayAdapter<T>(context, resource, objects) {

    var selectedPosition = -1

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val view = convertView ?: LayoutInflater.from(parent.context)
            .inflate(resource, parent, false)
        if(selectedPosition != -1) {
            setView(view, getItem(position)!!)
        }
        return view
    }

    override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
        val view = convertView ?: LayoutInflater.from(parent.context)
            .inflate(resource, parent, false)
        if(selectedPosition == position){
            view.setBackgroundResource(selectedColorResource)
        }else {
            view.setBackgroundResource(unselectedColorResource)
        }
        setView(view, getItem(position)!!)
        return view
    }

    open fun setView(view: View, item: T) {

    }

    open fun setEmptyView(view: View){

    }


    override fun getItem(position: Int): T? {
        return objects[position]
    }


    override fun getCount(): Int {
        return objects.size
    }
}

然后,我为从SpinnerSelectAdapter继承的字符串值创建了适配器

class SpinnerStringAdapter(
    context: Context,
    resource: Int,
    @ColorRes unselectedColorResource: Int,
    @ColorRes selectedColorResource: Int,
    override var objects: List<String>
) :
    SpinnerSelectAdapter<String>(
        context,
        resource,
        unselectedColorResource,
        selectedColorResource,
        objects
    ) {


    override fun setView(view: View, item: String) {
        val txt = view.findViewById<TextView>(android.R.id.text1)
        txt.text = item
    }

}

然后在您的片段或活动中

       val adapter = SpinnerStringAdapter(
           requireContext(),
           R.layout.spinner_item,
           R.color.colorBG,
           R.color.colorSubBG,
           types
       )
       spinner.adapter = typeAdapter
       spinner.setSelection(0, false)
       spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
           override fun onItemSelected(
               parent: AdapterView<*>?,
               view: View?,
               position: Int,
               id: Long
           ) {
               adapter.selectedPosition = position
               adapter.notifyDataSetChanged()
           }
``