【问题标题】:Spinner does not show selected value微调器不显示所选值
【发布时间】:2013-12-19 16:49:06
【问题描述】:

我已经通过数据库填充数组列表来实现微调器。我可以在微调器数组适配器中获取并显示数组列表,但是如果我在微调器中选择项目,它不会在微调器中显示?我在这里有什么错误?

这是我的代码,

 Spinner spinner1 = (Spinner) findViewById(R.id.prospin);
     ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, providerlist);

  adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);

我用这个得到了选中的项目字符串,

Spinner provid = (Spinner)findViewById(R.id.prospin);
String provider =provid.getSelectedItem().toString();

谁能帮帮我!!!

【问题讨论】:

    标签: android arraylist spinner selected


    【解决方案1】:

    这个答案可能有点愚蠢,但请尝试一下。它对我有用。

    1. 检查微调器的背景颜色!
    2. 如果它是白色的,请更换它
    3. 尽情享受吧!

    【讨论】:

    • 你成就了我的一天
    • 哈哈,这解决了我的问题 :) 但如果你想更改文本颜色,请参阅 here
    • 太棒了!对我来说绝对没问题!谢谢:)
    • 如果我更改了背景颜色,我的微调器消失了,我该怎么做?
    • 为此损失了 1 小时。微调器显示在模拟器中,但不在我的设备上。颜色问题!
    【解决方案2】:

    我遇到了同样的问题,通过在 Spinner 中绑定数据后添加notifyDataSetChanged() 解决了。

    首先,我将适配器与 Blank ArrayList 绑定,然后从服务器获取项目列表并添加到该列表中,但在更新列表后忘记了 notifyDataSetChanged()

    更新列表后添加adapter.notifyDataSetChanged();即可。

    希望对您有所帮助。

    【讨论】:

    • 这个答案很难理解,如果您提供示例代码可能会更有帮助。
    • 我不这么认为,因为一切都可以通过逐行阅读来理解。
    【解决方案3】:

    问题:

    微调器既不显示默认值也不显示选定项目值。但是选择时会显示下拉菜单项。

    原因:

    背景和文字颜色都是白色!!!

    解决方案:

    xml(首选):

    为微调器项目编写自定义布局并使用它来代替默认布局android.R.layout.simple_spinner_item

    How to change spinner text size and text color?

    代码(不太可靠):

    your_spinner_instance.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
        public void onItemSelected(AdapterView<?> parent, View view, int pos,
                                   long id) {
            ((TextView) view).setTextColor(Color.RED);
        }
        public void onNothingSelected(AdapterView<?> parent) {
        }
    
    });
    

    Android 需要一些重大更新,否则 dart 和 Flutter 应该会接管...

    感谢卡特鲁克

    【讨论】:

    • 您可以使用自定义的简单微调器项目。只需将您的文本视图添加到其中: style="?android:attr/spinnerItemStyle"
    【解决方案4】:

    使用wrap_content 作为Spinner 的高度。

    可能它没有足够的高度来显示文本。

    【讨论】:

      【解决方案5】:

      如果没有正确给出上下文,也会发生这种情况。我在需要getBaseContext()的地方使用getApplicationContext()

      【讨论】:

      【解决方案6】:

      如果您有自定义适配器,则应更改 TextView 文本颜色

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          TextView view = (TextView) super.getView(position, convertView, parent);
          view.setTextColor(Color.parseColor("#000000"));
          return view;
      }
      

      如果您没有自定义适配器,则只需更改微调器背景

      【讨论】:

        【解决方案7】:

        我对宽度和高度使用硬编码值。改成 wrap_content 后就可以了。

        【讨论】:

          【解决方案8】:

          对我来说,问题是我使用的是getApplicationContext()。当我将getApplicationContext() 替换为this 时,它工作正常。

          ArrayAdapter<*> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, documentsCategories);
          

          【讨论】:

            【解决方案9】:

            在您声明的 Spinner 元素的 xml

            中使用 android:spinnerMode="dropdown" 属性

            【讨论】:

            • 这是最好的解决方案!其他的对我不起作用。
            【解决方案10】:

            我刚遇到这个问题,在尝试了所有列出的解决方案后发现问题在于我将微调器 layout_width 设置为 60dp

            将其更改为 fill_parent 并解决问题。

            【讨论】:

            • 布局对我来说也是个问题:似乎微调器在没有足够的水平空间时被剪断,并且至少只显示插入符号。
            【解决方案11】:

            例如添加背景颜色白色,详细信息将显示 例如

            <Spinner
                android:id="@+id/size_spinner"
               style="@style/FormTextStyle"
                android:drawSelectorOnTop="true"
                android:layout_marginStart="@dimen/default_gap"
                android:background="@android:color/white"
                />
            

            【讨论】:

            • 能否添加更完整的答案。用户可能不知道在哪里进行您建议的更改,并且您的句子的构造方式不太容易理解。
            【解决方案12】:

            这种情况偶尔会发生,我想这是一个错误,但有一个解决方法很有趣,但就是这样做,

            在微调器的属性栏中将背景设置为不同的颜色,

            在你有微调器的 .xml 中,设置:

            android:background="@android:drawable/spinner_background_material"
            

            运行代码会产生一个错误提示背景是私有的

            删除.xml 中android:background 的行并再次运行代码,您的微调器应该可以正常显示所有值,如果它不采取额外步骤将背景色调设置为不同的颜色,最好是黑色.

            再次运行。

            【讨论】:

            • 确实会产生错误:AAPT: error: resource android:drawable/spinner_background_material is private。但是再次删除线路并构建并不能解决我的问题。
            【解决方案13】:

            我遇到了同样的问题,但没有一个解决方案对我有用。最后发现问题是微调器是通过异步任务从 firestore db 填充的。 重新排列代码,以便在异步任务完成后设置适配器,瞧!!

            【讨论】:

              【解决方案14】:

              检查这些链接:

              1. http://www.technotalkative.com/android-spinner-example/
              2. http://www.mkyong.com/android/android-spinner-drop-down-list-example/

              否则,请尝试在选择项目时将其存储:

              spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
              
                          @Override
                          public void onItemSelected(AdapterView<?> adapter, View v,
                                  int position, long id) {
                              // On selecting a spinner item
                              String item = adapter.getItemAtPosition(position).toString();
              
                              // Showing selected spinner item
                              Toast.makeText(getApplicationContext(),
                                      "Selected Country : " + item, Toast.LENGTH_LONG).show();
                          }
              
                          @Override
                          public void onNothingSelected(AdapterView<?> arg0) {
                              // TODO Auto-generated method stub
              
                          }
                      });
              

              【讨论】:

              • 它不要求获取所选项目的值。它说所选项目未在布局上绘制。我有同样的问题,我找不到答案。只显示微调器的背景,当您从下拉列表中选择一个项目时,没有任何反应。
              【解决方案15】:

              试试这个。

               final ArrayList<String> providerlist= new ArrayList<String>();
                  Spinner spinner1 = (Spinner) findViewById(R.id.prospin);
                  ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, providerlist);
              
                  adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                  spinner1.setAdapter(adapter1);
                  spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                      @Override
                      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
              
                                          // On selecting a spinner item
                          String item = providerlist.get(position);
              
                          // Showing selected spinner item
                          Toast.makeText(this,
                                  "Selected Country : " + item, Toast.LENGTH_LONG).show();
                      }
              
                      @Override
                      public void onNothingSelected(AdapterView<?> parent) {
              
                      }
                  });
              

              【讨论】:

              • 这不会在微调器中显示所选项目。它仅显示包含所选项目文本的 toast。微调器保持空白。
              【解决方案16】:

              通常在使用微调器时默认选择它们,因此尝试将空白或任何其他数据设置为第一个位置为零的位置,您将获得所选项目的确切位置。

              【讨论】:

                【解决方案17】:

                我发现的问题是样式表。使用这个

                <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
                  <!-- Customize your theme here. -->
                  <item name="windowNoTitle">false</item>
                  <item name="windowActionBar">true</item>
                  <item name="colorPrimary">@color/colorPrimary</item>
                  <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
                  <item name="colorAccent">@color/colorAccent</item>
                </style> 
                

                对于 xml 布局使用这个

                <?xml version="1.0" encoding="utf-8"?>
                <ScrollView
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:tools="http://schemas.android.com/tools"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:fitsSystemWindows="true"
                    android:paddingBottom="5dp"
                    style="@style/AppTheme">
                
                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:paddingTop="5dp"
                        android:paddingLeft="24dp"
                        android:paddingRight="24dp">
                
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Spinner"
                            android:layout_marginTop="10dp"
                            android:textColor="@color/colorBlack"/>
                
                        <Spinner
                            android:id="@+id/Spinner"
                            android:layout_width="fill_parent"
                            android:layout_height="50dp"
                            android:backgroundTint="@color/colorPrimary"
                            android:textColorHint="#05ab9a"
                            android:padding="15dp"
                            style="@style/Base.Widget.AppCompat.Spinner.Underlined"
                            tools:targetApi="lollipop" />
                    </LinearLayout>
                </ScrollView>
                

                最后是班级

                String [] NUMBERS= {"3 ","6 ","13 "};
                
                Spinner s_spinner = (Spinner) findViewById(R.id.Spinner);
                
                ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(this,
                                    android.R.layout.simple_dropdown_item_1line, NUMBERS);
                
                // Specify the layout to use when the list of choices appears 
                           spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                
                // attaching data adapter to spinner
                s_spinner.setAdapter(spinnerAdapter );
                

                【讨论】:

                  【解决方案18】:

                  这个答案可能有点愚蠢,但如果您犯了同样的错误,请尝试... 首先为您的 ArrayList 设置值,然后将该 arrayList 分配给微调器。 我声明了一个全局数组列表并首先将其设置为微调器,然后从另一种方法向它添加值......当时我遇到了同样的问题。否则,您可以对 arrayList 执行 notifyDataSetChanged()。

                  【讨论】:

                    【解决方案19】:

                    试试这个代码==>

                    ArrayAdapter<String> stateNameAdaptor = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, stateNameList);  
                         stateNameAdaptor.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    
                    spnState.setAdapter(stateNameAdaptor);
                    

                    【讨论】:

                      【解决方案20】:

                      奇怪。我有同样的问题。我做了什么使它工作: 1. 将一些初始数据添加到列表中(ex.---请选择- -) 2.加载其余数据并将其添加到列表中 3.调用adapter.notifyDatasetChaged()

                      【讨论】:

                        【解决方案21】:

                        在我的情况下,该列表是在提供给适配器构造之前从 firestore 填充的。该列表已填充并显示在微调器中。但列表的大小返回为零。所以我只能从列表中选择项目,但微调器文本中没有显示,无法调用 onitemselected 部分。我之前对查询快照进行了回调,并从快照中填充了列表。它仅导致将项目添加到微调器但无法选择。我通过从存储库类中获取列表的回调而不是查询快照来解决这个问题,之后我的列表填充得很好,它的大小是正确的,并且微调器工作正常..我的代码附在这里..希望这对某人有所帮助。 ..

                        //interface
                         public interface SprRbaCallback{
                                void onSprRbaCallback(List<String> list);
                            }
                        //Repository class
                         public void getRBA(final SprRbaCallback sprRbaCallback){
                        
                                rbaCollection.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                                    @Override
                                    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                        
                                        for(DocumentSnapshot snapshot : queryDocumentSnapshots){
                                            String item = (String) snapshot.get("dealerName");
                                            assert item != null;
                                            Log.d(TAG, item);
                        
                                            list.add(item);
                                        }
                                        Log.d(TAG, String.valueOf(list.size()));
                                        sprRbaCallback.onSprRbaCallback(list);
                                    }
                                }).addOnFailureListener(new OnFailureListener() {
                                    @Override
                                    public void onFailure(@NonNull Exception e) {
                                        Log.d(TAG, e.toString());
                                    }
                                });
                        } 
                        
                        // calling method..
                        
                         private void addItemsToRBASpinner() {
                                firebaseRepository.getRBA(new FirebaseRepository.SprRbaCallback() {
                                    @Override
                                    public void onSprRbaCallback(List<String> list) {
                                        int size = list.size();
                                        Log.d(TAG + "listsize", String.valueOf(size));
                        
                        
                                        ArrayAdapter<String> sprAdaptSelectRBA = new ArrayAdapter<String>(CreateAccountActivity.this,
                                                R.layout.spinneritem, list);
                        
                                        sprAdaptSelectRBA.setDropDownViewResource(R.layout.rbaspinner_dropdown);
                                        spr_rbaSelect.setAdapter(sprAdaptSelectRBA);
                        
                                        sprAdaptSelectRBA.notifyDataSetChanged();
                        
                                        spr_rbaSelect.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                            @Override
                                            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                                Log.d("NOBY", "ENTERED ON ITEMSELETED");
                        
                                                txt_selectedRBA = (String) parent.getItemAtPosition(position);
                                                Toast.makeText(parent.getContext(), "Selected RBA: " + txt_selectedRBA, Toast.LENGTH_LONG).show();
                        
                                                Log.d("NOBY", txt_selectedRBA);
                        
                                            }
                        
                                            @Override
                                            public void onNothingSelected(AdapterView<?> parent) {
                                                Log.d("NOBY", "ENTERED NOTHING SELECTED");
                                                txt_selectedRBA = "Dixon";
                                            }
                                        });
                                    }
                                });
                            }
                        

                        这段代码对我来说很好用...希望对某人有所帮助...:)

                        【讨论】:

                          【解决方案22】:

                          您应该使用第一个 Spinner 来获取值。

                          试试下面的代码:

                          String provider = spinner1.getSelectedItem().toString();
                          

                          您正在做的是获取微调器的默认值。

                          【讨论】:

                            【解决方案23】:

                            试试这个代码

                            ArrayAdapter<String> arrayAdapte=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,states);
                                arrayAdapte.setDropDownViewResource(android.R.layout.simple_list_item_1 );
                                spinnerState.setAdapter(arrayAdapte);
                            
                                String data=spinnerState.getSelectedItem().toString(); // this is select particular item from list;
                            
                                int position=spinnerState.getSelectedItemPosition(); // this return position of data selected in list; 
                            

                            【讨论】:

                              【解决方案24】:
                              out_marginLeft="50dp"
                                          android:layout_marginTop="50dp"
                                          android:layout_marginRight="50dp"
                                          android:gravity="center"
                                          android:orientation="vertical">
                              
                                          <ProgressBar
                                              android:id="@+id/progressBar"
                                              style="?android:attr/progressBarStyleHorizontal"
                                              android:layout_width="match_parent"
                                              android:layout_height="wrap_content"
                                              android:layout_above="@+id/txtProgress"
                                              android:layout_centerHorizontal="true"
                                              android:layout_marginLeft="16dp"
                                              android:layout_marginRight="16dp"
                                              android:progress="50"
                                              android:progressTint="@android:color/black" />
                              
                                          <TextView
                                              android:id="@+id/txtProgress"
                                              android:layout_width="wrap_content"
                                              android:layout_height="wrap_content"
                                              android:layout_marginTop="8dp"
                                              android:text="Progress"
                                              android:textColor="@android:color/black" />
                                      </LinearLayout>  
                              
                              Java
                              

                              【讨论】:

                                猜你喜欢
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                相关资源
                                最近更新 更多