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