简体   繁体   English

无法在方法括号内放置新的列表

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

相关问题 不能将值放入数组列表中 - cannot put values inside Array List 一种计算括号数量而不包括括号内内容的方法 - A method to count the number of brackets without including what's inside the brackets 我需要将正确的字符串放入“new FileReader(&quot;stringpath&quot;)”中的括号中 - I need the right String to put in the brackets in "new FileReader("stringpath")" 将括号放在main方法中的位置是否重要? - Does it matter where you put the brackets in the main method? 如何退货清单 <Long> java中方法的值并将其放在新列表中? - How to return List<Long> value from method in java and put it in new list? 当按下按钮然后将其放入dragHandler中时,是否有任何方法可以创建不同子类的新对象? - Is there any method to create a new object of different sub-class when a button is pressed and then put it inside dragHandler? 用大括号澄清一种方法中代码的不同部分 - Clarification on separate sections of code inside one method with curly brackets 多个打开和关闭大括号内的方法。 - 爪哇 - Multiple open and close curly brackets inside method. - Java 为什么可以在泛型括号内添加问号而不是方法参数? - why it is fine to add question mark inside a generics brackets but not for method parameter? 在一个方法中用大括号分隔代码段是不好的做法吗? - Is it bad practice to separate sections of code inside one method with curly brackets?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM