无法从Spinner中选择项目

时间:2017-05-20 20:51:46

标签: android list android-spinner onitemselectedlistener

我正在尝试使用List工作来填充我的微调器,但是当我尝试选择一个项目时,我无法这样做。 我已经从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不起作用。 我该怎么办?谢谢。

1 个答案:

答案 0 :(得分:1)

对微调器项使用自定义布局时,例如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

相关问题