简体   繁体   English

如何将视图从 Stack 添加到 LinearLayout?

[英]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

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 java.lang.ClassCastException: java.util.AbstractList$SimpleListIterator 不能转换为 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.注意我已经删除了 TextView 并将迭代器作为通用迭代器。

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

Changed to变成

Iterator iterator = rod1_stack.iterator();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以相反的顺序将子视图添加到 LinearLayout? - How to add child views to a LinearLayout in reverse order? Android如何从代码向另一个LinearLayout添加Linearlayout - Android how to add a Linearlayout to another LinearLayout from code 如何在另一个LinearLayout之后添加LinearLayout? - How to add a LinearLayout after another LinearLayout? 如何在另一个Linearlayout中创建带有特定视图的Linearlayout作为项目(任务)? - How to create a Linearlayout with specific views as an item (Task) inside another Linearlayout? 如何在LinearLayout中消除视图之间的空间 - How to get rid of space between Views in LinearLayout 如何从listview中的linearlayout中的动态添加视图中获取多个edittext值 - How do i get multiple edittext values from dynamically added views in a linearlayout inside listview 如何在 Android 中将 TextView 添加到 LinearLayout - How to add a TextView to LinearLayout in Android 如何动态添加视图到LinearLayout? - How to add a View Dynamically to a LinearLayout? 每次单击按钮将视图添加到Fragment的LinearLayout中 - Add Views to LinearLayout of Fragment everytime I click button 如何使用ImageView创建LinearLayout并将其以编程方式添加到另一个LinearLayout - How to create LinearLayout with ImageView and add it to another LinearLayout programatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM