ArrayList仅显示最后一个元素

时间:2017-06-12 17:17:09

标签: java android arraylist

我有要在TextView中显示的整数列表(使用RecyclerView).Problem是每次我的整数被最后一个元素覆盖时。我在ViewHolder的bind方法中调用此方法。 是否可以使用setText而不是append并将每个整数写入textview?

    public void showRankings(){
        Integer[] rankingsArray = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
        List<Integer> rankings = Arrays.asList(rankingsArray);


        for (Integer i : rankings){
          rankingsTxt.setText("#" + Integer.toString(i));
        }
   }

Full ViewHolder:

public class TvHolder extends ButterKnifeVH  {
  @Bind(R.id.trade_btn) AutoResizeTextView tradeBtn;
  @Bind(R.id.gain_lose) public AutoResizeTextView gainTxt;
  @Bind(R.id.symbolName) public AutoResizeTextView symbolName;
  @Bind(R.id.parent) ConstraintLayout constraintLayout;
  @Bind(R.id.ranking) AutoResizeTextView rankingsTxt;

  @BindString(R.string.percent_string) String strPercent;

  private FragmentActivity fragmentActivity;
  private TopsFlopsView topsFlopsView;
  private TopsFlopsSymbolEntity topsEntity;


  public TvHolder(ViewGroup parent, int layoutId
      ,FragmentActivity fragmentActivity,TopsFlopsView topsFlopsView) {
    super(parent, layoutId);
    this.topsFlopsView = topsFlopsView;
    this.fragmentActivity = fragmentActivity;
  }
  public void bind(TopsFlopsSymbolEntity topsEntity){
    symbolName.setText(topsEntity.getSymbol());
    showRankings();
  }


        public void showRankings(){
            Integer[] rankingsArray = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
            List<Integer> rankings = Arrays.asList(rankingsArray);


            for (Integer i : rankings){
              rankingsTxt.setText("#" + Integer.toString(i));
            }
       }

  }


}

这是适配器的主要部分,工作正常

public class TopsAdapter
    extends RealmRecyclerViewAdapter<TopsFlopsSymbolEntity, RecyclerView.ViewHolder> {

  private FragmentActivity context;
  private TopsFlopsView view;

  public TopsAdapter(Realm realm) {
    super(realm);
  }

  @Override
  public void onBindRealmViewHolder(RecyclerView.ViewHolder holder, TopsFlopsSymbolEntity tops) {
    if(holder instanceof TopsViewHolder){{
      ((TopsViewHolder) holder).bind(tops);
    }}
  }

  @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new TopsViewHolder(parent, R.layout.tops_flops_item, context, view);
  }

3 个答案:

答案 0 :(得分:0)

尝试以下代码段 -

...
...
String textToDisplay = "";
for (Integer i : rankings){
   textToDisplay = textToDisplay + " #" + Integer.toString(i);
}

rankingsTxt.setText(textToDisplay);
... 

答案 1 :(得分:0)

您必须创建一个要显示到视图中的字符串或从rankingsTxt获取前一个字符串,并使用上一个值rankingsTxt.getTest()+ new value附加新值,然后再次设置它。

答案 2 :(得分:0)

这可能会对你有帮助。

public void showRankings() {
    Integer[] rankingsArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
    List<Integer> rankings = Arrays.asList(rankingsArray);

    String str = "";
    for (Integer i : rankings) {
        str = str + "#" + Integer.toString(i);
    }
    rankingsTxt.setText(str);
}