如何在setText中从api作为数组传递数据

问题描述 投票:0回答:2

我正在尝试将多个数据传递给单个setText,用逗号分隔。我能够得到结果并将其显示在日志中,但我不知道如何将它们放在一起。

示例:喜剧,冒险

if (response.isSuccessful()) {
                Data data = response.body();

                for (Movie movie : data.getData()) {//iterate through all movies
                    Attributes attributes = movie.getAttributes();
                    Log.i("INFO", attributes.getName());
                    textGeners.setText(attributes.getName());
                }

            }
java android retrofit
2个回答
3
投票

使用

textGeners.append(attributes.getName()+", "); 或使用

if (response.isSuccessful()) {
            Data data = response.body();
            StringBuilder attrs = new StringBuilder();
            for (Movie Movie : data.getData()) {
                Attributes attributes = movie.getAttributes();
                Log.i("INFO", attributes.getName());
                attrs.append(attributes.getName() + ", ");
            }
             textGeners.setText(attrs.toString());
        } 

1
投票
if (response.isSuccessful()) {
                Data data = response.body();
                String s = ""; int i = 0;
                for (Movie movie : data.getData()) {//iterate through all movies
                    Attributes attributes = movie.getAttributes();
                    if( i < 1)
                    {
                       s += attributes.getName();
                    }else{
                         s+= ","+attributes.getName(); // this will ignore the last ','
                    }

                    Log.i("INFO", attributes.getName()); i++;
                }
                textGeners.setText(s);

            }
© www.soinside.com 2019 - 2024. All rights reserved.