My target is to show part of selected spinner item string, as selected item. In Spinner options I want show full string, but when selected only first part of it. Example:
Selected: "Racing Car" > show: "Racing",
Selected: "Fast car" > show: "Fast"
Does that possible? My code:
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) {
}
});
}
Try this,
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) {
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.