How to change List<String> value inside onOptionsItemSelected in Android Studio?

為{幸葍}努か 提交于 2020-01-07 03:53:09

问题


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!


回答1:


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.




回答2:


Have you notified your listview that data changed? Try

listViewAdapater.notifyDataSetChanged();


来源:https://stackoverflow.com/questions/34686505/how-to-change-liststring-value-inside-onoptionsitemselected-in-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!