如何在TextView中显示ArrayList字符串

时间:2015-02-27 21:51:43

标签: java android arraylist

我需要在TextView中显示当前值(删除String后)。当按钮打开时我添加字符串,我需要删除它,当它关闭时(我不知道我是否做得好),接下来我需要在TextView中显示字符串而不删除串。我只需要在On按钮上显示字符串。我不知道如何在TextView中显示ArrayList - 此代码为display.setText(mActiveToggles.toString());不起作用。这是我的代码:

public class Calc extends ActionBarActivity {

TextView display;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calculator);

    display = (TextView)findViewById(R.id.textView2);
    display.setText("Add item");
}

static boolean isempty=true;

public void changeButton(View sender) {
ToggleButton btn = (ToggleButton) sender;

ArrayList<String> mActiveToggles = new ArrayList<String>();

String b = btn.getText().toString();

boolean on = ((ToggleButton) sender).isChecked();

if(on) {
    if (isempty) {
        if (b.equals("0")) return;
        display.setText(btn.getText());
        mActiveToggles.add(b);
        isempty = false;
    } else {
        display.append(btn.getText());
        mActiveToggles.add(b);
    }
}
else
{
    if (b.equals(btn.getText()))
    {
        mActiveToggles.remove(b);
        display.setText(mActiveToggles.toString());

    }
}

}

1 个答案:

答案 0 :(得分:0)

使用循环。迭代ArrayList中的每个元素以将其添加到String(或者如果您愿意,可以使用StringBuilder)。它看起来像下面这样:

String activeToggles = "";

for (String s : mActiveToggles) {
    activeToggles += s + " ";
}

display.setText(activeToggles);