2011-04-25 33 views
0

我在我的应用程序中使用自定义列表视图。在所有行的自定义列表视图中,我放置了一个图像按钮。如果我点击该图像按钮,我必须删除自定义列表视图中单击的按钮行。任何人都可以告诉我如何做到这一点?下面的代码是不工作:自定义列表视图在Android中删除行

imgcross=(ImageView)v.findViewById(R.id.imgcross); 
imgcross.setId(position); 

if(v.getId()==R.id.imgcross) 
     { 
      Log.d("image id is",Integer.toString(imgcross.getId())); 
      myScheduleList.removeViewAt(imgcross.getId()); 
      Toast.makeText(MyScheduleDay0RequestedMeeting.this, "Cross Button is Clicked", Toast.LENGTH_LONG).show(); 

     } 






if (v.getId()==R.id.imgcross) 
{ //Integer index=(Integer)imgcross.getTag(); 
//Log.d("image id is",Integer.toString(index)); 
int index=imgcross.getId(); (imgcross.getId()); 
MyScheduleBean.listName.remove(index); 
MyScheduleBean.dateValue.remove(index); 
MyScheduleBean.dateValue.remove(index); 
CAdapter = new CustomAdapter(this,MyScheduleBean.listName,MyScheduleBean.dateValue,MyScheduleBe­an.meeting,R.layout.myschedule_day0_requestedmeetingrow,to); 
myScheduleList.setAdapter(CAdapter); 
} 

感谢

回答

0

您需要从底层Adapter删除数据。正确完成后,这会自动更新ListView。否则,请致电notifyDataSetChanged()Adapter,这将导致ListView更新。

+0

你可以告诉你怎么在点击事件底层适配器删除数据,即使我试图与此 – mohan 2011-04-26 09:21:07

+0

如果(v.getId()== R.id.imgcross) \t \t { \t \t \t \t \t \t //整数指数=(整数)imgcross.getTag(); \t \t \t //Log.d("image id is“,Integer.toString(index)); \t \t \t int index = imgcross.getId(); \t \t \t \t \t \t(imgcross.getId()); \t \t MyScheduleBean.listName.remove(index); \t \t MyScheduleBean.dateValue.remove(index); \t \t MyScheduleBean.dateValue.remove(index); \t \t CAdapter = new CustomAdapter(this,MyScheduleBean.listName,MyScheduleBean.dateValue,MyScheduleBean.meeting,R.layout.myschedule_day0_requestedmeetingrow,to); \t \t \t myScheduleList.setAdapter(CAdapter); \t \t \t \t \t \t} – mohan 2011-04-26 09:22:33

+0

@mohan:我不知道任何代码一样。要从ArrayAdapter中移除数据,请调用'ArrayAdapter'上的remove()。要从“CursorAdapter”中删除数据,请修改底层的数据源(例如数据库)和'requery()''Cursor'。 – CommonsWare 2011-04-26 11:08:53

0

我一直在寻找这个答案太久 - 非常感谢你。 从数据库中删除记录后:

adapter.remove(dbRecord); 

我在长按在ListView这样做:

DBRecordOperation.deleteRecord(dbRecord); 

简单地从ListView控件与删除记录。对于onContextItemSelected的完整代码:

@Override 
public boolean onContextItemSelected(MenuItem item){ 

    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo(); 
    DBRecord dbRecord = (DBRecord) getListAdapter().getItem(info.position); 
    int dbRecordId = dbRecord.getId(); 

    DBRecordOperation.deleteRecord(dbRecord); 
    adapter.remove(dbRecord); 


    return true; 

} 
相关问题