0

so far i have this:

btnAddContact.Click += delegate {
            String[] nameList = new string[]{nameText.Text, ipText.Text};

            var myAdapter = new ArrayAdapter(this, Resource.Layout.TextViewItem, nameList);
            contactView.Adapter = myAdapter;
            myAdapter.NotifyDataSetChanged();
            nameText.Text = "";
            };

Two EditText fields, a button and a ListView - when i add data into the two text fields and click the button it adds the data to an array and then displays it in the ListView.. except if i try to add another value to the array via the text fields, it just changes what was already entered..

how do i do this so it keeps adding the data forming a list?

1 Answer 1

1

Figured it out on my own.. This is something other people told me could only be achieved through a custom adapter so it may be useful to someone else.

Created a public array

public ArrayList nameList = new ArrayList ();

then on a button click, assigned the add to array code.

btnAddContact.Click += delegate {
            nameList.Add(nameText.Text + " " + ipText.Text);
            var myAdapter = new ArrayAdapter(this, Resource.Layout.TextViewItem, nameList);
            contactView.Adapter = myAdapter;
            myAdapter.NotifyDataSetChanged();
            nameText.Text = "";
            ipText.Text = "";
            };

This puts the name and ip address on the same line with a space in between so it's easily readable.

Sign up to request clarification or add additional context in comments.

2 Comments

Pull out line 3 and 4 out of your Click handler and put them in OnCreate. No reason to override the Adapter all the time.
In fact it won't work if not overriding the adapter. Instead it will crash, because ArrayAdapter has probably a bug.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.