-1

I've cut all the fat out of my problem. Could anyone tell me why the list isn't updating with the new values? I've tried all the refresh commands, perhaps there's something i'm missing?

public class DropboxFragment extends ListFragment {

String[] values;
ArrayAdapter<String> adapter;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    values = new String[] { "Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2" };

   adapter = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1, values);

    setListAdapter(adapter);

}


@Override
public void onListItemClick(ListView l, View v, int position, long id) {

    values =  new String[] { "IT", "WORKED"};
    l.invalidateViews();
    adapter.notifyDataSetChanged();
    l.refreshDrawableState();
}

}
Ben
  • 1,061
  • 10
  • 23

4 Answers4

3

Instead of notifyDataChanged try to initialize ArrayAdapter with the new values

Lamorak
  • 10,957
  • 9
  • 43
  • 57
  • Thanks man, it worked! Is that the way it's supposed to be done then? – Ben Feb 26 '15 at 13:50
  • 2
    @Ben: Well, you could update the existing adapter with new data. That's "the way it's supposed to be done". Right now, your code is not changing anything inside the adapter. If you do not want to update the existing adapter instance, you have to replace the existing adapter instance. – CommonsWare Feb 26 '15 at 13:51
  • Cool. I tried values = new String[] { "IT", "WORKED"}; adapter.clear(); adapter.addAll(values); but got a java.lang.UnsupportedOperationException Do I have to add them all individually? – Ben Feb 26 '15 at 13:57
  • 1
    I found why do you get `UnsupportedOperationException`. Check [this](http://stackoverflow.com/a/3200631/4584675) answer. According to [documentation](https://developer.android.com/reference/android/widget/ArrayAdapter.html#addAll%28java.util.Collection%3C?%20extends%20T%3E%29) the method `addAll` doesn't work with arrays natively. You should pass an `ArrayList` – Lamorak Feb 26 '15 at 20:28
1

Well... You're not passing the new strings to the adapter, you're just creating a new array.

Remember Java passes the reference value, and not the reference itself, so the values in the adapter is not the same reference as the one in your activity, though it points to the same object. When you change the reference in your activity pointing to the new object you instantiate the adapter does not know this.

Try this instead:

values =  new String[] { "IT", "WORKED"};
adapter = new ArrayAdapter<String>(getActivity(),
        android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
0

Chaning values in onListItemClick does not change the list adapter. You need to take the given view find the desired textview and change its text accordingly.

public void onListItemClick(ListView l, View v, int position, long id) {
   TextView tv = (TextView)v; // assuming that the only view created with the ArrayAdapter is a text view 
   tv.setText("new text");
}
giorashc
  • 13,691
  • 3
  • 35
  • 71
0

you must add that: notifyDataSetChanged()

setListAdapter(adapter);
adapter.notifyDataSetChanged();
altan yuksel
  • 173
  • 1
  • 6
  • OP already called that method. The issue is with the list being maintained outside of the adapter. – 2Dee Feb 26 '15 at 16:01