Android将数据写入Edittext

时间:2014-03-03 18:24:43

标签: android json arraylist tostring

当我将结果写入TextView时,它显示如下:

  

ChampionList [[[安妮],[布里茨],[仙后座],[考尔奇],[卢西恩]]]

我不知道怎么不显示数组“[]”。可以有人帮我吗?

这是代码:

import java.util.ArrayList;


public class ChampionList {

    private ArrayList<Champion> champions;

    public ArrayList<Champion> getChampions() {
        return champions;
    }

    @Override
    public String toString() {


        return "ChampionList [" + champions + "]";

    }

    public void setChampions(ArrayList<Champion> champions) {
        this.champions = champions;
    }



}

冠军级别:

package src.jriot.objects;

public class Champion {


    private String name;


    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {

        return "[" + name + "]";
    }

谢谢!

2 个答案:

答案 0 :(得分:0)

您需要覆盖toString()方法以创建自己的可呈现输出,该输出将遍历您的arraylist并将元素添加到字符串中。像这样:

String championsList = "";

for(int i = 0; i< champions.size(); i++)
{
    championsList += championsList.get(i) + ", ";
}

System.out.println(championsList);

答案 1 :(得分:0)

尝试这样的事情:

@Override
public String toString() {

   StringBuilder sb = new StringBuilder();
   for (Iterator<Champion> iterator = champions.iterator(); iterator.hasNext();) {
       Champion champion = (Champion) iterator.next();
       sb.append(champion.toString() + ",");

}
String result = sb.toString();
return result.substring(0,result.length()-1);


}

您可能还需要覆盖Champion类中的toString函数。

假设Champion类看起来像这样:

public class Champion {
    private String championName;
    //The Tostring() method can look like this
    @Override
    public String toString() {
        return championName;
    }

}