将Spinner适配器设置为null

时间:2015-04-28 11:16:36

标签: android android-spinner

我有一个Android微调器加载来做一些选择,我正在加载数据。有一种情况我想将数据设置为在spinner中加载为null。这有助于我选择位置sizeSP.setSelection(0); 但我想将数据设置为加载为null。因此,当点击微调器时,它不应该是任何东西。

2 个答案:

答案 0 :(得分:0)

检查这个Link,一个人通过创建自定义类适配器给出了完美的答案。

 /**
 * A modified Spinner that doesn't automatically select the first entry in the list.
 *
 * Shows the prompt if nothing is selected.

 * Limitations: does not display prompt if the entry list is empty.
 */
public class NoDefaultSpinner extends Spinner {

public NoDefaultSpinner(Context context) {
    super(context);
}

public NoDefaultSpinner(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public NoDefaultSpinner(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
public void setAdapter(SpinnerAdapter orig ) {
    final SpinnerAdapter adapter = newProxy(orig);

    super.setAdapter(adapter);

    try {
        final Method m = AdapterView.class.getDeclaredMethod(
                           "setNextSelectedPositionInt",int.class);
        m.setAccessible(true);
        m.invoke(this,-1);

        final Method n = AdapterView.class.getDeclaredMethod(
                           "setSelectedPositionInt",int.class);
        n.setAccessible(true);
        n.invoke(this,-1);
    } 
    catch( Exception e ) {
        throw new RuntimeException(e);
    }
}

protected SpinnerAdapter newProxy(SpinnerAdapter obj) {
    return (SpinnerAdapter) java.lang.reflect.Proxy.newProxyInstance(
            obj.getClass().getClassLoader(),
            new Class[]{SpinnerAdapter.class},
            new SpinnerAdapterProxy(obj));
}



/**
 * Intercepts getView() to display the prompt if position < 0
 */
protected class SpinnerAdapterProxy implements InvocationHandler {

    protected SpinnerAdapter obj;
    protected Method getView;


    protected SpinnerAdapterProxy(SpinnerAdapter obj) {
        this.obj = obj;
        try {
            this.getView = SpinnerAdapter.class.getMethod(
                             "getView",int.class,View.class,ViewGroup.class);
        } 
        catch( Exception e ) {
            throw new RuntimeException(e);
        }
    }

    public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
        try {
            return m.equals(getView) && 
                   (Integer)(args[0])<0 ? 
                     getView((Integer)args[0],(View)args[1],(ViewGroup)args[2]) : 
                     m.invoke(obj, args);
        } 
        catch (InvocationTargetException e) {
            throw e.getTargetException();
        } 
        catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    protected View getView(int position, View convertView, ViewGroup parent) 
      throws IllegalAccessException {

        if( position<0 ) {
            final TextView v = 
              (TextView) ((LayoutInflater)getContext().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE)).inflate(
                  android.R.layout.simple_spinner_item,parent,false);
            v.setText(getPrompt());
            return v;
        }
        return obj.getView(position,convertView,parent);
    }
 }
}

答案 1 :(得分:0)

为微调器设置自定义适配器

ArrayAdapter<String> adapter = null;声明为全局

设置适配器

String[] items = new String[] {"a", "b", "c"};
Spinner spinner = (Spinner) findViewById(R.id.mySpinner);
adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

清除需要空白的适配器

adapter.clear();