更改单个ListView项

时间:2013-05-24 09:05:04

标签: c# android listview xamarin

我已经坚持在已填充的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时它不会改变任何东西,有人可以解释为什么它不起作用以及我要做些什么来使它工作? 提前致谢

1 个答案:

答案 0 :(得分:1)

    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();
}