Android / Monodroid - NotifyDataSetChange不刷新视图

时间:2013-08-16 16:18:43

标签: c# android list xamarin.android

我正在使用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();
        }
    }

列表由我的数据库中的车辆填充,以防您想知道,所以我希望它能够删除车辆,然后刷新列表。

由于

1 个答案:

答案 0 :(得分:0)

您始终可以将ListAdapater重新实例化为新实例。假设您的删除方法与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
    }
}