Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Mobile Development
  3. Mobile
  4. Add new listitem in Listview dynamically

Add new listitem in Listview dynamically

Scheduled Pinned Locked Moved Mobile
csharpandroidvisual-studiohelplearning
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    klaydze
    wrote on last edited by
    #1

    Hi Guys, I populated my listview in android using ArrayAdapter. Populating my listview is easy BUT I'm having trouble adding new item on it dynamically. Kindly check my code.

    [Activity(Label = "Country List", MainLauncher = true)]
    public class MyListViewActivity : Activity
    {
    List<string> countries = new List<string>();
    ArrayAdapter<string> adapter;
    ListView lvw;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            
            SetContentView(Resource.Layout.ListviewLayout);
            InitCounties();
                                                
            lvw = FindViewById<ListView>(Resource.Id.lvwCountry);
            EditText txtMsg = FindViewById<EditText>(Resource.Id.edit\_text\_out);
            Button btnSend = FindViewById<Button>(Resource.Id.button\_send);
    
            adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, countries);
            lvw.Adapter = adapter;
            lvw.TextFilterEnabled = true;
            lvw.FastScrollEnabled = true;
    
            btnSend.Click += delegate(object sender, EventArgs e)
            {
                addItems(txtMsg.Text);
            };
        }
     }
    

    void InitCounties()
    {
    countries.Add("Philippines");
    countries.Add("USA");
    countries.Add("Hong Kong");
    countries.Add("Singapore");
    countries.Add("Russia");
    countries.Add("Afghanistan");
    countries.Add("Albania");
    countries.Add("Algeria");
    countries.Add("American Samoa");
    countries.Add("Andorra");
    }

    public void addItems(string newItem)
    {
    counties.Add(newItem);
    adapter.NotifyDataSetChanged();
    }

    Actually the newitem will add in the collection but when I break and check the adapter, the property Count does not changed and my listview is still the same. I know I'm missing somewhere in my code but I don't know where exactly it is. BTW, I'm using Visual Studio 2010 (Mono for Android) in my code. Thanks

    if(you type your code here) { Messagebox.Show("You help me a lot!"); } else { You help me = null; }

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups