几天来,我一直坚持在填充的 ListView 中更改单行,并尝试了在这里以及在xamarin上找到的许多 (java) 解决方案,但作为一名学生,我经常看到发布的解决方案没有适当的文档说明它的工作方式和原因. 到目前为止,我已经为股票 ListView 创建了这个自定义适配器:
//How I initialize the Adapter and ListView
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
List<string> online = new List<string>();
CustomAdapter lvOnlineAdapter = new CustomAdapter(this, online);
ListView lvOnline = FindViewById<ListView>(Resource.Id.lvOnline);
lvOnline.Adapter = lvOnlineAdapter;
lvOnlineAdapter.Add("Test1");
lvOnlineAdapter.Add("Test2");
lvOnlineAdapter.Add("Test3");
for(int i = 0; i < lvOnlineAdapter.Count; i++)
lvOnlineAdapter.EditItem(i, "Test " + i + " Description")
lvOnlineAdapter.NotifyDataSetChanged();
}
public class CustomAdapter : BaseAdapter<string>
{
List<string> items;
Activity context;
public CustomAdapter(Activity context, List<string> items) : base()
{
this.context = context;
this.items = items;
}
public void Add(string itemToAdd)
{
items.Add (itemToAdd);
}
public void Remove(string itemToRemove)
{
items.Remove (itemToRemove);
}
public void Remove(int position)
{
items.RemoveAt(position);
}
public override long GetItemId(int position)
{
return position;
}
public void EditItem(int Position, string Text2)
{ //Here is my problem, it does not change anything
View view = GetView (Position, null, null);
view.FindViewById<TextView>(Android.Resource.Id.Text1).Typeface = Typeface.DefaultBold;
view.FindViewById<TextView>(Android.Resource.Id.Text1).SetBackgroundColor(Color.Yellow);
view.FindViewById<TextView>(Android.Resource.Id.Text2).Text = Text2;
}
public override string this[int position]
{
get { return items[position]; }
}
public override int Count
{
get { return items.Count; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView; // re-use an existing view, if one is available
if (view == null) // otherwise create a new one
view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItem2, null);
view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = items[position];
return view;
}
}
我的问题是,当我使用 EditItem 时它不会改变任何东西,有人可以解释为什么它不起作用以及我必须做些什么才能使它起作用吗?提前致谢