无法 select 来自 Spinner 的项目
Unable to select item from Spinner
我正在尝试使用 List 来填充我的微调器,但当我尝试 select 一个项目时,我无法这样做。
我已经从 firebase 数据库创建了一个列表,我需要在微调器中使用它。
这是我获取列表的代码:
List<String> Uniname = new ArrayList<>();
childrefUNI.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
pl.setVisibility(View.VISIBLE);
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
String uniName = postSnapshot.getValue(String.class);
Uniname.add(uniName);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
这是我用来填充微调器的代码
ArrayAdapter < String > UniversityNameArrayAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_item,Uniname);
UniversityNameSpinner.setAdapter(UniversityNameArrayAdapter);
UniversityNameSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
UniversityName = UniversityNameSpinner.getSelectedItem().toString();
Toast.makeText(getApplicationContext(),UniversityName,Toast.LENGTH_SHORT).show();
if(!UniversityName.equals("Please Select University")){
CourseNameSpinner.setVisibility(View.VISIBLE);
if (UniversityName.equals("MDU")){
CourseNameArray = getResources().getStringArray(R.array.MDUCourses);
CourseName = CourseNameGen(CourseNameArray);
}
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
微调器填充但onItemSelected
不起作用。
我应该怎么办?谢谢。
当您对微调项使用自定义布局时,例如 spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:id="@+id/lnrRoot"
android:background="#263238">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="some text"
android:id="@+id/txtSomeId"
android:gravity="center_vertical"/>
</LinearLayout>
你应该使用 ArrayAdapter
的这个构造函数
ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)
将 textViewResourceId
作为参数来确定将文本放置在自定义微调项中的位置,textViewResourceId
将是 R.id.txtSomeId
。
我正在尝试使用 List 来填充我的微调器,但当我尝试 select 一个项目时,我无法这样做。 我已经从 firebase 数据库创建了一个列表,我需要在微调器中使用它。
这是我获取列表的代码:
List<String> Uniname = new ArrayList<>();
childrefUNI.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
pl.setVisibility(View.VISIBLE);
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
String uniName = postSnapshot.getValue(String.class);
Uniname.add(uniName);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
这是我用来填充微调器的代码
ArrayAdapter < String > UniversityNameArrayAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_item,Uniname);
UniversityNameSpinner.setAdapter(UniversityNameArrayAdapter);
UniversityNameSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
UniversityName = UniversityNameSpinner.getSelectedItem().toString();
Toast.makeText(getApplicationContext(),UniversityName,Toast.LENGTH_SHORT).show();
if(!UniversityName.equals("Please Select University")){
CourseNameSpinner.setVisibility(View.VISIBLE);
if (UniversityName.equals("MDU")){
CourseNameArray = getResources().getStringArray(R.array.MDUCourses);
CourseName = CourseNameGen(CourseNameArray);
}
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
微调器填充但onItemSelected
不起作用。
我应该怎么办?谢谢。
当您对微调项使用自定义布局时,例如 spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:id="@+id/lnrRoot"
android:background="#263238">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="some text"
android:id="@+id/txtSomeId"
android:gravity="center_vertical"/>
</LinearLayout>
你应该使用 ArrayAdapter
ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)
将 textViewResourceId
作为参数来确定将文本放置在自定义微调项中的位置,textViewResourceId
将是 R.id.txtSomeId
。