2021-12-09

Tech Programing

程式人小天地

Arraylist not getting repopulated after using notifyDataSetChanged() in java andriod

2 min read


I am trying to use arraylist.clear and adapter.notifyDataSetChanged() to clear an arraylist the list is cleared successfully but when i try to reload the list again with some data the list is not showing empty despite the arraylist.size showing the total number of items which is greater than 0. Here is the method to create the initial list

private void getTH(int ID) {
        url = "url";
        mainAdapterClasses = new ArrayList<>();

        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, response -> {
            try {
                JSONObject jsonObject0 = new JSONObject(response);
                String code = jsonObject0.getString("code");;
                if (code.matches("200")) {
                    JSONArray jsonArray = jsonObject1.getJSONArray("items");
                    if(jsonArray.length() == 0){
                        txtEmpty.setText(String.format("%s %s", getString(R.string.no), getString(R.string.empty)));
                    } else {
                        for (int k = 0; k < jsonArray.length(); k++) {
                            JSONObject jsonObject = jsonArray.optJSONObject(k);
                            String na = jsonObject.optString("na", "NA");
                            String aT = jsonObject.optString("Type", "NA")
                            
                            MainAdapterClass mainAdapterClass = new MainAdapterClass();
                            mainAdapterClass.setName(na);
                            mainAdapterClass.setType(aT);
                            
                            mainAdapter = new TAdapter(WalletHome.this, mainAdapterClasses);
                            listView.setAdapter(mainAdapter);
                            mainAdapter.notifyDataSetChanged();
                            txtEmpty.setText("Data Is Here"+mainAdapterClasses.size());
                        }
                    }
                }
            } catch (JSONException e) {
                Toast.makeText(this, ""+getString(R.string.unknown_err), Toast.LENGTH_SHORT).show();
            }
        })
        requestQueue.add(stringRequest);
    }

and here is the code that is supposes to reload the list

    @Override
    public void getRadioButtonListener1(int position) {
        WClass wClass = stateClassArrayList.get(position);
        wId = wClass.getWId();
        if(mainAdapterClasses.size() > 0){
            mainAdapterClasses.clear();
        }
        mainAdapter.notifyDataSetChanged();
        getTH(id);
    }

I tried to log the issue here the method is getting the current total of items everytime i run the method the only issue is that the list is not getting populated with the new data once it is initially cleared.

txtEmpty.setText("Data Is Here"+mainAdapterClasses.size());

Any help will be greatly appreciated thanks



Source link

資料來源:Stackoverflow

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *