0
String[] klassen = {"3ECa1", "3ECa2", "3ECb", "3WEa", "3WEb", "3STWa", "3STWb", "3STWc"};
String[] klassen2 = {"4ECa", "4ECb", "4WE", "4STWa", "4STWb", "4STWc", "1Okan", "2Okan"};
String[] klassen3 = {"5EM", "5EW", "5MW", "5WW", "5STWa", "5STWb", "5STWc", "1Okan"};
String[] klassen4 = {"6EM", "6EW", "6MW", "6WW", "6STWa", "6STWb", "6STWc", "2Okan"};

List<String> klassenList = Arrays.asList(klassen);

public boolean onOptionsItemSelected(MenuItem item){
    int id = item.getItemId();

    switch(id){
        case R.id.jaren_3de:
            klassenList = Arrays.asList(klassen);
            break;
        case R.id.jaren_4de:
            klassenList = Arrays.asList(klassen2);
            break;
        case R.id.jaren_5de:
            klassenList = Arrays.asList(klassen3);
            break;
        case R.id.jaren_6de:
            klassenList = Arrays.asList(klassen4);
            break;

        // case R.id.

    }

    return super.onOptionsItemSelected(item);
}

So I'm trying to change the value of the List klassenList inside my onOptionsItemSelected method.

R.id.jaren_*de

These are the menu items that I have to click to change the buttons on screen to the desired text (the klassen* arrays). The buttons are all done using an adapter, that needs

 List<String> klassenList;

This, however, isn't working. It isn't giving any errors (except a null exception error when I don't assign a value to klassenList, but I know why that happens). It just doesn't change the text of the buttons and I can't find any solution to it.

Thanks in advance!

appersiano
  • 2,670
  • 22
  • 42
GeeSplit
  • 53
  • 3
  • 12

2 Answers2

0

You registered the initial klassenlist instance with the adapter, but your current code is just changing the value of your local klassenlist variable. The adapter is still referencing the initial klassenlist instance, so you need to update that instance's data.

This should work:

public boolean onOptionsItemSelected(MenuItem item){
    int id = item.getItemId();

    switch(id){
        case R.id.jaren_3de:
            updateKlassenList(klassen);
            break; 
        case R.id.jaren_4de:
            updateKlassenList(klassen2);
            break; 
        case R.id.jaren_5de:
            updateKlassenList(klassen3);
            break; 
        case R.id.jaren_6de:
            updateKlassenList(klassen4);
            break; 

        // case R.id. 

    } 

    return super.onOptionsItemSelected(item);
}

private void updateKlassenList(String[] data) {
    klassenList.clear();
    klassenList.addAll(Arrays.asList(data));

    // I assume "adapter" is the adapter of interest.
    adapter.notifyDataSetChanged();
}

NOTE: This code is not optimal, as it creates a new List object every time (via Arrays.asList()). But it should give you an idea of how to fix the problem.

cybersam
  • 63,203
  • 6
  • 53
  • 76
  • I'm getting a "java.lang.UnsupportedOperationException" error now. I've looked it up and it has something to do with versions, but I have the latest so that couldn't be a problem. – GeeSplit Jan 09 '16 at 10:49
  • Never mind, I figured it out thanks to this thread: http://stackoverflow.com/questions/2965747/why-i-get-unsupportedoperationexception-when-trying-to-remove-from-the-list I used a LinkedList instead of an ordinary List now, and it works! – GeeSplit Jan 10 '16 at 17:18
-1

Have you notified your listview that data changed? Try

listViewAdapater.notifyDataSetChanged();
appersiano
  • 2,670
  • 22
  • 42