gpt4 book ai didi

c# - 更改单个 ListView 项目

转载 作者:行者123 更新时间:2023-11-30 03:35:08 26 4
gpt4 key购买 nike

几天来,我一直坚持更改填充的 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 时它没有任何改变,有人可以解释为什么它不起作用以及我必须做些什么才能让它起作用吗?提前致谢

最佳答案

    protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

SetContentView(Resource.Layout.Main);

lvOnlineAdapter = new CustomAdapter(this, online);
lvOnline = FindViewById<ListView>(Resource.Id.lvOnline);
lvOnline.Adapter = lvOnlineAdapter;

lvOnlineAdapter.Add("Test1");
lvOnlineAdapter.Add("Test2");
lvOnlineAdapter.Add("Test3");
lvOnlineAdapter.EditItem(2, "test");
for( int i = 0; i < myLayout.getChildCount(); i++ )
if( myLayout.getChildAt( i ) instanceof TextView )
{
TextView tvBold = (TextView) myLayout.getChildAt( i );
if(tvBold.getText().ToString() == "test")
tvBold.SetBackgroundColor(Color.Yellow);
}
}

public void EditItem(int Position, string Text2)
{
items[Position] = Text2;
NotifyDataSetChanged();
}

关于c# - 更改单个 ListView 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16731268/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com