[英]Cannot put a new List inside method brackets
Senario: 塞纳里奥:
So I am trying to simplify the usage of setting a lot of TextViews
invisible by making a lot a lines of code into one, so decided to make a method that gets List<TextView>
and turns them all invisible. 因此,我试图通过将大量代码合并为一个代码来简化将许多
TextViews
设置为不可见的用法,因此决定采用一种获取List<TextView>
并将其全部变为不可见的方法。 I made a test Class to show the error. 我做了一个测试类来显示错误。 (Problem bellow)
(问题如下)
Method: 方法:
private void makeTextViewsInvisible(List<TextView> listOfTextViews){
int i= 0;
while(i < listOfTextViews.size()){
listOfTextViews.get(i).setVisibility(View.INVISIBLE);
i++;
}
}
Using the method: 使用方法:
makeTextViewsInvisible(new List<t1,t2,t3>()) ;
//Dose'nt work?
//I don't want to define : (List<TextView> a = new ...) And add TextViews manually (a.add(TextView1)...)
//I want to add all three TextViews in one line of code inside the brackets
Problem is, that I cannot make a new List
inside the brackets, I need to make a whole new List
and add all the TextViews
manually (By using List.add();
), witch dosent simplify my code. 问题是,我无法在方括号内创建一个
new List
,我需要制作一个新List
并手动添加所有TextViews
(通过使用List.add();
),以便简化我的代码。 Any way to make a new List inside the bracket it self? 有什么办法可以在括号内自己制作一个新的List?
The syntax 语法
makeTextViewsInvisible(new List())
makeTextViewsInvisible(new List())
is incorrect. 是不正确的。 You clearly do not understand the syntax of List initialization.
您显然不了解List初始化的语法。 If you want to insert three elements to the list of a given type (in your case it'll be
List<TextViews>
I believe) you have to initialize it (the list of type TextViews
). 如果要在给定类型的列表中插入三个元素(我相信您的情况是
List<TextViews>
),则必须对其进行初始化( TextViews
类型的列表)。 Note that you probably don't want to instantiate a List, because it is an interface and you would have to implement every method of List interface, such as add(), get(), indexOf()
etc. I suggest that you pass the ArrayList
as your function argument. 请注意,您可能不想实例化List,因为它是一个接口,您将必须实现List接口的每个方法,例如
add(), get(), indexOf()
等。我建议您传递ArrayList
作为您的函数参数。 Let's say that t1,t2,t3
are already defined objects of type TextViews
. 假设
t1,t2,t3
已经是TextViews
类型的已定义对象。 To instantiate ArrayList<TextViews>
you need the following syntax: 要实例化
ArrayList<TextViews>
您需要以下语法:
ArrayList<TextViews> exampleList= new ArrayList<>(Arrays.asList(t1,t2,t3));
You have to pass an array as a constructor of a list, which you can easily do with Arrays.asList()
static method. 您必须将数组作为列表的构造函数传递,您可以使用
Arrays.asList()
静态方法轻松完成此操作。 With that said, syntax of your method invocation would look like this: 话虽如此,方法调用的语法如下所示:
makeTextViewsInvisible(new ArrayList<TextViews>(Arrays.asList(t1,t2,t3));
You could do something like that if you would use this library... 如果您使用此库,则可以执行类似的操作...
org.apache.commons org.apache.commons
Then it should be possible to call your method like this. 然后应该可以像这样调用您的方法。
List<String> newList = ListUtils.union(list1,list2);
makeTextViewsInvisible(ListUtils.union(t1,t2,t3));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.