我在我的应用程序中使用了Spinners。他们工作正常,但有一个例外。我已为每个提示设置了提示,但它们没有显示。我在ArrayAdapters
期间将onCreate
设置为Spinners,我的猜测是setAdapter
方法自动将选择设置为位置0.有没有办法设置提示并拥有它按预期工作?
这是一段代码:
从布局文件:
<Spinner android:id="@+id/selPunter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/select_quarterback_prompt"
android:layout_marginLeft="20sp"
android:layout_marginRight="20sp" />
来自活动:
offenseList = new ArrayAdapter<PlayerVO>(this,
R.layout.select_item_closed,
gdm.getPlayersByTeamId(offenseId));
offenseList.setDropDownViewResource(R.layout.select_item);
selKicker.setAdapter(offenseList);
答案 0 :(得分:1)
即使您将OnItemSelectedListener放在活动的onStart()方法中,这似乎也会发生。
我为此问题所做的工作是我在我的资源数组的位置0放置了一条默认消息(&#34;选择行程类型&#34;)。因此,当调用OnItemSelectedListener时,如果选择了位置0,则不执行任何操作。这是我的代码:
mTripTypeSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent,View v,int position,long rowId) {
//boolean used for hiding spinner
boolean hideSpinner = true;
switch(position){
case 0:
//nothing was selected - defualt "Select Trip Type"
hideSpinner = false;
break;
case 1:
mCurrentStop.setStopType(Stop.STOP_TYPE.DELIVERY);
break;
case 2:
mCurrentStop.setStopType(Stop.STOP_TYPE.START_OF_BREAK);
break;
case 3:
mCurrentStop.setStopType(Stop.STOP_TYPE.END_OF_BREAK);
break;
case 4:
mCurrentStop.setStopType(Stop.STOP_TYPE.START_OF_LUNCH);
break;
case 5:
mCurrentStop.setStopType(Stop.STOP_TYPE.END_OF_LUNCH);
break;
case 6:
mCurrentStop.setStopType(Stop.STOP_TYPE.END_OF_TRIP);
break;
}
//display other data screens
displayData(hideSpinner);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
mCurrentStop.setStopType(Stop.STOP_TYPE.DELIVERY);
}
});