简体   繁体   中英

Unable to select item from Spinner

I am trying to populate my spinner using a List it works, but when I try to select an item I am unable to do so. I've created a list from a firebase database and I need to use it in a spinner.

This is my code to get the list :

 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) {

                                          }
                                      });

and here is my code to populate the spinner

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) {

        }
    });

The spinner populates but onItemSelected doesn't work. What should I do? Thank you.

When you use custom layout for spinner item, for example 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>

you should use this constructor of ArrayAdapter

ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)

which takes textViewResourceId as an argument to determine where to place your text in your custom spinner item, and textViewResourceId will be R.id.txtSomeId .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM