按钮内的微调器项目选择onclick无效

时间:2016-10-14 10:41:43

标签: android spinner

在我的适配器类内部,因为我将在按钮单击内部获取数据我想在按钮单击内填充微调器并在其中设置项目选择的侦听器。

tvMediaCategory.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub

                    for(int i=0; i<mediaList.get(position).getMediaCatList().size(); i++)
                    {
                        catArr[i] = mediaList.get(position).getMediaCatList().get(i).getCategoryName();
                    }
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                for(int i=0; i<catArr.length; i++)
                {adapter.add(catArr[i]);}
                adapter.add("HINT_TEXT_HERE"); //This is the text that will be displayed as hint.

                spinner.setAdapter(adapter);
                spinner.setSelection(adapter.getCount()); //set the hint the default selection so it appears on launch.
                spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view,
                                               int position, long id) {
                        Log.v("item", (String) parent.getItemAtPosition(position));
                        Toast.makeText(context, "position clicked "+position, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {
                        // TODO Auto-generated method stub
                    }
                });
                tvMediaCategory.setVisibility(View.GONE);
                spinner.setVisibility(View.VISIBLE);
                spinner.performClick();

                //showCategoryDlg(catArr, position, selectedPos, tvMediaCategory);

            }               
        }
    });

这里我的主持人没有打开但是选择了默认项目。如何使它工作。这个我的代码在适配器里面。如何在点击按钮时显示我的微调器项目?

1 个答案:

答案 0 :(得分:0)

试试这个例子......我相信你能得到你想要的东西...... 你需要实现onItemSelectedListener

https://www.mkyong.com/android/android-spinner-drop-down-list-example