如何在Android中使用Spinner实施搜索?

兴奋剂

我的代码Spinner如下:

String[] countryNames = {"Select Country", "India", "China", "Australia",   "Portugle", "America", "New Zealand"};

Spinner spinner = (Spinner) findViewById(R.id.simpleSpinner);
hintAdapter = new CustomArrayAdapter(getApplicationContext(), R.layout.simple_row,countriesList,getApplicationContext());
spinner.setAdapter(hintAdapter);

我想在中实现搜索Spinner

我该如何实现?

谢尔佐贝克·穆罕默德耶夫(Sherzodbek Muhammadiev)

我在这里找到以下解决方案

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 Android 上的 Spinner 中使用 itemClickListener

如何在Android中使用键值实现Spinner?

如何在Android中使用CustomAdapter创建Spinner-list

如何在Recyclerview中使用Spinner?

如何在WebView中使用Spinner?

如何在Spinner Kotlin中使用ArrayAdapter

如何在iOS中使用Braintree实施付款应用

如何在React Native中使用Firebase实施Microsoft登录?

如何在omnet ++中使用AODV协议实施重播攻击?

如何在Spotlight搜索中使用通配符?

如何在php中使用mysql搜索?

如何在Elasticsearch中使用搜索

如何在 Laravel 中使用搜索功能?

如何在java中使用线性搜索?

如何在我的主类中使用Fragment的Spinner事件?

如何在Spinner中使用dataBinding并获取当前值?

如何在 MySQL 的搜索中使用 @ 进行 LIKE 搜索?

如何在Firefox中使用Google搜索而不是OpenDNS搜索

如何在Android中使用默认值设置搜索栏?

如何在Eclipse中使用Android中的材质设计创建搜索栏?

在React Native中使用FlatList实施搜索栏

使用Elasticsearch实施搜索

如何在Android应用中实施FCM?是否有可能在FCM中使用相同的GCM侦听器和接收器方法?

如何在Kotlin中使用Firebase实施电话身份验证?

如何在iOS应用中使用“服务帐户”实施Vision API?

如何在Java EE 8中使用相同的Entity Manager实施通用DAO

如何在yii中使用optgroup实施3级下拉菜单?

如何在reactjs中使用框架显示搜索输入?

如何在ElasticSearch的嵌套查询中使用&堆叠搜索条件