数据未在spinner android中列为下拉列表?

时间:2016-10-10 04:59:32

标签: android android-spinner

仅列出数组的第一个元素。每当我点击微调器时,它都不会显示微调器的其他元素。

代码: -

//对于微调器

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
            android.R.layout.simple_spinner_item, medincineName){

        public View getView(int position, View convertView, ViewGroup parent) {

            View v = super.getView(position, convertView, parent);

            ((TextView) v).setTextSize(16);

            return v;

        }

        public View getDropDownView(int position, View convertView,ViewGroup parent) {

            View v = super.getDropDownView(position, convertView,parent);

            ((TextView) v).setGravity(Gravity.CENTER);
            //((TextView) v).setPadding(10,10,10,10);

            return v;

        }

    };


    medicineList.setAdapter(adapter);

    //Select Data from spinner
    medicineList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            selectedMedicineName= adapterView.getItemAtPosition(i).toString();

            Toast.makeText(getApplicationContext(), "Selected Values is: " + selectedMedicineName, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

            Log.i("Nothing hasbeenSelected","kdnsadkla");
        }
    });

仅显示第一个元素。当我尝试单击微调器时,不会出现下拉列表。

1 个答案:

答案 0 :(得分:0)

像这样

创建 my_spinner_view.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:textColor="#ffffff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />

然后调用此 my_spinner_view.xml 以您希望的方式显示微调器视图。

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
            R.layout.my_spinner_view, medincineName){

        public View getView(int position, View convertView, ViewGroup parent) {

            View v = super.getView(position, convertView, parent);

            ((TextView) v).setTextSize(16);

            return v;

        }

        public View getDropDownView(int position, View convertView,ViewGroup parent) {

            View v = super.getDropDownView(position, convertView,parent);

            ((TextView) v).setGravity(Gravity.CENTER);
            //((TextView) v).setPadding(10,10,10,10);

            return v;

        }

    };


    medicineList.setAdapter(adapter);

    //Select Data from spinner
    medicineList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            selectedMedicineName= adapterView.getItemAtPosition(i).toString();

            Toast.makeText(getApplicationContext(), "Selected Values is: " + selectedMedicineName, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

            Log.i("Nothing hasbeenSelected","kdnsadkla");
        }
    }); 

你可以这么简单地做.. 创建 my_spinner_view2.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:gravity="left"  
    android:textColor="#FF0000"         
    android:padding="5dip"
    />

并设置适配器如bellow snippet

ArrayAdapter<String> adapter = new ArrayAdapter<String>(YourActivity.this,
                R.layout.my_spinner_view2, medincineName);
 medicineList.setAdapter(adapter);