Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将微调器适配器设置为null_Android_Android Spinner - Fatal编程技术网

Android 将微调器适配器设置为null

Android 将微调器适配器设置为null,android,android-spinner,Android,Android Spinner,我要加载一个android微调器来做一些选择,我正在向它加载数据。有一种情况,我想将要在微调器中加载的数据设置为null。这有助于我选择职位sizeSP.setSelection0; 但是我想将加载的数据设置为null。因此,当单击微调器时,它不应该发生任何事情。检查这一点,一个家伙通过创建自定义类适配器给出了一个完美的答案 /** * A modified Spinner that doesn't automatically select the first entry in the li

我要加载一个android微调器来做一些选择,我正在向它加载数据。有一种情况,我想将要在微调器中加载的数据设置为null。这有助于我选择职位sizeSP.setSelection0; 但是我想将加载的数据设置为null。因此,当单击微调器时,它不应该发生任何事情。

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

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

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

声明ArrayAdapter适配器=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);

使用spinner.setClickablefalse;我想将数据设置为null;但是我记不起我在哪里看到过它在本期中的使用。该链接可能会给出答案,但如果将来该链接不可用,会发生什么情况…我不喜欢从别人的帖子中复制答案,所以我给出了该链接。仍然是为了您的满意,以下是编辑后的答案@vishwajitplankar:-cheers如果从微调器中选择了一个项目,是否可以通过选择提示来撤消该项目
adapter.clear();