Android将按钮编号放入字符串列表中的文本列表中



我有一些按钮,它们的文本中包含一些随机字符。我需要从ArrayList中排除包含字符的按钮。

或者,更好的解释是,我的ArrayList中有字符a, s, d, a,并且我需要文本中有asda的按钮的编号。

我的代码在下面。它怎么了?

编辑:预期输出为buttonNumbers[],包含多个按钮。目前,此数组的每个值==1。

public void adsf() {
    ArrayList<String> characters = new ArrayList<String>();
    characters.addAll(spreadToCharacters(getWord(getActualNumber())));
    Button [] but = new Button[26];
    but[0] = (Button) findViewById(R.id.b1);
    but[1] = (Button) findViewById(R.id.b2);
    but[2] = (Button) findViewById(R.id.b3);
    but[3] = (Button) findViewById(R.id.b4);
    but[4] = (Button) findViewById(R.id.b5);
    but[5] = (Button) findViewById(R.id.b6);
    but[6] = (Button) findViewById(R.id.b7);
    but[7] = (Button) findViewById(R.id.b8);
    but[8] = (Button) findViewById(R.id.b9);
    but[9] = (Button) findViewById(R.id.b10);
    but[10] = (Button) findViewById(R.id.b11);
    but[11] = (Button) findViewById(R.id.b12);
    but[12] = (Button) findViewById(R.id.b13);
    but[13] = (Button) findViewById(R.id.b14);
    but[14] = (Button) findViewById(R.id.b15);
    but[15] = (Button) findViewById(R.id.b16);
    but[16] = (Button) findViewById(R.id.b17);
    but[17] = (Button) findViewById(R.id.b18);
    but[18] = (Button) findViewById(R.id.b19);
    but[19] = (Button) findViewById(R.id.b20);
    but[20] = (Button) findViewById(R.id.b21);
    but[21] = (Button) findViewById(R.id.b22);
    but[22] = (Button) findViewById(R.id.b23);
    but[23] = (Button) findViewById(R.id.b24);
    but[24] = (Button) findViewById(R.id.b25);
    but[25] = (Button) findViewById(R.id.b26);
    int[] buttonNumbers = new int[characters.size()];
    int j = 0;
    for(int i = 0; i<but.length; i++) {
        for(int o = 0; o < characters.size(); o++) {
            if(but[i].getText().equals(characters.get(o))) {
                for(int z = 0; z < buttonNumbers.length; z++) {
                    if(i != buttonNumbers[z]) {
                        buttonNumbers[j] = i;
                        j++;
                    }
                }
            }
        }
    }
}

u尝试contains方法:

int[] buttonNumbers = new int[characters.size()];
int j = 0;
for (int i = 0; i < but.length; i++){
    for (String s : characters){
        if (but.getText().contains(s)){
            buttonNumbers[j] = i;
            j++;
        }
    }
}

但如果你想检查重复项,我会让buttonNumbers是List:

    List<Integer> buttonNumbers = new ArrayList<Integer>();
    for (int i = 0; i < but.length; i++){
        for (String s : characters){
            if (but.getText().contains(s) && !buttonNumbers.contains(i)){
                buttonNumbers.add(i);
            }
        }
    }

最新更新