简体   繁体   中英

Android - Can you mix types in a Custom Adapter?

For an Adapter, you would give it some type of collection of objects/types, like

List<MyObject> myList = new ArrayList<MyObject>;
myCustomAdapter adapter = new myCustomAdapter(this, myList);

What if you wanted to use multiple types of Objects in your adapter. So certain items in the adapter would be one object while other items would be different objects. How would you go about doing something like that?

I assume that you have control over the code within myCustomAdapter ? If so, you can create an interface that all of your objects will share, make your adapter generic and constrain the type parameter to that interface.

public interface IAdapterItem{
    //Whatever functionality is needed
}

public class MyCustomAdapter<T extends IAdapterItem> extends ArrayAdapter<T>{
    //Adapter implementation
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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