我正在通过Xamarin开发基本的Android应用程序,我在List中使用ListItem在Adapter中设置。但是当我删除或编辑任何项目时,listview不会刷新自己。我知道ObservableCollection,但我不知道如何将它与适配器一起使用。有谁可以帮助我吗?我是Xamarin的新人。
适配器:
public class MealsAdapter : BaseAdapter<Meal>
{
List<Meal> items;
Activity context;
public MealsAdapter(Activity context, List<Meal> items)
: base()
{
this.context = context;
this.items = items;
}
public override Meal this[int position]
{
get
{
return items[position];
}
}
public override int Count
{
get
{
return items.Count;
}
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = items[position];
View view = convertView;
if (view == null)
view = context.LayoutInflater.Inflate(Resource.Layout.MealListItem, null);
view.FindViewById<TextView>(Resource.Id.textViewNazev).Text = item.Name;
view.FindViewById<TextView>(Resource.Id.textViewBilkoviny).Text = context.Resources.GetString(Resource.String.BilkovinyNadpis) + ": " + item.Bilkoviny.ToString();
view.FindViewById<TextView>(Resource.Id.textViewSacharidy).Text = context.Resources.GetString(Resource.String.SacharidyNadpis) + ": " + item.Sacharidy.ToString();
return view;
}
}
的ListView:
ListView listView;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.MealsSettings);
listView = FindViewById<ListView>(Resource.Id.listViewJidlo);
listView.Adapter = new MealsAdapter(this, DataStorage.meallist);
listView.ItemClick += ListView_ItemClick;
}
DataStorage.meallist是简单的List。我已经尝试将ObservableCollection放到适配器和DataStorage.meallist中,但它提供了帮助。
答案 0 :(得分:2)
您需要致电NotifyDataSetChanged()
让ListView
知道其收藏集已更改。
无论您何时删除/删除List<Meal> items
中的项目,请确保随后致电NotifyDataSetChanged()
。