Spinner does not show selected value

后端 未结 25 1345
-上瘾入骨i
-上瘾入骨i 2020-12-03 13:16

I have implemented the spinner by populating the array list through database.I can get and show the array list in my spinner array adapter but if I select the item in spinne

相关标签:
25条回答
  • 2020-12-03 13:57

    You have to do some changes as:

    Spinner spinner1 = (Spinner) findViewById(R.id.prospin);
     ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, providerlist);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    

    instead of "android.R.layout.simple_spinner_dropdown_item"

    adapter1.setDropDownViewResource(android.R.layout.simple_list_item_activated_1);
    

    Use "android.R.layout.simple_list_item_activated_1"

    spinner1.setAdapter(adapter1);
    

    It will highlight the selected value accordingly. Selected color is chosen by your apps default theme.

    For more information follow below link: https://developer.android.com/reference/android/R.layout.html#simple_list_item_activated_1

    0 讨论(0)
  • 2020-12-03 13:59

    Use wrap_content for the height of Spinner.

    Probably it does not have enough height to show text.

    0 讨论(0)
  • 2020-12-03 13:59

    Try this.

     final ArrayList<String> providerlist= new ArrayList<String>();
        Spinner spinner1 = (Spinner) findViewById(R.id.prospin);
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, providerlist);
    
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(adapter1);
        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    
                                // On selecting a spinner item
                String item = providerlist.get(position);
    
                // Showing selected spinner item
                Toast.makeText(this,
                        "Selected Country : " + item, Toast.LENGTH_LONG).show();
            }
    
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
    
            }
        });
    
    0 讨论(0)
  • 2020-12-03 13:59

    I had the same problem, and none of the solutions were working for me. Finally found the problem was that the spinner was being populated from firestore db via an async task. Rearranged the code so that the adapter is set after the async task completion, and voila!!

    0 讨论(0)
  • 2020-12-03 14:02

    This answer may be a little stupid but if you do the same mistake then try... set values to your ArrayList first and then assign that arrayList to spinner. I declared a global arrayList and set it to spinner first and then add values to it from another method... at that time i faced the same problem. Otherwise you can do notifyDataSetChanged() to your arrayList.

    0 讨论(0)
  • 2020-12-03 14:03

    I just had this problem and after trying all of the solutions listed found out that the issue was that I had set the spinner layout_width to 60dp.

    Changed it to fill_parent and problem solved.

    0 讨论(0)
提交回复
热议问题