NotifyDataSetChanged () does not update the list of extensible lists

I use the Expandable ListView , and the data in it is populated through the Sqlite database. I gave the option to remove the group from the ExpandableListView and actually removes the corresponding row in the database. But the activity is not updated, and the deleted "case" is displayed until one returns and returns to this activity . I created a class that extends *BaseExpandableListAdapter* . It contains all methods overridden . Below is the code:

 public class ExpandableListAdapterForRunning extends BaseExpandableListAdapter { private Context _context; private List<String> _listDataHeader; // header titles public static List<String> _listDate; public int index; // child data in format of header title, child title private HashMap<String, List<String>> _listDataChild; public ExpandableListAdapterForRunning(Context context, List<String> listDataHeader, HashMap<String, List<String>> listChildData,int i,List<String> listDate) { this._context = context; this._listDataHeader = listDataHeader; this._listDataChild = listChildData; this.index=i; this._listDate=listDate; } @Override public Object getChild(int groupPosition, int childPosititon) { return this._listDataChild.get(this._listDataHeader.get(groupPosition)) .get(childPosititon); } //other overridden methods } 

Now there is also the class RunningCase.java , which extends Activity . This class used to populate data in an ExpandableListView . OnLongClick() (tap and hold) of any group in ExpandableListView , a context menu will appear. context menu contains the Delete option to remove this case from the database and update RunningCase activity .

  public class RunningCase extends Activity { String[] day; LinearLayout layout_MainMenu; Case cases; ExpandableListAdapterForRunning listAdapter; ExpandableListView expListView; List<String> listDataHeader; List<String> listDate; HashMap<String, List<String>> listDataChild; Case date; protected void onCreate(Bundle savedInstanceState) { prepareListData(); expListView=(ExpandableListView) findViewById(R.id.lvExp); listAdapter= new ExpandableListAdapterForRunning(this,listDataHeader,listDataChild,i,listDate); expListView.setAdapter(listAdapter); registerForContextMenu(expListView); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.add("Delete"); } @Override public boolean onContextItemSelected(MenuItem item) { // TODO Auto-generated method stub super.onContextItemSelected(item); final DatabaseHandler dbs = new DatabaseHandler(this); Cursor c = null; ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) item .getMenuInfo(); int type = ExpandableListView.getPackedPositionType(info.packedPosition); int groupPosition = ExpandableListView.getPackedPositionGroup(info.packedPosition); if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) { String cp=(String) listAdapter.getChild(groupPosition, 0); caseno=cp.substring(11, cp.length()); } if(item.getTitle()=="Delete") { dbs.deletecase(caseno); expListView.post(new Runnable() { public void run() { listAdapter.notifyDataSetChanged(); //this is not working } }); Toast.makeText(this, "The Case has been Deleted.", Toast.LENGTH_LONG).show(); } return true; } //this is the method to populate the expandableListView public void prepareListData() { listDataHeader = new ArrayList<String>(); listDate =new ArrayList<String>(); listDataChild = new HashMap<String, List<String>>(); final DatabaseHandler db = new DatabaseHandler(this); List<Case> cases = db.getRunning(); int i=0; for (Case cn : cases) { String str = cn.getPetitioner()+" V/S " + cn.getDefendant(); String caseNo=cn.getCaseNumber(); String caseStage=cn.getCaseStage(); String caseName=cn.getCourtName(); listDataHeader.add(str); List<String> case2 = new ArrayList<String>(); case2.add("Case No. : "+caseNo); case2.add("Court Name :"+caseName); case2.add("Case Stage :"+caseStage); listDataChild.put(listDataHeader.get(i++), case2); } List<Date> date = db.getRunning_child(); for (Date cn : date) { String str = cn.getNextDate(); listDate.add(str); } } 

The db.getRunning() , db.getRunning_child() methods retrieve data from the database. In addition, the following code is code to remove from the database, which works fine:

 void deletecase(String casenum){ SQLiteDatabase db = this.getWritableDatabase(); String whereClause = "Case_number = ?"; String[] whereArgs = new String[] { casenum }; db.delete(TABLE_CASEINFO,whereClause,whereArgs); db.delete(TABLE_CLIENTINFO,whereClause,whereArgs); db.delete(TABLE_DATEINFO,whereClause,whereArgs); Log.d("case deleted","casenum: "+casenum); db.close(); // Closing database connection } 

Please help me. activity should be updated when deleting a case to exclude an extra case. Thanks in advance!!

+4
android expandablelistview notifydatasetchanged
source share
1 answer

Before calling notfifyDataSetChanged you must update your data, which is the private method HashMap> _listDataChild with the setData method that you create on your adapter. Set the data first, then notify.

Example:

 public class MyAdapter extends BaseAdapter { public MyAdapter(...) { super(...); } @Override public View getView(int position, View view, ViewGroup parent) { ..... return view; } private void setData() { ... notifyDataSetChanged(); } } 
+2
source share

All Articles