Home > Software design >  Arraylist not getting repopulated after using notifyDataSetChanged() in java andriod
Arraylist not getting repopulated after using notifyDataSetChanged() in java andriod

Time:12-09

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 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);
                            mainAdapterClasses.add(mainAdapterClass);

                        }
                            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(mainAdapter.getCount() > 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

CodePudding user response:

At first glance it looks like you are creating and populating the "MainAdapterClass" with the "na" and "aT" variables but not using them anywhere, so you are not really adding items to mainAdapterClasses array.

Second of all, in the for loop you are recreating and setting the adapter each time. I would advise creating and setting the adapter outside of the foor loop once you have your array (mainAdapterClasses) populated with the requested items.

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);

    mainAdapterClasses.add(mainAdapterClass);
} //for 
mainAdapter = new TAdapter(WalletHome.this, mainAdapterClasses);
listView.setAdapter(mainAdapter);
mainAdapter.notifyDataSetChanged();
txtEmpty.setText("Data Is Here" mainAdapterClasses.size());

CodePudding user response:

You forgot to add mainAdapterClass to the ArrayList.

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);

    mainAdapterClasses.add(mainAdapterClass); // Add this line.
}

Don't setAdapter inside the loop. Just add items and then set the adapter outside the loop. Also calling notifyDataSetChanged() after setting adapter doesn't make sence.

for (int i = 0; i < jsonArray.length(); i  ) {
   // ...
}

listview.setAdapter(mainAdapter);
  • Related