notifyDataSetChanged()不刷新扩展列表视图 [英] notifyDataSetChanged() not refreshing expandable list view

查看:322
本文介绍了notifyDataSetChanged()不刷新扩展列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用可扩展的ListView 及其中的数据通过 sqlite的数据库填充。我给了一个选项,删除 ExpandableListView A组,它实际上会删除数据库中的相应但活动不被刷新和删除的案例是可见的,直到一个返回并再次谈到这个活动
我创建了一个扩展* BaseExpandableListAdapter * 。它包含了所有的覆盖方法。以下是code:

I am using Expandable ListView and the data in it is populated through Sqlite database. I have given an option to delete a group from ExpandableListView and it actually deletes the respective row in the database. But the activity doesn't gets refreshed and the deleted 'case' is visible until one goes back and comes again to this activity. I have created a class that extends *BaseExpandableListAdapter*. It contains all the overridden methods. Following 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
}    

现在有另一个 RunningCase.java <其中code>延伸活动。这用来填充 ExpandableListView 中的数据。 OnLongClick() ExpandableListView 任何一组(点住),一个上下文菜单出现。在上下文菜单包含一个选项删除以从数据库中删除的情况下,并刷新 RunningCase活动

Now there is another class RunningCase.java which extends Activity. This class is used to populate the data in ExpandableListView. OnLongClick() (tap and hold) of any group in ExpandableListView, a context menu appears. The context menu contains an option Delete to delete that case from the database and refresh the 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);
    }
}

该方法 db.getRunning() db.getRunning_child()从数据库中检索数据。
此外,以下是code来删除数据库是可以正常使用的情况下:

The methods db.getRunning(), db.getRunning_child() retrieve data from database. Also, the following is the code to delete the case from database which is working perfectly:

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 out. The activity should get refreshed on deleting a case so that the deleted case no more appears. Thanks in advance!!

推荐答案

在调用之前 notfifyDataSetChanged 你应该更新你的数据这是私人的HashMap>按_listDataChild 使用setData 方法。第一组数据,然后通知。

Before calling notfifyDataSetChanged you should update your data which are private HashMap> _listDataChild by setData method which is created by you on your adapter. First set data, then notify.

例如:

public class MyAdapter extends BaseAdapter {

public MyAdapter(...) {
    super(...);
}

@Override
public View getView(int position, View view, ViewGroup parent) {
    .....
    return view;

}

private void setData() {
    ...
   notifyDataSetChanged();
}
}

这篇关于notifyDataSetChanged()不刷新扩展列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆