我有提供一些地址注册表格的活动。 区域微调器如下:
我已经有办法从JSON获取数据,这是我的目标: “根据“省微调器”上的选定项目填充“城市微调器”,并与“区域微调器”和“乡村微调器”中的其余选项相同。
由于隐私问题,我无法在此处发布完整的Java类代码。
我正在使用翻新2从JSON检索数据,这是获取省份数据的方法:
private void initSpinnerProvinsi() {
apiInterface.getProvinsi()
.enqueue(new Callback<GetProvinsiResponse>() {
@Override
public void onResponse(Call<GetProvinsiResponse> call, Response<GetProvinsiResponse> response) {
if (response.isSuccessful()){
List<GetProvinsi> provinsiList = response.body().getSemuaprovinsi();
List<String> listSpinnerProvinsi = new ArrayList<>();
for (int i = 0; i < provinsiList.size(); i++){
listSpinnerProvinsi.add(provinsiList.get(i).getNama());
String idkota = provinsiList.get(i).getId();
Log.d("idkota ", idkota);
}
//set result into spinner adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext,
R.layout.support_simple_spinner_dropdown_item, listSpinnerProvinsi);
adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
spinner_provinsi.setAdapter(adapter);
}
else {
Log.d("onResponse: ", "gagal memuat data provinsi");
}
}
@Override
public void onFailure(Call<GetProvinsiResponse> call, Throwable t) {
Toast.makeText(mContext, "Koneksi gagal, silahkan cek koneksi internet anda",
Toast.LENGTH_SHORT).show();
}
});
}
这是我的ProvinceSpinner setOnItemSelected方法:
spinner_provinsi.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedName = parent.getItemAtPosition(position).toString();
//request data
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
这是我来自邮递员的ProvinceSpinner的json数据:
{
"error": false,
"message": "Berhasil mendapatkan data provinsi",
"semuaprovinsi": [
{
"id": "11",
"nama": "Aceh"
}
]
}
这是邮递员给我的CitySpinner的json数据:
{
"error": false,
"message": "Berhasil mengambil data kabupaten",
"daftar_kecamatan": [
{
"id": "1101",
"id_prov": "11",
"nama": "Kab. Simeulue"
}
]
}
任何人都请指教。 谢谢。
答案 0 :(得分:0)
已解决,请检查此代码:
private void initSpinnerProvinsi() {
apiInterface.getProvinsi()
.enqueue(new Callback<GetProvinsiResponse>() {
@Override
public void onResponse(Call<GetProvinsiResponse> call, Response<GetProvinsiResponse> response) {
if (response.isSuccessful()){
final List<GetProvinsi> provinsiList = response.body().getSemuaprovinsi();
List<String> listSpinnerProvinsi = new ArrayList<String>();
for (int i = 0; i < provinsiList.size(); i++){
listSpinnerProvinsi.add(provinsiList.get(i).getNama());
}
//set result into spinner adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext,
R.layout.support_simple_spinner_dropdown_item, listSpinnerProvinsi);
adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
spinner_provinsi.setAdapter(adapter);
spinner_provinsi.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedName = parent.getItemAtPosition(position).toString();
//request data
String idkota = provinsiList.get(position).getId();
Log.d("idkota :", idkota);
if (selectedName != null)
spinner_kota.setEnabled(true);
initSpinnerKota(idkota);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
else {
Log.d("onResponse: ", "gagal memuat data provinsi");
}
}
@Override
public void onFailure(Call<GetProvinsiResponse> call, Throwable t) {
Toast.makeText(mContext, "Koneksi gagal, silahkan cek koneksi internet anda",
Toast.LENGTH_SHORT).show();
}
});
}