【问题标题】:How to add dynamic data (something like array of strings) to spinner如何将动态数据(类似于字符串数组)添加到微调器
【发布时间】:2015-09-15 10:42:19
【问题描述】:

我有一个微调器,我目前正在其中显示静态数据,我如何显示动态数据(例如:来自字符串数组)或类似适配器的东西,以便当我从服务器接收动态值时,我可以直接提供它旋转器

下面是我的代码:

if(value.contains("Extra")){
                    spinner.setVisibility(View.VISIBLE);
                    image.setVisibility(View.GONE);
                    triangleimage.setVisibility(View.VISIBLE);
                    spinner.setOnItemSelectedListener(Adapter.this);

                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item) {

                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {

                            View v = super.getView(position, convertView, parent);
                            if (position == getCount()) {
                                ((TextView)v.findViewById(android.R.id.text1)).setText("");
                                ((TextView)v.findViewById(android.R.id.text1)).setHint(getItem(getCount())); //"Hint to be displayed"
                            }

                            return v;
                        }       

                        @Override
                        public int getCount() {
                            return super.getCount()-1;            // you don't display last item. It is used as hint.
                        }

                    };

                    adapter.setDropDownViewResource(R.layout.custom_spinner_list);
                    adapter.add("One");
                    adapter.add("Two");
                    adapter.add("Three");
                    adapter.add("Four");
                    adapter.add("Five");
                    adapter.add("Six");
                    adapter.add("");                              //Add empty string in db option at last


                    spinner.setAdapter(adapter);
                    spinner.setSelection(adapter.getCount());     //set the hint the default selection so it appears on launch.
                    spinner.setOnItemSelectedListener(this);

                    notifyDataSetChanged();                       

                }

【问题讨论】:

    标签: android


    【解决方案1】:

    ArrayAdapter中有一个构造函数来传递数组列表作为参数。请参阅下面的示例。

        // Assume this list is from server 
         List<String> your_array_list = new ArrayList<String>();
         your_array_list.add("Test");
         your_array_list.add("Working");
    
         // This is the array adapter, it takes the context of the activity as a  
         // first parameter, the type of list view as a second parameter and your  
         // array as a third parameter. 
         ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                 this, 
                 android.R.layout.simple_list_item_1,
                 your_array_list );
    
         yourSpinner.setAdapter(arrayAdapter); 
    

    这应该可行。

    【讨论】:

    • 谢谢@Vino。这有帮助。你能告诉我如何在同一个微调器中将“emptyvalue”设置为默认值吗?我以前在我发布的代码中有问题,但是即使我将第一个值设置为“”,它也不会设置默认值微调器中的值为空。
    【解决方案2】:

    动态数据到 Spinner

    首先将所有数据存储在 arraylist 或数组中,然后将其传递给 spinner 的适配器它的工作......

    您可以将数据写入数组

    array_name.add(your_data);
    

    【讨论】:

      【解决方案3】:

      创建一个侦听器,用于将动态数据绑定到您的微调器。 Example 1Example 2

      您可以使用 AsyncTask 从服务器检索数据(或使用合适的方法)。

      【讨论】:

        猜你喜欢
        • 2017-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-30
        • 1970-01-01
        • 1970-01-01
        • 2013-06-23
        • 1970-01-01
        相关资源
        最近更新 更多