如何在微调器中设置位置?

2023-07-08移动开发问题
4

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

问题描述

我使用 BufferedReader 从系统文件中读取文本;例如,此文本包含 5 个单词,但在另一种情况下,它可以包含更少或更多的单词.然后我将此文本(提到的单词)放入一个 SINGLE 字符串并将该字符串保存到共享首选项中.然后我用这个字符串做了一个微调器.代码如下:

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 个词,其中一个词是黑色",那么我读的第二个文件也是包含黑色").我需要将这个特定的词(两个文件中都存在)作为我微调器中的默认选择选项.

Then I read text from another file. This text always contains one word. And this word is the same as one of the words which I had read from the first file (for example, if the first file which I read contained 5 words and one of these words was "black", the second file which I read also contains "black"). And I need to make this particular word (which exists in both files) as the default chosen option in my spinner.

例如:

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

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

第二个字符串包含:黄色

Second string contains: yellow

我从第一个字符串制作了一个微调器,因此微调器中的选项完全像这样填充:红色,蓝色,黄色,黑色,白色",默认选择的选项是红色(因为它恰好是我第一个string),但在这种情况下我需要将黄色作为默认选择选项,因为第二个字符串包含黄色".两个字符串中的单词总是不同的.

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: 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;
}

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

The End

相关推荐

从 Documents 目录存储和读取文件 iOS 5
Storing and reading files from Documents directory iOS 5(从 Documents 目录存储和读取文件 iOS 5)...
2024-08-12 移动开发问题
9

Cocos2d 播放2种不同的背景音乐文件或循环播放效果
Cocos2d play 2 different background music files or loop playEffect(Cocos2d 播放2种不同的背景音乐文件或循环播放效果)...
2024-08-12 移动开发问题
30

为什么 xcode 归档成功但创建一个空的 .xcarchive 文件?
Why does xcode archive succeed but create an empty .xcarchive file?(为什么 xcode 归档成功但创建一个空的 .xcarchive 文件?)...
2024-08-12 移动开发问题
2

Cocos2d 字体:如何为 CCLabelBMFont 创建 .fnt 文件?(任何免费的位图字体创建工具?)
Cocos2d font : How to create .fnt file for CCLabelBMFont? ( Any free bitmap font creation tool ? )(Cocos2d 字体:如何为 CCLabelBMFont 创建 .fnt 文件?(任何免费的位图字体创建工具?))...
2024-08-12 移动开发问题
2

在 MPMoviePlayerController 中播放 mov 文件时出现问题
Problem playing mov file in MPMoviePlayerController(在 MPMoviePlayerController 中播放 mov 文件时出现问题)...
2024-08-12 移动开发问题
9

Cocos2d - 设置设备/屏幕方向
Cocos2d - Setting Device/Screen Orientation(Cocos2d - 设置设备/屏幕方向)...
2024-08-12 移动开发问题
19