C# Android/Monodroid-NotifyDataSetChange不刷新视图

C# Android/Monodroid-NotifyDataSetChange不刷新视图,c#,android,list,xamarin.android,C#,Android,List,Xamarin.android,我正在使用monodroid并尝试使用NotifyDataSetChange。但是当我在listadapter上调用它时,它不会刷新视图 我的代码 CustomListAdapter listAdapter; ListView listView; protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resour

我正在使用monodroid并尝试使用NotifyDataSetChange。但是当我在listadapter上调用它时,它不会刷新视图

我的代码

    CustomListAdapter listAdapter;
    ListView listView;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.customlist);

        listAdapter = new CustomListAdapter(this);
        listView = FindViewById<ListView>(Resource.Id.listView);
        //Hook up our adapter to our ListView
        listView.Adapter = listAdapter;

        listView.ItemLongClick += listView_ItemLongClick;
    }

    void listView_ItemLongClick(object sender, AdapterView.ItemLongClickEventArgs e)
    {

     var item = this.listAdapter.GetItemAtPosition(e.Position);

     deletevehicle(item);

    }


    private void deletevehicle(Vehicle v)
    {
        if (v != null)
        {
            VehicleDB.DeleteVehicle(v.ID);
            listAdapter.NotifyDataSetChanged();
        }
    }
CustomListAdapter-listAdapter;
列表视图列表视图;
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.customlist);
listAdapter=新的CustomListAdapter(此);
listView=FindViewById(Resource.Id.listView);
//将适配器连接到ListView
Adapter=listAdapter;
listView.ItemLongClick+=listView\u ItemLongClick;
}
void listView_ItemLongClick(对象发送者,AdapterView.ItemLongClickEventArgs e)
{
var item=this.listAdapter.GetItemAtPosition(e.Position);
删除车辆(项目);
}
私家车(v车)
{
如果(v!=null)
{
车辆B.删除车辆(v.ID);
listAdapter.NotifyDataSetChanged();
}
}
该列表由我的数据库中的车辆填充,以防您感到奇怪,因此我希望它能够删除车辆,然后刷新列表

private void deletevehicle(Vehicle v)
{
    if (v != null)
    {
        //Raise up a progress dialog here
        //I'm assuming that VehicleDB.DeleteVehicle is actually interacting with a service, 
        //or deleting the record from a local sqllite db.
        VehicleDB.DeleteVehicle(v.ID);
        listAdapter = new CustomListAdapter(this);
        //Make the progress Dialog invisible again
    }
}

谢谢

您始终可以将ListAdapater重新实例化为新实例。假设您的delete方法与web服务交互。。并导致您需要的数据更改。我建议在刷新数据时使用对话框

private void deletevehicle(Vehicle v)
{
    if (v != null)
    {
        //Raise up a progress dialog here
        //I'm assuming that VehicleDB.DeleteVehicle is actually interacting with a service, 
        //or deleting the record from a local sqllite db.
        VehicleDB.DeleteVehicle(v.ID);
        listAdapter = new CustomListAdapter(this);
        //Make the progress Dialog invisible again
    }
}

您没有从
适配器中删除它
?我所做的只是代码中显示的内容,是否还有其他我应该做的事情?我没有看到它从
适配器
保存的项目源中删除。
适配器是否以某种方式侦听数据库?如果是这样的话,如何通知它从中删除了什么?不,我有一个车辆列表,我用该列表填充ListAdapter。调用delete vehicle(删除车辆)时,将删除该车辆,从而从列表中删除该车辆。我现在需要listview用新更新的listview刷新我看不到代码中的任何地方…然后需要验证listadapter是否确实获取了正确的数据,或者delete方法是否确实有效,因为适配器的新实例(如果确实删除了记录)应返回较短的列表。此链接指向“文档活动”的一些代码。可以稍微扩展以管理文件。