Android中3个Spinner异常:车辆信息Spinner显示重复内容求助
carro) Display Issue It sounds like your carro Spinner is accidentally getting assigned the same adapter as your technician Spinners—let’s walk through how to fix this quickly:
Step 1: Create Separate Adapters for Different Data
The core problem is likely reusing the same adapter instance for all three Spinners, or assigning the technician adapter to carro by mistake. Here’s how to set up distinct adapters correctly:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_alterar_equipe); // Initialize all Spinners first tecnico_altera1 = findViewById(R.id.tecnico_altera1); tecnico_altera2 = findViewById(R.id.tecnico_altera2); carro = findViewById(R.id.carro); // Adapter for technician Spinners (they can share this since they show identical data) ArrayAdapter<CharSequence> tecnicoAdapter = ArrayAdapter.createFromResource( this, R.array.tecnicos_list, // Your technician string array resource android.R.layout.simple_spinner_item ); tecnicoAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Assign to technician Spinners tecnico_altera1.setAdapter(tecnicoAdapter); tecnico_altera2.setAdapter(tecnicoAdapter); // **Separate adapter for vehicle data** ArrayAdapter<CharSequence> carroAdapter = ArrayAdapter.createFromResource( this, R.array.carros_list, // Your vehicle string array resource (different from technicians!) android.R.layout.simple_spinner_item ); carroAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Assign to the vehicle Spinner carro.setAdapter(carroAdapter); }
Step 2: Verify Your String Resources
Make sure your res/values/strings.xml has two distinct string arrays—one for technicians, one for vehicles:
<string-array name="tecnicos_list"> <item>Maria Silva</item> <item>João Souza</item> <item>Carlos Mendes</item> </string-array> <string-array name="carros_list"> <item>Ford Focus</item> <item>Toyota Corolla</item> <item>Volkswagen Golf</item> </string-array>
Step 3: Double-Check for Accidental Reuse
Scan your code to ensure you didn’t copy-paste the technician adapter assignment and forget to update it for carro. A common mistake is something like:
// Oops! Wrong adapter assigned to carro carro.setAdapter(tecnicoAdapter); // This is what causes the duplicate display
If You’re Using Dynamic Data
If you’re fetching data from a database or API instead of string resources, make sure you’re populating the carroAdapter with vehicle-specific data, not the technician list. For example:
// Fetch vehicle data (example) List<String> vehicleList = getVehicleDataFromDatabase(); ArrayAdapter<String> carroAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, vehicleList); carro.setAdapter(carroAdapter);
By separating the adapters and ensuring each Spinner gets the correct one, your carro Spinner should now display unique vehicle information instead of matching the technician Spinners.
内容的提问来源于stack exchange,提问作者Sergio Guerjik

