0

I have a EditText box so a user can input names and then click the Add button and the name saves to the array playerList and clears the box so another name can be added.

I also have a ListView on the same Activity which will then be populated by the names in the Array playerList. The problem is that the ListView dosen't seem to be populated.

So I tried with a defualt set String teststring which you can see below and that populates the ListView fine. My question is how come it isn't working with the Array playerList maybe its not saving to the Array correctly?

Update just need adapter.notifyDataSetChanged(); adding to refresh the ListView Credit to @Lalit Poptani and @Jave

ArrayList<String> playerList = new ArrayList<String>();
ListView listview;
protected String[] teststring = {"Name 1", "Name 2"};

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addremove);
ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, teststring);
ListView employeeList = (ListView) findViewById(R.id.namelistview);
employeeList.setAdapter(adapter);


Button confirm = (Button) findViewById(R.id.add);
confirm.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText playername = (EditText) findViewById(R.id.userinput);
String name = playername.getText().toString();
playerList.add(name);
playername.setText("");

}});
1
  • have you tried printing the array values i mean playerlist values..???? Is array populating fine..???? if yes then you must call notifyDataSetChnaged() whenever is adapter updated. Commented Jan 12, 2012 at 10:34

2 Answers 2

3

To refresh the ListView after you add new data to it you have to call adapter.notifyDataSetChanged(). This is refresh your ListView will new data.

But in your case you are populating your ListView with String[] so it won't work dynamically you will have to give the size of the String[]. So, I will suggest you to populate your ListView with ArrayList itself for adding the content dynamically.

ListAdapter adapter = new ArrayAdapter<String>
                       (this, android.R.layout.simple_list_item_1,playerList);
Sign up to request clarification or add additional context in comments.

8 Comments

Heh, answering 4 secs before me :p
@Lalit Where abouts should that be called?
Matt in your button when you add data in your arrayList just after that call adapter.notifyDataSetChanged()
@LalitPoptani Ahh ok so then I would have to convert my ListAdapter to final ListAdapter adapter
You can but I will say to declare ListAdapter adapter; globally above onCreate.
|
2

You should call adapter.notifyDataSetChanged() after adding new items to make it update with the new data.

Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

6 Comments

Where abouts should it be called?
Just after you have updated the list. It basically tells it that it has new items in it.
so it would be after playerList.add(name);
Cool, but getting the error The method notifyDataSetChanged() is undefined for the type ListAdapter :/
Ah, you can(should) define it as an ArrayAdapter instead of a ListAdapter: ArrayAdapter<String> adapter = new ArrayAdapter(args);
|

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.