简体   繁体   中英

How to add views from Stack to LinearLayout?

This is stack created by me

  public Stack<TextView> rod1_stack=new Stack<TextView>();

And I have following LinearLayout

LinearLayout Layout_rod_1=findViewById(R.id.rod1_layout);

I want to add elements from stack to this layout

This is what I'm trying

Layout_rod_1.removeAllViews();
Iterator<TextView> iterator = rod1_stack.iterator();
while (iterator.hasNext()) {
    Layout_rod_1.addView((View) iterator);
}

But I'm Getting error as

java.lang.ClassCastException: java.util.AbstractList$SimpleListIterator cannot be cast to android.view.View

Please try the below code snippet to add the views from the stack to your linear layout.

Layout_rod_1.removeAllViews();
Iterator iterator = rod1_stack.iterator();
while (iterator.hasNext()) {
    TextView tv = iterator.next();
    Layout_rod_1.addView(tv);
}

Note I have removed TextView and made iterator as generic one.

Iterator < TextView> iterator = rod1_stack.iterator();

Changed to

Iterator iterator = rod1_stack.iterator();

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