gpt4 book ai didi

java - Android Spinner 选定项目操作

转载 作者:行者123 更新时间:2023-12-01 09:30:55 26 4
gpt4 key购买 nike

我的目标是将选定的微调项目字符串的一部分显示为选定的项目。在微调器选项中,我想显示完整的字符串,但选择时仅显示其第一部分。示例:

选择:“赛车”> 显示:“赛车”,

选择:“快车” > 显示:“快”

这可能吗?我的代码:

 private void defineRacingCarsArray(){
racingCarsArray = Arrays.asList(
"Racing Car",
"Fast Car"
);
}



private void defineRacingCarsCodeSpinner(){
racingCarsCodeSpinner = (Spinner) view.findViewById(R.id.cars_code_spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
for (int i = 0; i < racingCarsArray.size(); ++i) {
adapter.add(racingCarsArray.get(i));
}
racingCarsCodeSpinner.setAdapter(adapter);
racingCarsCodeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
parent.getItemAtPosition(position);
String car = racingCarsArray.get(position);
;
}

public void onNothingSelected(AdapterView<?> parent) {

}
});
}

最佳答案

试试这个,

racingCarsCodeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// here you have to update
TextView selectedView = (TextView) view;
selectedView.setText(racingCarsArray.get(position).split(" ",2)[0]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

关于java - Android Spinner 选定项目操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39409018/

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