簡而言之,我創建了一個微調框。我的array.xml
中存在微調項和值條目。TextView不會隨OnItemSelectedListener更改
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="select">
<item>a</item>
<item>b</item>
<item>c</item>
</string-array>
<integer-array name="selectValues">
<item>1</item>
<item>2</item>
<item>3</item>
</integer-array>
</resources>
另外這裏是我的linearlayout
。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/selectLayout"
>
<TextView
android:id="@+id/tvSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/select"
android:textSize="18sp"
android:layout_gravity="center"
/>
<Spinner
android:id="@+id/sSelect"
android:layout_width="179dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:entries="@array/select"
android:prompt="@string/sec"
android:spinnerMode="dialog"
/>
</LinearLayout>
在我的活動我創建OnItemSelectedListener
和當用戶選擇一個項目我想改變TextView
。我認爲使用開關時會出現問題。
以下是活動。
public class Select extends Activity implements OnItemSelectedListener{
int[] itemValues;
TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Resources rsc = getResources();
itemValues = rsc.getIntArray(R.array.selectValues);
t = (TextView) findViewById(R.id.tvSelect);
setContentView(R.layout.select);
Spinner form = (Spinner) findViewById(R.id.sSelect);
form.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> item, View which, int sort,
long arg3) {
// TODO Auto-generated method stub
switch(which.getId())
{
case R.id.sSelect:
int value = itemValues[sort];
t.setText(value);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
如何解決這個問題?
編輯:當我嘗試刪除開關,我得到unfortunately has stopped
錯誤。
您是否檢查過setText方法是否被調用?也許一個簡單的if語句在這裏比較容易(同樣,如果你只是爲一個spinner設置監聽器,就不需要檢查視圖的id) –
我正在編程學習。我也檢查了。此外,當我刪除開關,我不幸錯誤。 –
R.id.sSelect是什麼? –