Home > Mobile >  How to display list data from database to spinner based on id properly?
How to display list data from database to spinner based on id properly?

Time:01-27

i'm newbie in android studio. I'm trying to display list of NAMA_PROSES based on NOMOR_KK. this is my database example

NOMOR_KK URUT NAMA_PROSES
6666 1 STICKER
6666 2 STAMPING, SISIR
6666 3 CETAK, PONZ, KELUPAS
6666 4 SORTIR IN ROLL
6666 5 RAJANG COUNTER
6037 1 COATING & TREAT
6037 2 CETAK HOLOGRAM
6037 3 POTONG & SORTIR
6037 4 REWIND & SORTIR

When i try to display NAMA_PROSES based on NOMOR_KK, the output is correct like the picture below enter image description here

but when I change to another KK number, the history of the old KK number is still listed enter image description here

i still looking for how to clear previous history

spinJSON.java

private void spinJSON(String response){
    try {
        JSONObject obj = new JSONObject(response);
        if (obj.optString("kode").equals("true")){
            dataModels = new ArrayList<>();
            JSONArray dataArray = obj.getJSONArray("data");

            for (int i = 0; i < dataArray.length(); i  ) {
                DataModel dataModel = new DataModel();
                JSONObject dataobj = dataArray.getJSONObject(i);

                dataModel.setNAMA_PROSES(dataobj.getString("NAMA_PROSES"));
                dataModels.add(dataModel);
            }

            for (int i = 0; i < dataModels.size(); i  ){
                    getnamaProses.add(dataModels.get(i).getNAMA_PROSES().toString());
            }
                ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, getnamaProses);
                spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinnerKK.setAdapter(spinnerArrayAdapter);
            }
    } catch (JSONException e){
        e.printStackTrace();
    }
}

CodePudding user response:

Basically the logic of your code has nothing wrong, but you have to pay attention to this line:

ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, getnamaProses);

The String targets to be chosen in the Spinner are from this variable getnamaProses.

Seems that getnamaProses is a global variable as I cannot see you declare it within the provided function. And you just keep adding String to getnamaProses without clearing it.

So you have to do something like getnamaProses.clear(); before you add String to it.

  • Related