如何设置微调位置? [英] How to set position in spinner?

查看:309
本文介绍了如何设置微调位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读与系统文件的BufferedReader文本;这个文本中包含,例如,5个字,但在另一种情况下它可以包含更少或更多的单词。然后我把这个文本(提话)合并为一个字符串,并保存该字符串的共享preferences。然后,我从这个字符串微调。 code是如下:

I read with BufferedReader text from a system file; this text contains, for example, 5 WORDS, but in another case it can contain fewer or more words. Then I put this text (the mentioned words) into a SINGLE string and saved that string to shared preferences. Then I made a spinner from this string. Code is as follows:

Spinner spinner = new Spinner(this);
    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, yourString.split(" "));
    spinner.setAdapter(spinnerArrayAdapter);

然后我读从另一个文件中的文本。该文本总是包含一个单词。而这个词是一样的,我的第一个文件读了一个关键词(例如,如果这是我读的第一个文件包含5个字和这些词之一是黑,这一点我也可以参考第二个文件包含黑)。 我需要让这个特殊的词(它存在于两个文件)在我的飞旋的默认选择的选项。

例如:

第一个字符串包含:红,蓝,黄,黑,白

First string contains: red, blue, yellow, black, white

二字符串包含:黄

我做的第一个字符串微调所以在微调选项填充完全是这样的:红,蓝,黄,黑,白,默认选中的选项是红色的(因为它恰好是第一个在我的第一次字符串),但我需要让黄如选择在这种情况下,选择默认的,因为第二个字符串包含黄。 这两个字符串中的单词总是不同的。

I make a spinner from first string so options in spinner are populated exactly like this: "red, blue, yellow, black, white" and the default selected option is red (because it happens to be the first one in my first string), but I need to make yellow as the default selected option in this case, because second string contains "yellow". The words in both string are always different.

BTW:我知道如何保存在一个微调的位置,但我不知道如何,如果我比较两个字符串设定位置的微调,其中一个包含多个字

BTW: I know how to save the position in a spinner, but I don't know how to set the position in a spinner if I compare two strings and one of them contains more words.

推荐答案

下面是解决方案,这要归功于sfratini寻求帮助。

Here is solution, thanks to sfratini for help.

使用:

spinner.setSelection(getIndex(spinner, myString));

然后:

private int getIndex(Spinner spinner, String myString){

        int index = 0;

        for (int i=0;i<spinner.getCount();i++){
            if (spinner.getItemAtPosition(i).equals(myString)){
                index = i;
            }
        }
        return index;
}

这篇关于如何设置微调位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆