如何在 TextView 中显示 ArrayList 中的字符串
How to display Strings from ArrayList in TextView
我需要在 TextView 中显示当前值(在删除字符串之后)。
我在按钮打开时添加字符串,我需要将其删除,当它关闭时(我不知道我是否做得好),接下来我需要在不删除字符串的情况下在 TextView 中显示字符串。我只需要显示来自 On 按钮的字符串。
这是我的代码:
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());
}
}
}
根据新信息进行编辑:
要显示您 TextView
中当前打开的所有开关的文本,我建议您创建一个
private ArrayList<String> mActiveToggles = new ArrayList<String>();
只要将切换按钮设置为打开,您就将该切换按钮的标签添加到列表中。每当切换按钮设置为关闭时,您就会从列表中删除标签。
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());
}
要更新您的 TextView
,只需迭代列表中的项目并将它们添加到 TextView
(使用适当的分隔符)。像这样:
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());
如果您确实需要持久存储 TextView
的内容,请遍历列表项并将它们连接起来,然后将它们存储到您的 SharedPreferences
对象中。从存储中恢复它们只是拆分字符串和填充列表的反向操作。根据您提供的代码,我认为不需要这样做,但当然可以比您在此处发布的内容更多。
要从共享首选项中获取字符串,您必须使用:
if(sharedpreferences.contains(key_value)){
textview.setText(sharedpreferences.getString(key_value, null));
}
如果您遇到任何问题,请告诉我。
我需要在 TextView 中显示当前值(在删除字符串之后)。 我在按钮打开时添加字符串,我需要将其删除,当它关闭时(我不知道我是否做得好),接下来我需要在不删除字符串的情况下在 TextView 中显示字符串。我只需要显示来自 On 按钮的字符串。 这是我的代码:
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());
}
}
}
根据新信息进行编辑:
要显示您 TextView
中当前打开的所有开关的文本,我建议您创建一个
private ArrayList<String> mActiveToggles = new ArrayList<String>();
只要将切换按钮设置为打开,您就将该切换按钮的标签添加到列表中。每当切换按钮设置为关闭时,您就会从列表中删除标签。
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());
}
要更新您的 TextView
,只需迭代列表中的项目并将它们添加到 TextView
(使用适当的分隔符)。像这样:
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());
如果您确实需要持久存储 TextView
的内容,请遍历列表项并将它们连接起来,然后将它们存储到您的 SharedPreferences
对象中。从存储中恢复它们只是拆分字符串和填充列表的反向操作。根据您提供的代码,我认为不需要这样做,但当然可以比您在此处发布的内容更多。
要从共享首选项中获取字符串,您必须使用:
if(sharedpreferences.contains(key_value)){
textview.setText(sharedpreferences.getString(key_value, null));
}
如果您遇到任何问题,请告诉我。