gpt4 book ai didi

java - 如何获取ListView中显示的Spinners的值?

转载 作者:行者123 更新时间:2023-12-01 14:01:52 25 4
gpt4 key购买 nike

我已经尝试解决这个简单的问题两天了,但没有成功。我知道以前也有人问过类似的问题(如下所示: How do you get the selected value of a 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;
}
}
}

这是扩展布局的 fragment ,创建第一个微调器(对格式和代码样式感到抱歉)

@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

最佳答案

此方法将从微调器中返回所选项目。

spinner.getSelectedItem().toString()

关于java - 如何获取ListView中显示的Spinners的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19295169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com