如何将旋转器位置设置为从不同片段选择的选择中选定的项目

发布于 2025-01-29 23:17:01 字数 395 浏览 4 评论 0原文

添加颜色旋转器

更新片段

这是我第一次问一个问题,我是一个新的编码器,并且一直在与Kotlin和Java一起尝试Android。我有一个应用程序,允许用户添加一件衣服,这些选项是使用旋转器设置的。问题是当用户从旋转器中选择一个项目时,我想更新值(如图像)显示蓝色单词,说棕色是选项的选项,添加服装项目旋转器,但旋转器显示灰色显示灰色我可以将旋转器设置为棕色而不是默认值。

Add color spinner

the update fragment

this is my first time asking a question and I am a new coder and been trying out android with kotlin and java. I have an app that allows a user to add an item of clothing the options are set up with spinners. The issue is when the user selects an item from the spinner it saves and when I want to update the values like the image shows the blue words saying brown is the option selected form the add clothing item spinner but the spinner is showing grey is there a way I can set the spinner to be on brown and not the default value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情痴 2025-02-05 23:17:01

是的。您需要使用getItematPosition()方法。从您的用户界面中,您要检索的对象是字符串。因此,如果您在Java中编写代码。知道您已经使用了文本视图来显示选择

java

TextView clotheColor = findViewById(R.id.whatever_the_id_is);

String selectedColor = "";

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectedColor = parent.getItemAtPosition(position).toString();
                Log.i(TAG, selectedColor);

                switch (selectedSpinnerItem) {
                    case "Brown":
                        
                    case "Red":
                    case "Blue":
                        clotheColor.SetText(selectedColor);
                        break;
                    default:
                        Log.i(TAG, "");

                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Log.i(TAG, "Nothing has been selected");
            }
        });

kotlin

您可以从这里将代码转换为Kotlin。阅读此。
如何转换Java的一部分源文件到Kotlin?

Yes. You need to use the getItemAtPosition() method. From your user interface, the Object you are trying to retrieve is a String. So if you wrote your code in Java. Knowing you've used a TextView to display the selection

Java

TextView clotheColor = findViewById(R.id.whatever_the_id_is);

String selectedColor = "";

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectedColor = parent.getItemAtPosition(position).toString();
                Log.i(TAG, selectedColor);

                switch (selectedSpinnerItem) {
                    case "Brown":
                        
                    case "Red":
                    case "Blue":
                        clotheColor.SetText(selectedColor);
                        break;
                    default:
                        Log.i(TAG, "");

                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Log.i(TAG, "Nothing has been selected");
            }
        });

KOTLIN

You can convert the code to Kotlin from here. Read this.
How can I convert a part of Java source file to Kotlin?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文