1

I'm working on a program that uses a ListView which contains strings that a user enters. I want it to list out each string after the user clicks the button. Also I want every string value that is displayed in the ListView to also be in an array so that I can manipulate the list later. This is what I have right now:

public ArrayList<String> choices = new ArrayList<String>();
public String[] choicesArray;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final ListView listView = (ListView) findViewById(R.id.listView1);
    choicesArray = new String[] { "You have not entered a choice yet" };
    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1,
            choicesArray);
    listView.setAdapter(adapter);

    final Button addButton = (Button) findViewById(R.id.Button1);
    addButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            EditText editText = (EditText) findViewById(R.id.edit_choice);
            String message = editText.getText().toString();
            adapter.add(message);

            //choices.add(message);
            //choicesArray = choices.toArray(choicesArray);

            //listView.setAdapter(adapter);
        }
    });
}

This code creates an error:

FATAL EXCEPTION: main
java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:404)
at java.util.AbstractList.add(AbstractList.java:425)
at android.widget.ArrayAdapter.add(ArrayAdapter.java:179)
at MainActivity$1.onClick(MainActivity.java:38)
FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
Ryan Sayles
  • 3,389
  • 11
  • 56
  • 79
  • what is variable `choices` you don't seem to declare it in the code that you've posted. – FoamyGuy Mar 06 '13 at 20:11
  • Apologies, I edited it. It is a class variable – Ryan Sayles Mar 06 '13 at 20:28
  • Ok, I edited my answer with the info that you need to fix it. I am going to edit your post again to add back the tags that you removed. Please leave them there this time, using specific tags keeps this site organized, and it gives you a better chance to get answers from people, because many people "favorite" the tags that they are knowledgeable in – FoamyGuy Mar 06 '13 at 21:57
  • Thank you! I really appreciate it, I didn't even know I removed the tags, I must have done so by mistake – Ryan Sayles Mar 06 '13 at 23:23

2 Answers2

1

The reason your list is not updating is that you are not ever adding your data items to the Adapter. You are only adding them to choicesArray which is the array that you passed to the new ArrayAdapter() constructor.

That constructor does not tie the adapter to the array, but basically copies the array into the adapter (at the time that you call the constructor) Changes made to the array after that will not be reflected in the ArrayAdapter.

I like to think of it that ArrayAdapter<> is basically the same thing as ArrayList<> with the added bonus of being able to generate child views for some AdpaterView parent. You can pass it an array[] to start with and it will initialize itself with the values in that array, but it does not link the array to the to the Adapter, or to the ListView. After that initial constructor call any changes to either one will not be reflected in the other.

So you should change your code to look like this:

addButton.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        EditText editText = (EditText) findViewById(R.id.edit_choice);
        String message = editText.getText().toString();
        adapter.add(message);

        //adapter.notifyDataSetChanged(); //<-- you might need to call this, but I think it might work without if you do it this way.
        //listView.setAdapter(adapter); //<-- you shouldn't need to call this each time. Once at the begining is enough.
    }
});

Edit:

change your ArrayAdapter constructor and remove the choiceArray parameter so you should be left with this:

final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, android.R.id.text1);

after that I think you should be good to go. Essentially the problem was that since you were passing in an array, and since arrays are of a fixed size (yours was length = 1) it was not allowing you to add anything. I suspect you could probably use an ArrayList<String> instead of String[] if you wanted, but honestly for your situation I think it will be easier to just skip that parameter all together and add items directly to your adapter whenever you need to add new ones.

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
0

after adding the new String to your list you just use notifyDataSetChanged() of your adapter which tells the adapter to update the display and it'll update the ListView with the new string

addButton.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

        EditText editText = (EditText) findViewById(R.id.edit_choice);
        String message = editText.getText().toString();
        choices.add(message);
        choicesArray = choices.toArray(choicesArray);

        adapter.notifyDataSetChanged();

    }
});

Update

you should consider making this change too

choicesArray = new String[choices.size()];
choicesArray = choices.toArray(choicesArray);
ColdFire
  • 6,764
  • 6
  • 35
  • 51
  • The answer sounds right, but why the update? It looks like you're just creating a `new String[]` for no reason, since you immediately reassign it. – Geobits Mar 06 '13 at 20:37
  • I essentially want to add the string value to the existing arraylist (which has to be converted to an array) and have that arraylist be displayed in the list view. Adding the adapter.notifyDataSetChanged(); statement didn't help, and the update caused my program not to work at all – Ryan Sayles Mar 06 '13 at 20:43
  • what kind of exception did you get? – ColdFire Mar 06 '13 at 21:00
  • I didn't get an exception, it just didn't display the string in the list view – Ryan Sayles Mar 06 '13 at 21:41