简体   繁体   English

如何在TextView中显示ArrayList中的字符串

[英]How to display Strings from ArrayList in TextView

I need to display current values in TextView (after removing String). 我需要在TextView中显示当前值(删除String后)。 I'm adding String when button is On and I need to remove it, when it's Off ( I don't know if I do it well), next I need to display Strings in TextView without deleted String. 我在按钮打开时添加字符串,当按钮关闭时我需要将其删除(我不知道是否做得很好),接下来我需要在TextView中显示字符串而不删除字符串。 I need to display only Strings from On buttons. 我只需要显示“打开”按钮上的“字符串”。 Here is my code: 这是我的代码:

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

        }
    }
}

Edit based on new information: 根据新信息进行编辑:

To display the texts from all the toggles that are currently ON in your TextView I suggest that you create a 要显示TextView中当前处于打开状态的所有切换的文本,建议您创建一个

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

Whenever a toggle button is set to ON, you add the label of that toggle button to the list. 只要将切换按钮设置为“开”,就可以将该切换按钮的标签添加到列表中。 Whenever a toggle button is set to OFF, you remove the label from the list. 每当切换按钮设置为OFF时,就从列表中删除标签。

if(on) {
    if (b.equals("0")) return; // Don't really know why you need this one...

    // If the button is ON, add its text label to the list.
    mActiveToggles.add(btn.getText());
} else {
    // If button is OFF, remove its text label from the list.
    mActiveToggles.remove(btn.getText());
}

To update your TextView simple iterate over the items in the list and add them to the TextView (with an appropriate separator). 要更新您的TextView ,只需对列表中的项目进行迭代,然后将它们添加到TextView (使用适当的分隔符)。 Something like this: 像这样:

StringBuilder allLabels = new StringBuilder();
for(String s : mActiveToggles) {
    if(allLabels.length() > 0) {
        allLabels.append(" "); // some divider between the different texts
    }
    allLabels.append(s);
}
display.setText(allLabels.toString());

If you really do need the content of your TextView to be persistently stored, iterate over the list items and concatenate them and then store them to your SharedPreferences object. 如果确实需要持久存储TextView的内容,请遍历列表项并将其连接起来,然后将其存储到SharedPreferences对象。 Restoring them from storage would simply be the reverse operation of splitting the String and populating the list. 从存储中还原它们只是将String拆分并填充列表的相反操作。 Based on the code that you've provided I don't see a need for this, but there could of course be more to it than what you've posted here. 根据您提供的代码,我认为不需要这样做,但是当然可以比您在此处发布的内容更多。

For getting strings from shared preferences you have to use : 为了从共享首选项中获取字符串,您必须使用:

if(sharedpreferences.contains(key_value)){
textview.setText(sharedpreferences.getString(key_value, null));
}

If there any issue you get please let me know. 如果您有任何问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM