Myadapter.notifyDataSetChanged()抛出错误

时间:2012-08-03 07:41:26

标签: android

我正在尝试刷新适配器以及我的列表。但我对于放置方法Myadapter.notifyDataSetChanged()和list.clear()的位置几乎没有什么困惑,所以它们对我有用...任何人都可以编辑我的代码并帮助我从中出来..这是我的代码

@覆盖

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    calendars = Uri.parse("content://com.android.calendar/events");
    managedCursor = getContentResolver().query(calendars, null, null, null, null);
    if(managedCursor.getCount()==0){
        Toast.makeText(getBaseContext(), "There Is No Event To Show", Toast.LENGTH_SHORT).show();
        Intent i=new Intent(DisplayEventActivity.this,EventCalendarActivity.class);
        startActivity(i);
        finish();

    }else{
    setContentView(R.layout.displayevent);
    id=getIntent().getExtras().getLong("ID");
    event_ListView=(ListView)findViewById(R.id.eventList);
    event_ListView.setOnItemClickListener(this);

    }
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    model.getEventTitleList().clear();  
    myadapter.notifyDataSetChanged();
    event_ListView.setAdapter(myadapter);

    if(id==9){
        model=new MyEventModel(managedCursor);
        myadapter=new MyListAdapter(getApplicationContext(), model.getEventTitleList());
        event_ListView.setAdapter(myadapter);

    }else if(id==10){
        model=new MyEventModel(managedCursor);
        myadapter=new MyListAdapter(getApplicationContext(), model.getEventTitleList());
        event_ListView.setAdapter(myadapter);
    }

}


public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
    event_Id=model.getEventId();
    System.out.println("Event id from disp="+event_Id);
    Intent i=new Intent(DisplayEventActivity.this,UpdateEvent.class);
    i.putExtra("EventId", event_Id);
    startActivity(i);
    }
}

2 个答案:

答案 0 :(得分:1)

您正在使用myadapter.notifyDataSetChanged(); 在初始化myadapter之前。在使用之前初始化它。

答案 1 :(得分:0)

您应该在适配器中设置数据后通知数据更改。

protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    model.getEventTitleList().clear();  
    event_ListView.setAdapter(myadapter);

    if(id==9){
        model=new MyEventModel(managedCursor);
        myadapter=new MyListAdapter(getApplicationContext(), model.getEventTitleList());
        event_ListView.setAdapter(myadapter);

    }else if(id==10){
        model=new MyEventModel(managedCursor);
        myadapter=new MyListAdapter(getApplicationContext(), model.getEventTitleList());
        event_ListView.setAdapter(myadapter);
    }
    myadapter.notifyDataSetChanged();


}