i want to do a reverse operation like i get the position of the selected items from the two spinners then going to the json url and get that selected data from "fr" array and "ar" array to upload it to realtime database firebase.
this is my code:
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
response -> {
try {
// Parse the JSON response
//get the current app Language
currentLang = getResources().getConfiguration().locale.getLanguage();
jsonArray = new JSONArray(response);
JSONObject jsonObject = jsonArray.getJSONObject(0);
if (currentLang.equals("fr") || currentLang.equals("en")) {
firstArray = jsonObject.getJSONArray("fr");
for (int i = 0; i < firstArray.length(); i++) {
obj = firstArray.getJSONObject(i);
wilayaName = obj.getString("name");
Log.d("JSON", "Name:" + wilayaName);
wilayaNames.add(wilayaName);
}
} else if (currentLang.equals("ar")) {
firstArray = jsonObject.getJSONArray("ar");
for (int i = 0; i < firstArray.length(); i++) {
obj = firstArray.getJSONObject(i);
wilayaName = obj.getString("name");
Log.d("JSON", "Name:" + wilayaName);
wilayaNames.add(wilayaName);
}
}
} catch (JSONException e) {
e.printStackTrace();
Log.e("Error", e.getMessage());
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, wilayaNames);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Finally, set the adapter to the spinner
wilayaSpinner.setAdapter(adapter);
}, error -> {
// Handle error
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
wilayaSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Handle the user's selection here
selectedState = wilayaSpinner.getSelectedItem().toString();
JsonArrayRequest request;
request = new JsonArrayRequest(Request.Method.GET, url, null, response -> {
try {
// Parse the JSON data and extract the required information
if (communeAdapter != null) {
communeAdapter.clear();
}
if (currentLang.equals("fr") || currentLang.equals("en")) {
JSONArray jsonArray = response.getJSONObject(0).getJSONArray("fr");
JSONObject jsonObject = null;
for (int i = 0; i < jsonArray.length(); i++) {
jsonObject = jsonArray.getJSONObject(i);
if (jsonObject.getString("name").equals(selectedState)) {
break;
}
}
JSONArray communesArray = jsonObject.getJSONArray("communes");
for (int j = 0; j < communesArray.length(); j++) {
String commune = communesArray.getString(j);
communesList.add(commune);
Log.d("Commune List", commune);
}
} else if (currentLang.equals("ar")) {
JSONArray jsonArray = response.getJSONObject(0).getJSONArray("ar");
JSONObject jsonObject = null;
for (int i = 0; i < jsonArray.length(); i++) {
jsonObject = jsonArray.getJSONObject(i);
if (jsonObject.getString("name").equals(selectedState)) {
break;
}
}
JSONArray communesArray = jsonObject.getJSONArray("communes");
for (int j = 0; j < communesArray.length(); j++) {
String commune = communesArray.getString(j);
communesList.add(commune);
Log.d("Commune List", commune);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
communeAdapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, communesList);
communeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
communeSpinner.setAdapter(communeAdapter);
communeAdapter.notifyDataSetChanged();
}, Throwable::printStackTrace);
queue.add(request);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing
}
});
this is my Database Structure:
{
"location": {
"location_enfr": {
"city": "Akabli",
"province": "ADRAR"
},
"location_ar": {
"city": "أكابلي",
"province": "أدرار"
}
}
}
and this this is my json:
[
{
"fr": [
{
"id": 1,
"name": "ADRAR",
"communes": [ "Adrar","Akabli","Aougrout","Aoulef","Bordj badji mokhtar","Bouda"]
},
{
"id": 2,
"name": "CHLEF",
"communes": ["Abou el hassan","Ain merane","Benairia","Beni bouateb","Beni haoua","Beni rached"]
},
{
"id": 3,
"name": "LAGHOUAT",
"communes": ["Aflou","Ain madhi","Ait sidi ali","Beidha","Brida","El assafia","El ghicha","El houaita"]
}
],
"ar": [
{
"id": 1,
"name": "أدرار",
"communes": ["أدرار","أكابلي","أوقروت","أولف","برج بادجي مختار","بودة" ]
},
{
"id": 2,
"name": "شلف",
"communes": ["أبو الحسن","عين مران","بنايرية","بني بوعتاب","بني حواء","بني راشد"]
},
{
"id": 3,
"name": "الأغواط",
"communes": ["عفلو","عين ماضي","عيت سيدي علي","البيضاء","بريدة","الأسافية","الغيشة","الحويطة"]
}
]
}
]
someone can help me please this is my first project.