我已经尝试解决这个简单的问题两天了,但没有成功。我知道之前有人问过类似的问题(比如这里: 如何获得 Spinner 的选定值?),但我还没有解决这个问题。
因此,我创建了一个简单的应用程序,其中包含一个微调器和一个自定义 ListView。微调器(包含值 1、2、3、4、5..)为 ListView 创建更多微调器(其中包含玩家名称)(例如,选择 4 会创建四个微调器)。目前,该程序运行良好,只是我不知道在单击操作栏中的按钮时如何获取 ListView 中微调器的选定值?我尝试使用 getItemAtPosition 和 getSelectedXXX() 方法,但我总是倾向于从我最近选择的单个微调器中获取值。
我不确定我是否必须调用 Spinner 的 setOnItemSelectedListener() 中的方法,或者我是否可以从 SpinnerAdapter 类外部调用这些值。
这是自定义 Spinner 类
public class PlayerAdapter extends ArrayAdapter<String> {
public PlayerAdapter(ArrayList<String> mPlayers) {
super(getActivity(), 0, mPlayers);
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(
R.layout.list_item_players, null);
}
Spinner mSpinnerPlayer = (Spinner) convertView
.findViewById(R.id.spinner_player);
ArrayAdapter<Player> spinnerAdapterPlayer = new ArrayAdapter<Player>(getActivity(), android.R.layout.simple_spinner_item, mPlayers);
spinnerAdapterPlayer.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinnerPlayer.setAdapter(spinnerAdapterPlayer);
mSpinnerPlayer.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// TODO Auto-generated method stub
//Should I use getItemAtPosition or getSelectedXXX methods here?
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
return convertView;
}
}
}
这是使布局膨胀的片段创建了第一个微调器(对格式和代码样式感到抱歉)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
setHasOptionsMenu(true);
//gets player names from a database, such as "Player 1", "Player 2" etc..
//this is container of the spinners displayed in the listview
mPlayers = PlayerDB
.get(getActivity())
.getPlayers();
}
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_menu, parent, false);
listView = (ListView) view.findViewById(R.id.listView1);
mSpinnerNumOfPlayers = (Spinner) view.findViewById(R.id.spinner_numOfPlayers);
ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter
.createFromResource(this.getActivity(),
R.array.numberOfPlayers_array,
android.R.layout.simple_spinner_item);
spinnerAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinnerNumOfPlayers.setAdapter(spinnerAdapter);
mSpinnerNumOfPlayers.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
String position = parent.getItemAtPosition(pos).toString();
mArray = new ArrayList<String>();
posInt = Integer.parseInt(position);
for (int i = 0; i < posInt; i++) {
mArray.add("" + i);
}
PlayerAdapter adapter = new PlayerAdapter(mArray);
listView.setAdapter(adapter);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
listView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
}
}
);
return view;
}
// ActionBar
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.main, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_start:
//when "start" button is clicked the spinner values are saved into an ArrayList