【问题标题】:Only the last item added in ArrayList shows只有在 ArrayList 中添加的最后一项显示
【发布时间】:2016-06-19 17:47:14
【问题描述】:

我是一般编程的新手,我正在尝试将项目添加到我创建的 ArrayList 中。然后我尝试在 Android TextView 中显示 ArrayList 以确认 ArrayList 的内容。简化代码如下:

 public TextView textView1;
 public ArrayList<String> namesArray = new ArrayList<>();
 public static final String DEFAULT= "N/A";

 public void contactsArray() {
    loadArrayButton = (Button) findViewById((R.id.loadArrayButton));
    loadArrayButton.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View view) {
              SharedPreferences sharedPreferences = getSharedPreferences("MyData", Context.MODE_PRIVATE);
              int howMany = sharedPreferences.getAll().size();

              for (int i = 1; i < howMany; i ++) {
                  String name = sharedPreferences.getString("name" + i, DEFAULT);
                  namesArray.add(sharedPreferences.getString("name" + i, DEFAULT));
            }

我知道共享首选项已正确保存,因为如果我单独检索它们,它们会按照我的预期出现。当我显示 ArrayList 的内容时,仅显示添加到我的 ArrayList 的最后一项。其余代码如下:

String listRepresentation = "";
for (String name : namesArray)
if ("".equals(listRepresentation))
    listRepresentation  = name; else
    listRepresentation  = ", " + name;
textView1.setText(listRepresentation );

同样,唯一出现的是在 for 循环中添加的姓氏。任何帮助将不胜感激 - 谢谢!

【问题讨论】:

  • 只是为了获取逗号分隔的名称,您可以使用下面的代码。 String names = namesArray.toString(); textView1.setText(names.substring(1, names.length()-1));

标签: java android arraylist add


【解决方案1】:

在您的循环中显示结果,您会在每个索引处覆盖字符串。

使用 += 解决问题:

 String listRepresentation = "";
 for (String name : namesArray) {
     if ("".equals(listRepresentation)) {
          listRepresentation += name;
      } else {
          listRepresentation += ", " + name;
      }
 }
 textView1.setText(listRepresentation );

运算符 += 等于 listRepresentation = listRepresentation + ...;

【讨论】:

    【解决方案2】:

    你错过的是:

    您刚刚设置了值listRepresentation,但没有按顺序添加。

    解决办法:

    您可以使用此代码代替最后一部分代码

    String listRepresentation = "";
    String showData = "";
    for (String name : namesArray) {
        if ("".equals(listRepresentation)) {
            listRepresentation  = name;
        } else {
            listRepresentation  = ", " + name;
        }
    showData = showData + listRepresentation;
    }
    textView1.setText(showData);
    

    【讨论】:

      【解决方案3】:

      请使用 StringBuilder 代替字符串 (listRepresentation)。问题会解决的。

      【讨论】:

        【解决方案4】:

        你知道如果sharedPreferences.getAll().size();例如,您的代码中有 2 个项目放在 namesArray 中,只有 1 个项目? 试试

        for (int i = 1; i <= howMany; i ++) {
                      String name = sharedPreferences.getString("name" + i, DEFAULT);
                      namesArray.add(sharedPreferences.getString("name" + i, DEFAULT));
                }
        

        【讨论】:

          猜你喜欢
          • 2013-11-12
          • 1970-01-01
          • 1970-01-01
          • 2019-01-26
          • 1970-01-01
          • 2016-11-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多