如何从数据json更新微调器? 示例微调器B仅检索类别“ket”的数据,并且响应json已经转换为arraylist但我无法从json更新1个数组的适配器微调器
// EXAMPLE JSON
[{"kode":"I5","hrg":5700,"nom":"5000","ket":"IM3 REG 5000"},{"kode":"ISG5","hrg":5700,"nom":"5000","ket":"IM3 GPRS ALTERNATIF 5000"},{"kode":"ISS5","hrg":5700,"nom":"5000","ket":"IM3 SMS ALTERNATIF 5000"},{"kode":"IS5","hrg":5700,"nom":"5000","ket":"IM3 REG ALTERNATIF 5000"},{"kode":"I5S","hrg":5700,"nom":"5000","ket":"IM3 SMS 5000"},{"kode":"ITG5","hrg":5700,"nom":"5000","ket":"IM3 GPRS 5000"},{"kode":"ITG10","hrg":10700,"nom":"10000","ket":"IM3 GPRS 10000"},{"kode":"I10","hrg":10700,"nom":"10000","ket":"IM3 10000"},{"kode":"I10S","hrg":10700,"nom":"10000","ket":"IM3 SMS 250 10000"},{"kode":"IS10","hrg":10700,"nom":"10000","ket":"IM3 REG ALTERNATIF 10000"},{"kode":"ISG10","hrg":10700,"nom":"10000","ket":"IM3 GPRS ALTERNATIF 10000"},{"kode":"ISS10","hrg":10700,"nom":"10000","ket":"IM3 SMS ALTERNATIF 10000"},{"kode":"I20","hrg":20400,"nom":"20000","ket":"IM3 REG 20000"},{"kode":"I25","hrg":25200,"nom":"25000","ket":"IM3 25000"},{"kode":"I25S","hrg":25400,"nom":"25000","ket":"IM3 SMS 1667 25000"},{"kode":"ITG25","hrg":25400,"nom":"25000","ket":"IM3 GPRS 25000"},{"kode":"I30","hrg":30400,"nom":"30000","ket":"IM3 REG 30000"},{"kode":"I50","hrg":49425,"nom":"50000","ket":"IM3 50000"},{"kode":"I100","hrg":98500,"nom":"100000","ket":"IM3 100000"}]}
// GET JSON TO LIST
private static ArrayList<DataVoucher> processResponse(String response) {
ArrayList<DataVoucher> listdata = new ArrayList<DataVoucher>();
try {
JSONArray jsonArray = new JSONArray(response);
Log.d(TAG, "data lengt: " + jsonArray.length());
DataVoucher dataVoucher = null;
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
dataVoucher = new DataVoucher();
// dataVoucher.setKodeVoucher(obj.getString("kode"));
// dataVoucher.setHrgVoucher(obj.getString("hrg"));
// dataVoucher.setNomVoucher(obj.getString("nom"));
dataVoucher.setKetVoucher(obj.getString("ket"));
listdata.add(dataVoucher);
}
} catch (JSONException e) {
Log.d(TAG, e.getMessage());
}
return listdata;
}
// SENT POST TO SERVER
public static void requestDataVoucher(final String operator) {
Thread thread = new Thread() {
public void run() {
Looper.prepare();
final String TAG = "SEND JSON";
JSONObject jsonObjSend = new JSONObject();
try {
jsonObjSend.put("type", "svoc");
jsonObjSend.put("hp", "089631633614");
jsonObjSend.put("opr", operator);
Log.i(TAG, jsonObjSend.toString(2));
} catch (JSONException e) {
e.printStackTrace();
}
// Send the HttpPostRequest and receive a JSONObject in return
JSONObject jsonObjRecv = SendHttpPost(jsonObjSend);
Looper.loop();
}
};
thread.start();
}
// ASYNTASK update spinner
private class MainActivityAsync extends AsyncTask<Void, Integer, Void> {
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("retrieving...");
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected Void doInBackground(Void... values) {
/** send post from list spinner A */
requestDataVoucher(pilihOperator.getSelectedItem().toString());
return null;
}
@Override
protected void onPostExecute(Void result) {
progressDialog.dismiss();
runOnUiThread(new Runnable() {
@Override
public void run() {
populateSpinnerVoucher();
}
});
}
}
private void populateSpinnerVoucher() {
adapter = new ArrayAdapter<DataVoucher>(this,
android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
pilihVoucher.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
答案 0 :(得分:0)
我认为您忘记将这些项目添加到list
。看看:
adapter = new ArrayAdapter<DataVoucher>(this,
android.R.layout.simple_spinner_item, list);
您正在list
使用adapter
,但是您在哪里将项目插入列表?您可能应该在onBackground
。
并且不需要在runOnUiThread
中使用onPostExecute
,因为onPostExecute
已经在UI线程上执行了代码。