如何更改Android Studio中onOptionsItemSelected内的List <string>值?

时间:2016-01-08 21:36:11

标签: java android android-menu

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的值。

R.id.jaren_*de

这些菜单项我必须单击才能将屏幕上的按钮更改为所需的文本(klassen *数组)。这些按钮都是使用需要

的适配器完成的
 List<String> klassenList;

然而,这并不起作用。它没有给出任何错误(当我没有为klassenList分配值时出现空异常错误,但我知道为什么会发生这种情况)。它只是没有改变按钮的文字,我找不到任何解决方案。

提前致谢!

2 个答案:

答案 0 :(得分:0)

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

这应该有效:

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

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

答案 1 :(得分:-1)

您是否已通知列表视图数据已更改?尝试

function sendNotification() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var cell = ss.getActiveCell().getA1Notation();
  var row = sheet.getActiveRange().getRow();
  var column = sheet.getActiveRange().getColumn();
  var colLetter = columnToLetter(column);
  var cellvalue = ss.getActiveCell().getValue().toString();
  var recipients = "email@domain.org";
  var message = '';
  if(cell.indexOf('G')!=-1){ 
    message = sheet.getRange('F'+ sheet.getActiveCell().getRowIndex()).getValue()
  }
  var subject = 'Update to Notification TEST Sheet';
  var body = 'Sheet ' +sheet.getName() + ' has been updated.   Visit ' + ss.getUrl() + '  View the changes in row ' + row + ', column ' +colLetter+ '.';
  MailApp.sendEmail(recipients, subject, body);
};
function columnToLetter(column) {
  var temp, letter = '';
  while (column > 0)
  {
    temp = (column - 1) % 26;
    letter = String.fromCharCode(temp + 65) + letter;
    column = (column - temp - 1) / 26;
  }
  return letter;
}

function letterToColumn(letter)
{
  var column = 0, length = letter.length;
  for (var i = 0; i < length; i++)
  {
    column += (letter.charCodeAt(i) - 64) * Math.pow(26, length - i - 1);
  }
  return column;
};