其他分享
首页 > 其他分享> > android-从Spinner onItemSelected更改Spinner选项

android-从Spinner onItemSelected更改Spinner选项

作者:互联网

我在上面有一个微调器,显示圣经的书.选择一本书后,我会找出其中有几章,并将其放入Chapters变量中.然后,我想根据有多少章来更改名为bible_chapters的微调器.

public class Find extends Fragment {

    private Spinner bibleBooks;
    private Spinner bibleChapters;
    private Spinner bibleVerses;
    private boolean spinnerInitialized;

    public Find() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        final View rootView = inflater.inflate(R.layout.fragment_find, container, false);
        final DatabaseManager databaseAccess = DatabaseManager.getInstance(getActivity());
        final Context con = getContext();

        // Spinner Listener
        bibleBooks = rootView.findViewById(R.id.bible_books);
        bibleChapters = rootView.findViewById(R.id.bible_chapters);

        bibleBooks.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if (!spinnerInitialized) {
                    spinnerInitialized = true;
                    return;
                }

                databaseAccess.open();
                String book = adapterView.getSelectedItem().toString();
                int Chapters = databaseAccess.howManyChapters(book);
                databaseAccess.close();
                Log.d("Chapters",String.valueOf(Chapters));
                if(Chapters > 1) {

                    List<String> newChapters = new ArrayList<String>();
                    for (int x = 2; x <= Chapters; x++) {
                        newChapters.add("Chapter " + x);
                    }
                    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(con, bibleBooks, newChapters);
                    bibleBooks.setAdapter(arrayAdapter);
                }

            }
            public void onNothingSelected(AdapterView<?> adapterView) {
                return;
            }
        });


        // Inflate the layout for this fragment
        return rootView;
    }

}

错误在于ArrayAdapter< String>. arrayAdapter = new ArrayAdapter< String>(con,bibleBooks,newChapters);线.问题在于上下文.我试过了:

这个,

rootView,

getContext(),

getActivity(),

我不断得到的错误是:
错误:找不到适用于ArrayAdapter(Context,Spinner,List< String>)的合适的构造函数

他们似乎都不是它要找的东西.我要去哪里错了?

解决方法:

在Xamarin.Android中,我总是传递spinnerItem,例如Android.Resource.Layout.SimpleSpinnerItem

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(con, Android.Resource.Layout.SimpleSpinnerItem, newChapters);

而不是微调框本身的* bibleBooks *.

编辑:
对于Java:android.R.layout.simple_spinner_item

标签:android-studio-3-0,spinner,android
来源: https://codeday.me/bug/20191025/1925576.html