0
  • My code using many array list with custom model, now i want to store all array list in one Seprate arrayList, how it is possible.

    public ArrayList<TwitterFeedCustomModel> mFacebookFeedCustomModelArrayList = new ArrayList<>();
    public ArrayList<TwitterFeedCustomModel> mTwitterFeedCustomModelArrayList = new ArrayList<TwitterFeedCustomModel>();
    public ArrayList<LinkedInFeedCustomModel> mLinkedInFeedCustomModelArrayList = new ArrayList<LinkedInFeedCustomModel>();
    public ArrayList<InstagramFeedCustomModel> mInstagramFeedCustomModelArrayList = new ArrayList<InstagramFeedCustomModel>();
    public ArrayList<YouTubeFeedCustomModel> mYouTubeFeedCustomModelArrayList = new ArrayList<YouTubeFeedCustomModel>();
    public ArrayList<BlogFeedCustomModel> mBlogFeedCustomModelArrayList = new ArrayList<BlogFeedCustomModel>();
    
  • All arraylist have seprate data, now i want to store all array list data in one arrayList, how we can do please help me.

8
  • How about using generics for creating the mother ArrayList ? Commented Oct 25, 2016 at 10:58
  • the types are different you can join them in an arraylist<fatherType> that all these types extend that fatherClass. you cant achieve that without casting. Commented Oct 25, 2016 at 10:59
  • @AmirZiarati please, can you explain with proper example. Commented Oct 25, 2016 at 11:02
  • Do you want to replace all this with a single list of use these ArrrayLists to produce a new list containing all? Commented Oct 25, 2016 at 11:05
  • the types will be casted and you wont be able to find the types easily after doing this !!!! you can use a list of objects like ArrayList<Object> and add all the lists into that. Commented Oct 25, 2016 at 11:07

2 Answers 2

1
ArrayList<Object> singleArrayList = new ArrayList<> ();
singleArrayList.add(mFacebookFeedCustomModelArrayList);
singleArrayList.add(mTwitterFeedCustomModelArrayList);
singleArrayList.add(mLinkedInFeedCustomModelArrayList);
......

while retrieving data you have to do type check.

Sign up to request clarification or add additional context in comments.

11 Comments

after above implementation, how to get all data ?
int index =0; if( singleArrayList.get(index) instanceof ArrayList<TwitterFeedCustomModel>) { mFacebookFeedCustomModelArrayList = singleArrayList.get(index) ; ........} like wise.
hi friend, your solution is correct, but i want to all arraylist data in one arraylist, on that single array list iwant to perform many operation like sorting, filter etc, so it is possible to without object, because this one is not help full for me
this one is doesn't meaning for implementation, because we check every time which object is, so same as my implementation,
ok. ArrayList<Object> singleArrayList = new ArrayList<> (); singleArrayList.addAll(mFacebookFeedCustomModelArrayList);
|
0

Create a New class Say NewT that will be super class of TwitterFeedCustomModel, LinkedInFeedCustomModel,InstagramFeedCustomModel,YouTubeFeedCustomModel,BlogFeedCustomModel and make new ArrayList of NewT(newly created class)

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.