如何更改List< String> Android Studio中onOptionsItemSelected内的值? [英] How to change List<String> value inside onOptionsItemSelected in Android Studio?

查看:111
本文介绍了如何更改List< String> Android Studio中onOptionsItemSelected内的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);
}

所以我试图在onOptionsItemSelected方法内更改List klassenList的值.

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

R.id.jaren_*de

这些是我必须单击以将屏幕上的按钮更改为所需文本(klassen *数组)的菜单项.这些按钮全部使用需要的适配器完成

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;

但是,这不起作用.它没有给出任何错误(当我不给klassenList赋值时会出现null异常错误,但是我知道为什么会发生).它只是不会更改按钮的文本,而且我找不到任何解决方案.

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.

提前谢谢!

推荐答案

您已使用适配器注册了初始klassenlist实例,但是当前代码只是更改了本地klassenlist变量的值.适配器仍在引用初始klassenlist实例,因此您需要更新 实例的数据.

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.

这应该有效:

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();
}

注意:此代码不是最佳代码,因为它每次都会(通过Arrays.asList())创建一个新的List对象.但这应该使您了解如何解决该问题.

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.

这篇关于如何更改List&lt; String&gt; Android Studio中onOptionsItemSelected内的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆