简体   繁体   English

传安 ArrayList<customobject> 到接受 ArrayList 作为参数的 function<object> 在 Java<div id="text_translate"><p> 我正在编写一个通用的 java-android function,它将接受ArrayList<Object>作为其参数之一,这样我就可以在我的整个应用程序中使用,而不管 ArrayList 元素的类型如何。</p><p> 这是我的 function:</p><pre> public GenericDisplayAdapter(Activity activity, ArrayList<Object> arrData) { this.m_ArrData = arrData; inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); }</pre><p> 当我尝试将我的ArrayList<CustomObject>作为参数传递给此 function 时,出现错误“无法从ArrayList<CustomObject>转换为ArrayList<Object> ”,</p><pre> m_LVStructDamageHeader.setAdapter(new GenericDisplayAdapter( (Activity)this, (ArrayList<Object>)arrStructDamageHeaders));</pre><p> 处理这种情况的最佳方法是什么,谢谢</p></div></object></customobject>

[英]Pass An ArrayList<CustomObject> to a function that accepts as parameter an ArrayList<Object> in Java

I am writing a generic java-android function that will accept as one of it's parameters an ArrayList<Object> , so that I can use all over my application regardless of the type of the ArrayList elements.我正在编写一个通用的 java-android function,它将接受ArrayList<Object>作为其参数之一,这样我就可以在我的整个应用程序中使用,而不管 ArrayList 元素的类型如何。

This is my function:这是我的 function:

public GenericDisplayAdapter(Activity activity, ArrayList<Object> arrData) {

    this.m_ArrData = arrData;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

When I try to to pass my ArrayList<CustomObject> as a parameter to this function, I get the error "can't cast from ArrayList<CustomObject> to ArrayList<Object> ",当我尝试将我的ArrayList<CustomObject>作为参数传递给此 function 时,出现错误“无法从ArrayList<CustomObject>转换为ArrayList<Object> ”,

        m_LVStructDamageHeader.setAdapter(new GenericDisplayAdapter(
            (Activity)this, (ArrayList<Object>)arrStructDamageHeaders));

What is the best approach to handle such a situation, Thanks处理这种情况的最佳方法是什么,谢谢

Change your function from将您的 function 从

public GenericDisplayAdapter(Activity activity, ArrayList<Object> arrData)

to

public GenericDisplayAdapter(Activity activity, ArrayList<?> arrData)

Then you will be able to pass ArrayList<T> for any T .然后您将能够为任何T传递ArrayList<T> ArrayList<?> is almost like ArrayList<Object> , but the difference is that while you can add ANY object into the ArrayList<Object> (which is pretty bad if you pass eg ArrayList<CustomObject> there), you cannot add anything into the ArrayList<?> (which is fine). ArrayList<?>几乎类似于ArrayList<Object> ,但不同之处在于,虽然您可以将任何object 添加到ArrayList<Object>中(如果您将ArrayList<CustomObject>传递到那里,这是非常糟糕的),但您不能将任何内容添加到ArrayList<?> (很好)。

change the method parameter更改方法参数

ArrayList<Object> to ArrayList<? extends Object>

Consider also, that it may be appropriate to give the GenericDisplayAdapter class a type parameter.还要考虑,为 GenericDisplayAdapter class 提供一个类型参数可能是合适的。 Eg例如

class GenericDisplayAdapter<T> {
    private List<T> m_ArrData;

    public GenericDisplayAdapter(Activity activity, ArrayList<T> arrData) {
        ...
    }
}

So then other methods can take advantage of this type parameter, instead of dealing in Object, they can use T.那么其他方法可以利用这个类型参数,而不是处理 Object,他们可以使用 T。

You should you generic ArrayList just like你应该通用 ArrayList 就像

public GenericDisplayAdapter(Activity activity, ArrayList<?> arrData) {

    this.m_ArrData = arrData;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM