本文介绍了微调与空选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个空选择一个微调,但actualy它显示从飞旋的选择的第一个项目。如果我添加空值到我的字符串,它是在飞旋的选择源,再经过微调打开时显示空行。我应该怎么办呢?下面是code我使用:

 的String [] CH = {SESSION1,会话2,会议3};
  微调SP;
  TextView的sess_name;
       SP =(微调)findViewById(R.id.spinner1);
       sess_name =(TextView中)findViewById(R.id.sessname);
      ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(这一点,android.R.layout.simple_spinner_item,CH);
       sp.setAdapter(适配器);

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

     sp.setOnItemSelectedListener(新AdapterView.OnItemSelectedListener()
    {
    @覆盖
    公共无效onItemSelected(适配器视图<>为arg0,查看ARG1,
    INT ARG2,长ARG3)
    {
    INT指数= arg0.getSelectedItemPosition();
    sess_name.setText(CH [指数]);

     Toast.makeText(getBaseContext(),
    您选择的项目:+ CH [指数]
    Toast.LENGTH_SHORT).show();
    }
 

解决方案

巴拉克的解决方案有问题,当你再选择第一项,微调不会叫OnItemSelectedListener的onItemSelected方法,并刷新空内容,因为previous位置和您的选择这两个位置是0。

先放一个空字符串,在你的字符串数组的开始,例如:

 的String []测试= {,一,二,三};
 

打造adatper,不修改getView(),修改getDropDownView(),设置为空观的高度,以1px的

 私有类MyArrayAdapter扩展ArrayAdapter<字符串> {

    私人诠释textViewResourceId;

    公共MyArrayAdapter(上下文的背景下,INT textViewResourceId,字符串[]对象){
        超(背景下,textViewResourceId,对象);
        this.textViewResourceId = textViewResourceId;

    }


    @覆盖
    公共查看getDropDownView(INT位置,查看convertView,ViewGroup中父){
        查看图。
        TextView的文字;

        如果(convertView == NULL){
            鉴于= getLayoutInflater()膨胀(textViewResourceId,父母,假)。
        } 其他 {
            鉴于= convertView;
        }


        文=(TextView的)观点;
        字符串项;

        项目=的getItem(位置);


        text.setText((CharSequence的)项目);
        如果(位置== 0){
            ViewGroup.LayoutParams升= text.getLayoutParams();
            l.height = 1;
            text.setLayoutParams(升);
        } 其他 {
            ViewGroup.LayoutParams升= text.getLayoutParams();
            //你可以改变高度值到你
            l.height = 96;
            text.setLayoutParams(升);
        }

        返回查看;
    }
}
 

I'm trying to create a spinner with empty selection, but actualy it displays the first item from the choices of spinner. If I add null value to my string, which is the source of choices in spinner, then after opening spinner that empty row is displayed. How should I do it? Here's code I'm using:

      String[] ch= {"Session1","Session2","Session3"};
  Spinner sp;
  TextView  sess_name;
       sp= (Spinner)findViewById(R.id.spinner1);
       sess_name=(TextView)findViewById(R.id.sessname);
      ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,ch);
       sp.setAdapter(adapter);

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

     sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,
    int arg2, long arg3)
    {
    int index = arg0.getSelectedItemPosition();
    sess_name.setText(ch[index]);

     Toast.makeText(getBaseContext(),
    "You have selected item : " + ch[index],
    Toast.LENGTH_SHORT).show();
    }
解决方案

Barak's solution have problem when you then select the first item, Spinner wont call OnItemSelectedListener's onItemSelected method and refresh the empty content because the previous position and your selection position both is 0.

First put a empty string at the begin of your string array, for example

String[] test={" ","one","two","three"};

build adatper,don't modify getView(), modify getDropDownView(), set the empty View's height to 1px

 private class MyArrayAdapter extends ArrayAdapter<String> {

    private int textViewResourceId;

    public MyArrayAdapter(Context context, int textViewResourceId, String[] objects) {
        super(context, textViewResourceId, objects);
        this.textViewResourceId = textViewResourceId;

    }


    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        View view;
        TextView text;

        if (convertView == null) {
            view = getLayoutInflater().inflate(textViewResourceId, parent, false);
        } else {
            view = convertView;
        }


        text = (TextView) view;
        String item;

        item = getItem(position);


        text.setText((CharSequence) item);
        if (position == 0) {
            ViewGroup.LayoutParams l = text.getLayoutParams();
            l.height = 1;
            text.setLayoutParams(l);
        } else {
            ViewGroup.LayoutParams l = text.getLayoutParams();
            //you can change height value to yours
            l.height = 96;
            text.setLayoutParams(l);
        }

        return view;
    }
}

这篇关于微调与空选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:39