从微调器 onItemSelected 更改微调器选项
Change spinner options from spinner onItemSelected
我在顶部有一个旋转器,显示圣经书籍。选择一本书后,我会找出其中有多少章并将其放入 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>)
None 似乎正是它要找的东西。我哪里错了?
请尝试在 ArrayAdapter
的 Constructor
中使用 getActivity()
而不是 getContext()
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), bibleBooks, newChapters);
bibleBooks.setAdapter(arrayAdapter);
希望对您有所帮助
在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
在代码new ArrayAdapter<String>(con, bibleBooks, newChapters),
中,第二个参数必须是资源ID。例如,new ArrayAdapter<String>(con, R.layout.item_spinner, newChapters);
和R.layout.item_spinner
:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:drawableEnd="@drawable/ic_spinner"
android:drawablePadding="4dp"
android:drawableRight="@drawable/ic_spinner"
android:gravity="right"
android:padding="8dp"
android:textColor="@color/colorPrimary"
android:textSize="14dp" />
你的尝试是错误的。
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(con, bibleBooks, newChapters);
您正在传递 spinner
对象而不是 layout resource id
。
尝试下面它适合你。
ArrayAdapter<String> adapter=new ArrayAdapter<String>(context,R.layout.support_simple_spinner_dropdown_item,newChapters);
我在顶部有一个旋转器,显示圣经书籍。选择一本书后,我会找出其中有多少章并将其放入 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>)
None 似乎正是它要找的东西。我哪里错了?
请尝试在 ArrayAdapter
Constructor
中使用 getActivity()
而不是 getContext()
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), bibleBooks, newChapters);
bibleBooks.setAdapter(arrayAdapter);
希望对您有所帮助
在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
在代码new ArrayAdapter<String>(con, bibleBooks, newChapters),
中,第二个参数必须是资源ID。例如,new ArrayAdapter<String>(con, R.layout.item_spinner, newChapters);
和R.layout.item_spinner
:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:drawableEnd="@drawable/ic_spinner"
android:drawablePadding="4dp"
android:drawableRight="@drawable/ic_spinner"
android:gravity="right"
android:padding="8dp"
android:textColor="@color/colorPrimary"
android:textSize="14dp" />
你的尝试是错误的。
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(con, bibleBooks, newChapters);
您正在传递 spinner
对象而不是 layout resource id
。
尝试下面它适合你。
ArrayAdapter<String> adapter=new ArrayAdapter<String>(context,R.layout.support_simple_spinner_dropdown_item,newChapters);