我在页面顶部放置了一个旋转器,用于显示圣经书籍。当选择一本书时,我会查询该书有多少章,并将其放入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);
这一行,问题出在上下文。我尝试过以下选项:this
、rootView
、getContext()
和getActivity()
,但是我一直收到以下错误信息:error: no suitable constructor found for ArrayAdapter(Context,Spinner,List<String>)
。没有一个是需要的选项,我错在哪里了?