Adapter does not work

In my OncreateView() install an adapter that works when I first load the page. When I go to another page and make changes, return to this snippet, adapter.notifyDatasetchanged() does not work.

  @Override public void onStart() { super.onStart(); groupItem.clear(); childItem.clear(); List<String> child_Category;child_Category=new ArrayList<String>(); groupItem = obj_Listdatabase.fetchcategory(); childItem.clear(); ListIterator<String> iterator = groupItem .listIterator(); while (iterator.hasNext()) { String categoryname = iterator.next(); child_Category = new ArrayList<String>(); child_Category = obj_Listdatabase .fetchchildlist(categoryname); childItem.add(child_Category); } adapter.notifyDataSetChanged(); } 
+4
source share
2 answers

Hai I got a way out for this:

  groupItem.addAll(obj_Listdatabase.fetchcategory()); 

Instead of this

  //groupItem = obj_Listdatabase.fetchcategory(); 

Since I am changing the link in the assignment statement (=) for this adapter. Therefore, I use the addAll () method to store values ​​only instead of a link.

0
source

You should clearly indicate your code more cleanly, never use a few ";" for example, in one line. And you can definitely use BaseExpandableListAdapter, release somewhere else.

You should also notice that the onCreate view is more suitable for "creating a view", so all adapter materials should be somewhere else, for example, onViewCreated or onActivityCreated as needed (these are just some tips)


I assume that your adapter is in the fragment, since you use GetActivity () , and in the constructor of your adapter you pass a link to the activity (context), a list of groups and a list of children .. ok

Suppose you are using the onStart snippet. From the official doc:

 Called when the Fragment is visible to the user. This is generally tied to Activity.onStart of the containing Activity lifecycle. 

Thus, usually this method is called after onViewCreate, onViewCreated .. etc., at least when the code is first run. So it's ok

Have you tried using adapter.notifyDataSetInvalidate () and then doing adapter.notidyDataSetChanged ?


The latter, since you are actually passing data to the designer when updating the lists , how can the adapter be aware of the changes? Are your lists global (static)?

If not, before running the adapter.notifyDataSetChanged () command, you must pass lists (group and children) to an adapter with some setter.

luck

0
source

Source: https://habr.com/ru/post/1489688/


All Articles