C# 更改单个ListView项
几天来,我一直在修改一个填充的ListView中的一行,并尝试了很多(java)解决方案,但作为一名学生,我经常看到一些解决方案没有适当的文档说明它是如何工作的以及为什么工作的。到目前为止,我已经为stock ListView创建了此自定义适配器:C# 更改单个ListView项,c#,android,listview,xamarin,C#,Android,Listview,Xamarin,几天来,我一直在修改一个填充的ListView中的一行,并尝试了很多(java)解决方案,但作为一名学生,我经常看到一些解决方案没有适当的文档说明它是如何工作的以及为什么工作的。到目前为止,我已经为stock ListView创建了此自定义适配器: //How I initialize the Adapter and ListView protected override void OnCreate(Bundle bundle) { bas
//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;
}
}
//如何初始化适配器和ListView
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
联机列表=新列表();
CustomAdapter lvOnlineAdapter=新的CustomAdapter(此,联机);
ListView lvOnline=findviewbyd(Resource.Id.lvOnline);
Adapter=lvOnlineAdapter;
lvOnlineAdapter.Add(“Test1”);
lvOnlineAdapter.Add(“Test2”);
添加(“Test3”);
for(int i=0;i
我的问题是,当我使用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();
}
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
lvOnlineAdapter=新的CustomAdapter(此,联机);
lvOnline=findviewbyd(Resource.Id.lvOnline);
Adapter=lvOnlineAdapter;
lvOnlineAdapter.Add(“Test1”);
lvOnlineAdapter.Add(“Test2”);
添加(“Test3”);
lvOnlineAdapter.EditItem(2,“测试”);
对于(int i=0;i
受保护的重写void OnCreate(捆绑包)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
lvOnlineAdapter=新的CustomAdapter(此,联机);
lvOnline=findviewbyd(Resource.Id.lvOnline);
Adapter=lvOnlineAdapter;
lvOnlineAdapter.Add(“Test1”);
lvOnlineAdapter.Add(“Test2”);
添加(“Test3”);
lvOnlineAdapter.EditItem(2,“测试”);
对于(int i=0;i
对不起,我应该说我已经对它进行了初始化,并添加了一个列表,编辑了问题。我希望我说得足够清楚,让您能够理解,谢谢您的帮助,但我不想在项目单击时更改它,例如,当设备从列表视图中的用户处收到通知时。因此,用户将看到通知来自哪个用户。现在的问题是,一旦将文本添加到ListView中,我就无法更改它,或者我没有理解您的观点吗?现在我明白了,您需要将NotifyDataSetChanged()放入;在adapters add方法中,我认为您不理解我的问题,我无法使用SetNotification(int位置,string Text2)更改一行。不添加和删除项目,因为这样做有效。对不起,我应该说我已经初始化了它并添加了一个列表,编辑了问题。我希望我说得足够清楚,让您能够理解,感谢您的帮助,但我不想在项目单击时更改它,例如,当设备收到列表视图中用户的通知时。因此,用户将看到通知来自哪个用户。现在的问题是,一旦将文本添加到ListView中,我就无法更改它,或者我没有理解您的观点吗?现在我明白了,您需要将NotifyDataSetChanged()放入;我认为你不理解我的问题,我