简体   繁体   中英

Change single ListView item

I'm stuck on changing a single row in a populated ListView for several days now and tried lots of (java) solutions found on here as well on xamarin but as a student I see often solutions posted without proper documentation on how and why it works. So far I have created this custom Adapter for the stock 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;
    }
}

My problem is, when I use EditItem it doesn't change anything, can someone please explain why it doesn't work and what I got to do to make it work? Thanks in advance

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM