我正在使用Expandable ListView,其中的数据通过Sqlite数据库填充.我已经选择从ExpandableListView中删除一个组,它实际上删除了数据库中的相应行.但是活动不会刷新,删除的“案例”是可见的,直到有人返回并重新参加此活动.

我创建了一个扩展* BaseExpandableListAdapter *的类.它包含所有重写的方法.以下是代码:

public class ExpandableListAdapterForRunning extends BaseExpandableListAdapter {

private Context _context;

private List _listDataHeader; // header titles

public static List _listDate;

public int index;

// child data in format of header title, child title

private HashMap> _listDataChild;

public ExpandableListAdapterForRunning(Context context, List listDataHeader,

HashMap> listChildData,int i,List 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

}

现在有另一个类RunningCase.java扩展了Activity.此类用于填充ExpandableListView中的数据.在ExpandableListView中的任何组的OnLongClick()(点击并按住)中,将出现一个上下文菜单.上下文菜单包含一个选项Delete,用于从数据库中删除该案例并刷新RunningCase活动.

public class RunningCase extends Activity

{

String[] day;

LinearLayout layout_MainMenu;

Case cases;

ExpandableListAdapterForRunning listAdapter;

ExpandableListView expListView;

List listDataHeader;

List listDate;

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

listDate =new ArrayList();

listDataChild = new HashMap>();

final DatabaseHandler db = new DatabaseHandler(this);

List 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 case2 = new ArrayList();

case2.add("Case No. : "+caseNo);

case2.add("Court Name :"+caseName);

case2.add("Case Stage :"+caseStage);

listDataChild.put(listDataHeader.get(i++), case2);

}

List date = db.getRunning_child();

for (Date cn : date) {

String str = cn.getNextDate();

listDate.add(str);

}

}

方法db.getRunning(),db.getRunning_child()从数据库中检索数据.

此外,以下是从数据库中删除案例的代码,它完美地工作:

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

}

请帮帮我.删除案例时应刷新活动,以便不再显示已删除的案例.

提前致谢!!

解决方法:

在调用notfifyDataSetChanged之前,您应该更新您的私有HashMap>数据. _listDataChild是由您在适配器上创建的setData方法.首先设置数据,然后通知.

例:

public class MyAdapter extends BaseAdapter {

public MyAdapter(...) {

super(...);

}

@Override

public View getView(int position, View view, ViewGroup parent) {

.....

return view;

}

private void setData() {

...

notifyDataSetChanged();

}

}

标签:android,expandablelistview,notifydatasetchanged

来源: https://codeday.me/bug/20190703/1365444.html

android notifydatasetchanged 刷新错误,android – notifyDataSetChanged()不刷新可扩展列表视图...相关推荐

  1. android notifydatasetchanged 刷新错误,Android 调用notifyDataSetChanged方法失败解决办法

    Android 调用notifyDataSetChanged方法失败解决办法 如果使用ListView.GridView等进行数据展示,当绑定的数据有了更新的时候,需要实时刷新ListView,即调用 ...

  2. android notifydatasetchanged 刷新错误,Android中适配器的notifyDataSetChanged()为何有时不刷新...

    学过Android开发的人都知道,ListView控件在开发中经常遇到,并且ListView通常结合Adapter适配器来进行数据显示和数据更新操作.姑且假设数据存储在名为dataList的成员变量中 ...

  3. android可扩展列表,android-可扩展列表视图未显示

    代码可以正常运行,没有任何错误或警告,但可扩展列表未显示,这是代码 public class MainActivity extends Activity implements ExpandableLi ...

  4. android 无刷新 好友,Android viewPage notifyDataSetChanged无刷新

    转载 http://www.67tgb.com/?p=624 最近项目结束,搞了一次代码分享.其中一位同学分享了一下自己在解决问题过程中的一些心得体会,感觉受益匪浅.整理出来,分享给大家. 建议使用自 ...

  5. android智能刷新smart,Android横向智能刷新框架-SmartRefreshHorizontal

    距离上次发文又过了一年,此次我给你们带来了另外一个开源库 SmartRefreshHorizontal,下面先看看效果:java 传送门 Demo 效果演示 商品详情 ViewPager 数据演示 由 ...

  6. Android布局文件错误,android 布局文件常见错误

    1.'Missing contentDescription attribute on image' in XML 原因:缺少图片描述,在 ADT 16 开始会抛出这个警告,确保开发者为图片添加内容描述 ...

  7. android怎么刷新活动,Android 如何刷新当前activity的显示数据?

    "某项操作发生引起数据变化"我没有理解错的话,可以用观察者模式去处理,因为他是一个操作引发另一个操作嘛,而且这样做也比较简单,省心. 首先注册数据监听: getContentRes ...

  8. android autolink识别错误,android - autolink窃取ontouch事件,从而反馈触摸列表视图行无效 - 堆栈内存溢出...

    我有一个listview . 并且行布局中包含很少的布局. 在行布局中,我有一个TextView ,我想在其上启用自动链接当我启用自动链接时,链接工作正常但我们点击列表行时得到的反馈不再有效. 在行布 ...

  9. android notifydatasetchanged 刷新错误,错误:无法解析notifyDataSetChanged(); Android的

    我在更新ListView时遇到了一些麻烦. 所以我使用了notifyDataSetChanged();但它说它无法解决. 下面是代码中不起作用的部分: public void onCreate(Bun ...

最新文章

  1. Zookeeper源码分析:Follower角色初始化
  2. java串口通信DataRecive_串口通信之DataReceive事件触发时机
  3. mysql like reverse_mysql 优化like查询
  4. [转载]java中Date,SimpleDateFormat
  5. j详细说明ava于clone办法
  6. Window平台 mysql8.0下载安装方式
  7. RN返回navigation方法
  8. 【数据结构和算法笔记】队列(Queue)详解:c实现
  9. c语言中extern关键字
  10. 《数学之美》—自然语言处理
  11. kali2020.4 root权限下无法启动自带浏览器解决办法
  12. 小米手机刷机工具MiFlash
  13. java根据入参不同调不同方法_java根据传入参数不同调用不同的方法,求高手支妙招!...
  14. 《文明之光》第六章读书笔记
  15. echart 折线图 ---- 坐标轴、网格、折线配置
  16. MyCat做MySQL负载均衡(享学课堂,咕泡学院听课笔记)
  17. 虚拟化 半虚拟化 全虚拟化_我的虚拟实习经历
  18. 书到用是方恨少......
  19. WEB页面或者H5页面如何打开高德或者百度地图APP导航(实战向)
  20. cherry键盘alt+tab快捷键失效

热门文章

  1. ETL工具(数据同步)
  2. oa办公系统是什么?
  3. 《速度与激情8》也要出手游了!前赴后继的“影游联动”如何走得更远?
  4. svn revert 批量文件夹
  5. Java实现《逢七过》的游戏
  6. 六步骤确保企业成功选择CRM
  7. 线性回归系数的标准误_回归模型中的标准化回归系数是什么,该如何计算得到?...
  8. python adb模块,python + adb 实现控制手机,,主要步骤:1、USB
  9. DIY掌上POS机,或许是最小的收银POS机了!
  10. 数据结构 | 单向链表学习总结