SM Jubayer SM Jubayer - 10 months ago 54
Android Question

Android Two different type object arraylist add into single arraylist

I try to add two arraylist into single arraylist. all arraylist object type is different.

here my two arraylist-

ArrayList<NewsItem> topnewslist=new ArrayList<>();
ArrayList<LatestNewsInfo> latestnewslist=new ArrayLis();

ArrayList<AllNewsInfo> allnewslist=new ArrayList<>();// add here

// I try this
allnewslist.addAll(topnewslist); // not add bcoz differnt type of object
allnewslist.addAll(latestnewslist); //not add bcoz differnt type of object

// Note: all object data model value is same.

What is best solution for this??


ArrayList<Object> allnewslist=new ArrayList<>();.

Ideally both NewsItem and LatestNewsInfo should inherit common interface, and then you can have the list of this interface.

interface News{}  //can be an abstract class

class NewsItem implements News{

class LatestNewsInfo implements News{

//edit - define variables to interface instead of actual implementation object 
List<News> allnewslist=new ArrayList<>();