简体   繁体   中英

ArrayList is showing only last element

I have list of Integers that I want to show inside TextView (using RecyclerView).Problem is that each time my integer is overridden by last element.I'm calling this method inside bind method of ViewHolder. Is it possible to use setText instead of append and have each integer written to 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));
            }
       }

  }


}

Here is main part of Adapter which is working fine

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);
  }

Try the following snippet -

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

rankingsTxt.setText(textToDisplay);
... 

您必须创建一个要显示在视图中的字符串,或者从rankingsTxt获取先前的字符串,然后将新值附加到先前的值(如rankingsTxt.getTest()+ new value后再进行设置。

This may help you.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM