旋转屏幕后,Android微调器变为空

时间:2014-01-09 09:34:56

标签: android null android-spinner

我正在使用微调器将值显示为下拉列表,我正在使用下面的代码更改微调器文本值

<Spinner
        android:id="@+id/showUnit"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:entries="@array/unitName"
        android:background="@drawable/gradient_spinner_map_miles_button" />

    showUnit.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                String item = arg0.getItemAtPosition(arg2).toString();
                if (arg1 != null && arg1 instanceof TextView) {
                     ((TextView)arg1).setTextColor(Color.WHITE);
                    ((TextView) arg1).setTextSize(13);
                    ((TextView) arg1).setGravity(Gravity.CENTER);
                 }

}

showUnit = (Spinner) findViewById(R.id.showUnit);

但是当我尝试旋转屏幕时,((TextView)arg0.getChildAt(0))会返回null。

我知道当我在landsacpe或portrait模式下旋转屏幕时,活动循环重新开始,那么为什么微调器变为空。

请给我适当的解决方案。

由于

2 个答案:

答案 0 :(得分:2)

为什么在getChildAt中有适当的观点时使用View arg1

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
{           
     if (arg1 != null && arg1 instanceof TextView) {
         ((TextView)arg1).setTextColor(Color.WHITE);
     }
}

要更改微调器中所选文本的颜色,最佳做法是使用选择器。

spinner_state.xml (在可绘制文件夹中)

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_enabled="false"
        android:drawable="@color/black" />
    <item
        android:state_pressed="true"
        android:state_enabled="true"
        android:drawable="@color/red" />
    <item
        android:state_focused="true"
        android:state_enabled="true"
        android:drawable="@color/red" />
    <item
        android:state_enabled="true"
        android:drawable="@color/gray" />
</selector>
在您的微调器中,应用选择器:

    android:dropDownSelector="@drawable/spinner_state"

Spinner来自:Spinner does not apply dropDownSelector attribute

这并不能解释为什么onItemSelected中的值为空,但如果没有完整的代码,我就看不出有什么问题。

答案 1 :(得分:0)

我遇到了同样的问题,所以我在getView上移动了setTextColor:

<section>
    <div class="section-header">this is the NEW section header for mobile</div>
    <h1>this is the header</h1>
</section>

现在:

ArrayAdapter<CharSequence> adapter =
    new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, getResources().getTextArray(R.array.myarray)){
        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent){
            View v = null;
            System.out.println("getDropDownView");
            v = super.getDropDownView(position, null, parent);
            // If this is the selected item position
            if (position == PosItemSelected) {
                v.setBackgroundColor(getResources().getColor(R.color.orange_energicod));
            }
            else {
                // for other views
                v.setBackgroundColor(Color.WHITE);
            }
            return v;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);                     
            ((TextView) v).setTextColor(getResources().getColor(R.color.white));
            return v;
        }
    };

// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter); // set the adapter to provide layout of rows and content
spinner.setOnItemSelectedListener(this); // set the listener, to perform actions based on item selection

希望它也适合你。