0

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??

3
  • ArrayList<Object> allnewslist=new ArrayList<>(); Commented Aug 4, 2016 at 5:02
  • thanks for your feedback. i also try this but when i display all data into listview . then face a problem to set data base of position in adapter class. Commented Aug 4, 2016 at 5:06
  • Main prob is values ovver right. allnewslist.addAll(topnewslist); allnewslist.addAll(latestnewslist); Commented Aug 4, 2016 at 5:15

1 Answer 1

6

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<>(); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.