如何设置当前微调器文本而不更改关联的选择列表项

时间:2014-08-08 15:40:07

标签: android spinner

我有一个带有一些值的Spinner

| Monday       | 
| Thuesday     |
| Wednesday    |
| Thursday     |
| Friday       |
| Saturday     |
| USER DEFINED |

当用户选择USER DEFINED时,他可以在对话框中输入自定义值,假设我将此值设为String userDef="Your choice"

我需要将此String设置为当前项,而不更改微调器选择列表,该列表必须与上述相同,当用户再次单击微调器时,类似于Google Analytics Android App,请参见图像。

未点击的微调器 Unclicked spinner Google Analytics 点击微调器 clicked spinner Google Analytics

我怎么能这样做?

3 个答案:

答案 0 :(得分:15)

实现这一点的关键细节是Spinner使用的SpinnerAdapter接口有两种不同但相关的方法:

因此,要使一个项目在弹出窗口中与在微调器本身中显示不同,您只需以不同方式实现这两种方法。根据代码的具体情况,细节可能会有所不同,但一个简单的例子就是:

public class AdapterWithCustomItem extends ArrayAdapter<String>
{
    private final static int POSITION_USER_DEFINED = 6;
    private final static String[] OPTIONS = new String[] {
        "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Custom..." };

    private String mCustomText = "";

    public AdapterWithCustomItem(Context context){
        super(context, android.R.layout.simple_spinner_dropdown_item, OPTIONS);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        if (position == POSITION_USER_DEFINED) {
            TextView tv = (TextView)view.findViewById(android.R.id.text1);
            tv.setText(mCustomText);
        }

        return view;
    }

    public void setCustomText(String customText) {
        // Call to set the text that must be shown in the spinner for the custom option.
        mCustomText = customText;
        notifyDataSetChanged();
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        // No need for this override, actually. It's just to clarify the difference.
        return super.getDropDownView(position, convertView, parent);
    }
}

然后,当用户输入自定义值时,您只需要使用要显示的文本调用适配器上的setCustomText()方法。

mAdapter.setCustomText("This is displayed for the custom option");

产生以下结果:

enter image description here

由于您只是覆盖了getView()方法,因此下拉菜单仍显示与选项本身中定义的文本相同的文本。

答案 1 :(得分:2)

我认为最好的方法是实现自定义数组适配器。首先为每个条目创建一个类:

public class Choice {
    // Represents the underlying value
    public String value;

    // Represents the user-displayed value
    public String text;

    public Choice(String value, String text) {
        this.value = value;
        this.text = text;
    }

    // Only the text will be shown, not the underlying value
    @Override
    public String toString() {
        return text;
    }
}

然后声明Choice个对象的适配器:ArrayAdapter<Choice>。只显示定义的文本,并且只要选择了项目,就可以访问基础值:

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
    int position, long arg3) {
    Choice choice = adapter.get(position);
    // Set the value of the choice, not the text
    myValue = choice.value;
}

答案 2 :(得分:0)

您可以将USER DEFINED字符串添加到数组列表&gt;

调用notifyDatasetChanged()&gt;

获取新数组列表的大小&gt;

然后致电

         spinner.setSelection(Index of USER DEFINED);

          spinner.setSelection(Arraylist.size()-1);