【问题标题】:notifyDataSetChanged() not refreshing expandable list viewnotifyDataSetChanged() 不刷新可扩展列表视图
【发布时间】:2014-03-14 12:36:35
【问题描述】:

我正在使用Expandable ListView,其中的数据通过Sqlite 数据库填充。我提供了一个从ExpandableListView 中删除组的选项,它实际上删除了数据库中相应的row。 但活动不会刷新,并且删除的“案例”是可见的,直到一个人返回并再次来到这个activity。 我创建了一个class 那个extends *BaseExpandableListAdapter*。它包含所有overridden 方法。以下是代码:

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
}    

现在还有另一个class RunningCase.java extends Activity。这个class 用于填充ExpandableListView 中的数据。 OnLongClick()(点击并按住)ExpandableListView 中的任何组,将出现 context menu。 context menu 包含一个选项Delete,用于从数据库中删除该案例并刷新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() 方法从数据库中检索数据。 此外,以下是从数据库中删除案例的代码,运行良好:

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      
}

请帮帮我。 activity 应该在删除案例时刷新,以便不再出现已删除的案例。 提前致谢!!

【问题讨论】:

  • 您从数据库中删除了该条目,但并未将其从列表中删除。你应该修改你的长点击例程
  • 我应该如何从列表中删除它?你能帮我写代码吗

标签: android expandablelistview notifydatasetchanged


【解决方案1】:

在调用notfifyDataSetChanged 之前,您应该通过您在适配器上创建的setData 方法更新您的私有HashMap 数据> _listDataChild。先设置数据,再通知。

例子:

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() 之前调用方法 prepareListData() ?
  • @ankul09jain 我不这么认为。您应该更新适配器中的数据。当您第一次创建适配器实例时,您会通过构造函数向适配器发送一些数据。 prepareListData() 无法更新。
  • 谢谢@MSr。我在调用 notifyDataSetChanged() 之前添加了以下行并且它起作用了:listDataHeader.remove(groupPosition); listDate.remove(groupPosition); for(int j=0;j
  • 第一次添加上述代码行。但是当我连续删除另一个案例时,在 ExpandableListAdapterForRunning.java 的 getChild() 方法下的 getChildPosition() 处会发生 NullPoiinterException。请告诉我哪里出错了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多