How to call notifyDataSetChanged for a custom ExpandableListView?

I am trying to call notifyDataSetChanged for my custom ExpandableListView. what happens for my child list, it shows the files in the selected folder (group list). when a child is clicked, it will ask to delete the file or not. then, after removal, the ExpandableList will be "updated". but for some reason I could not call the notifyDataSetChanged method.

my custom adapter:

public class customListAdapter extends BaseExpandableListAdapter { // Sample data set. children[i] contains the children (String[]) for groups[i]. private File mapFolder = new File (Environment.getExternalStorageDirectory(),"/MLT/A"); private File recordFolder = new File (Environment.getExternalStorageDirectory(),"/MLT/B"); private File scribbleFolder = new File (Environment.getExternalStorageDirectory(),"/MLT/C"); private String[] groups = { "A", "B", "C" }; private String[][] children = { mapFolder.list(), recordFolder.list(), scribbleFolder.list() }; public Object getChild(int groupPosition, int childPosition) { return children[groupPosition][childPosition]; } public long getChildId(int groupPosition, int childPosition) { return childPosition; } public int getChildrenCount(int groupPosition) { return children[groupPosition].length; } public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView textView = new TextView(MLT_File.this); textView.setBackgroundColor(Color.BLACK); textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); textView.setPadding(100, 5, 0, 5); textView.setTextColor(Color.WHITE); textView.setTextSize(23); textView.setId(1000); textView.setText(getChild(groupPosition, childPosition).toString()); return textView; }//getChildView public Object getGroup(int groupPosition) { return groups[groupPosition]; } public int getGroupCount() { return groups.length; } public long getGroupId(int groupPosition) { return groupPosition; } public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { TextView textView = new TextView(MLT_File.this); textView.setBackgroundColor(Color.WHITE); textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); textView.setPadding(100, 0, 0, 0); textView.setTextColor(Color.BLACK); textView.setTextSize(25); textView.setText(getGroup(groupPosition).toString()); return textView; } public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } public boolean hasStableIds() { return true; } public void notifyDataSetChanged() { this.notifyDataSetChanged(); } }//customListAdapter 

my onCreate:

 // Set up our adapter listAdapter = new customListAdapter(); expLV = (ExpandableListView) findViewById(R.id.mlt_expList); expLV.setAdapter(listAdapter); //registerForContextMenu(expLV); expLV.setOnChildClickListener(new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { // TODO Auto-generated method stub String parentName = ""; if (groupPosition == 0) { parentName = "A"; } else if (groupPosition == 1) { parentName = "B"; } else if (groupPosition == 2) { parentName = "C"; } TextView childView = (TextView) v; String childName = childView.getText().toString(); File file = new File(Environment.getExternalStorageDirectory(),"/Folder/"+childName); file.delete(); return false; } });//OnChildClickListener 

notifyDataSetChanged () in my custom is what I tried to do, as I realized that the ExpandableList does not. but still, the method that I created does not appear when I click expLV.n ...

+4
source share
3 answers

well ... because no one told me how to do this, I thought about how it could be stupid, but useful.

what i did was create it in onCreate instead, i created it in onResume (). when I make any changes, it will be created again. The reason is that I use TabMenu, and when I switch between them, the class is not created again and, therefore, the list is in the same state.

using onResume, it also simplifies my work, because when I want it to be updated, I only need to call the address Resume ().

Hope this helps other people. =)

-one
source

Actually there is a notifyDataSetChanged() function on BaseExpandableListAdapter .

You are not achieving anything:

  public void notifyDataSetChanged() { this.notifyDataSetChanged(); } 

delete it.

call adapter.notifyDataSetChanged() from onClick(View v) to OnItemClickListener after changing the dataset.

+5
source

I know this is old, but for those of you still facing this problem, you can use invalidateViews() ExpandableListeView as mentioned here

0
source

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


All Articles