Я хочу спросить, как установить выбор для нескольких взаимосвязанных (например, страна-государство-город) счетчиков в Android Studio, поскольку я создаю функцию обновления для системы. Как обычно для функции обновления, мне нужно сначала получить существующие данные из базы данных и поместить их в качестве выбранной по умолчанию опции в соответствующих счетчиках.
Например, данные в моей базе данных Страна C -> Штат B -> Город D . Но то, что я получил из моего отображенного вывода, было Страна C -> Штат A -> Город A . Я полагаю, чтобы получить Штат B -> Город D (что правильно) вместо Штат A -> Город A (что неправильно) .
Первая часть кода - это входные счетчики и адаптеры:
spVehicleType_B.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
arrayAdapter_vehicleBrand_B = new ArrayAdapter<>(getApplicationContext(), R.layout.spinner_item, al_car_B);
spVehicleBrand_B.setAdapter(arrayAdapter_vehicleBrand_B);
spVehicleBrand_B.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
arrayAdapter_vehicleModel_B = new ArrayAdapter<>(getApplicationContext(), R.layout.spinner_item, al_proton_B);
spVehicleModel_B.setAdapter(arrayAdapter_vehicleModel_B);
spVehicleModel_B.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(position== 0|position== 1|position== 2|position== 3){
arrayAdapter_MaxSeat_B = new ArrayAdapter<>(getApplicationContext(), R.layout.spinner_item, al_carSeat_4_B);
spMaxSeat_B.setAdapter(arrayAdapter_MaxSeat_B);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
if (position == 1) {
arrayAdapter_vehicleModel_B = new ArrayAdapter<>(getApplicationContext(), R.layout.spinner_item, al_perodua_B);
spVehicleModel_B.setAdapter(arrayAdapter_vehicleModel_B);
spVehicleModel_B.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
arrayAdapter_MaxSeat_B = new ArrayAdapter<>(getApplicationContext(), R.layout.spinner_item, al_carSeat_4_B);
spMaxSeat_B.setAdapter(arrayAdapter_MaxSeat_B);
}if (position == 1) {
arrayAdapter_MaxSeat_B = new ArrayAdapter<>(getApplicationContext(), R.layout.spinner_item, al_carSeat_4_B);
spMaxSeat_B.setAdapter(arrayAdapter_MaxSeat_B);
}if (position == 2) {
arrayAdapter_MaxSeat_B = new ArrayAdapter<>(getApplicationContext(), R.layout.spinner_item, al_carSeat_6_B);
spMaxSeat_B.setAdapter(arrayAdapter_MaxSeat_B);
}if (position == 3){
arrayAdapter_MaxSeat_B = new ArrayAdapter<>(getApplicationContext(), R.layout.spinner_item, al_carSeat_4_B);
spMaxSeat_B.setAdapter(arrayAdapter_MaxSeat_B);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
if (position == 2) {
arrayAdapter_vehicleModel_B = new ArrayAdapter<>(getApplicationContext(), R.layout.spinner_item, al_nissan_B);
spVehicleModel_B.setAdapter(arrayAdapter_vehicleModel_B);
spVehicleModel_B.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0|position == 1|position == 2){
arrayAdapter_MaxSeat_B = new ArrayAdapter<>(getApplicationContext(), R.layout.spinner_item, al_carSeat_4_B);
spMaxSeat_B.setAdapter(arrayAdapter_MaxSeat_B);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
if (position == 1) {
arrayAdapter_vehicleBrand_B = new ArrayAdapter<>(getApplicationContext(), R.layout.spinner_item, al_motorcycle_B);
spVehicleBrand_B.setAdapter(arrayAdapter_vehicleBrand_B);
spVehicleBrand_B.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
arrayAdapter_vehicleModel_B = new ArrayAdapter<>(getApplicationContext(), R.layout.spinner_item, al_modenas_B);
spVehicleModel_B.setAdapter(arrayAdapter_vehicleModel_B);
spVehicleModel_B.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0|position == 1|position == 2) {
arrayAdapter_MaxSeat_B = new ArrayAdapter<>(getApplicationContext(), R.layout.spinner_item, al_motorSeat_1_B);
spMaxSeat_B.setAdapter(arrayAdapter_MaxSeat_B);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
if (position == 1) {
arrayAdapter_vehicleModel_B = new ArrayAdapter<>(getApplicationContext(), R.layout.spinner_item, al_honda_B);
spVehicleModel_B.setAdapter(arrayAdapter_vehicleModel_B);
spVehicleModel_B.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0|position == 1) {
arrayAdapter_MaxSeat_B = new ArrayAdapter<>(getApplicationContext(), R.layout.spinner_item, al_motorSeat_1_B);
spMaxSeat_B.setAdapter(arrayAdapter_MaxSeat_B);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
Вторая часть кода используется для извлечения данных из базы данных Firestore:
db.collection("verified_user").document(userId).collection("vehicle").document(vehicleid).get()
.addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
if(documentSnapshot.exists()){
String plateNo = documentSnapshot.getString("plate_no");
carPlateNo_B.setText(plateNo);
String vehicleType = documentSnapshot.getString("vehicle_type");
int vehicleTypePosition = arrayAdapter_vehicleType_B.getPosition(vehicleType);
spVehicleType_B.setSelection(vehicleTypePosition, true);
String vehicleBrand = documentSnapshot.getString("vehicle_brand");
int vehicleBrandPosition = arrayAdapter_vehicleBrand_B.getPosition(vehicleBrand);
spVehicleBrand_B.setSelection(vehicleBrandPosition, true);
String vehicleModel = documentSnapshot.getString("vehicle_model");
int vehicleModelPosition = arrayAdapter_vehicleModel_B.getPosition(vehicleModel);
spVehicleModel_B.setSelection(vehicleModelPosition, true);
String vehicleColour = documentSnapshot.getString("vehicle_colour");
int vehicleColourPosition = arrayAdapter_vehicleColour_B.getPosition(vehicleColour);
spVehicleColour_B.setSelection(vehicleColourPosition, true);
String vehicleMaxSeat = documentSnapshot.getString("max_seat");
int vehicleMaxSeatPosition = arrayAdapter_MaxSeat_B.getPosition(vehicleMaxSeat);
spMaxSeat_B.setSelection(vehicleMaxSeatPosition, true);
Log.d("String",vehicleType+", "+vehicleBrand+", "+vehicleModel+", "+vehicleColour+", "+vehicleMaxSeat);
Log.d("Position",""+vehicleTypePosition+", "+vehicleBrandPosition+", "+vehicleModelPosition+", "+vehicleColourPosition+", "+vehicleMaxSeatPosition);
}
}
});
Я напечатал Сообщение журнала и вывод показывает, что какое-то поле возвращает -1 позицию , что приводит к неправильному результату. Обратите внимание, что строки, извлеченные из базы данных, являются правильными, но возвращаемые позиции являются неправильными.
D/String: Motorcycle, Honda, Vario 150, Black, 1
D/Position: 1, -1, -1, 3, 3