0

I am working on a project where I have to parse two different api's and populate them into a single ListView. I have created two setter-getter classes to hold the parsed data.

  1. CategoryDetails
  2. VideoItem

Both classes has different different variables. Now here what I am trying to merge them.

 private ArrayList<CategoryDetails> categoryDetailsArrayList = new ArrayList<>();
 private ArrayList<VideoItem> mVideoItems = new ArrayList<>();

 /**
  *  API parsing and other coding stuff
  **/

 categoryDetailsArrayList.addAll(mVideoItems);
 adapter = new Adapter(this, categoryDetailsArrayList)

When I am trying to merge them, I am getting the error:

addall(java.util.collection extends java.lang.string) in arraylist cannot be applied to (java.util.arrayList)

duncan
  • 31,401
  • 13
  • 78
  • 99
Devraj
  • 1,479
  • 1
  • 22
  • 42
  • Why don't you create a model that include both object's parameters? – Ricardo Sep 09 '16 at 10:29
  • because both arraylist has different types. – George Thomas Sep 09 '16 at 10:31
  • @RicardoBarroca, yes I thought about it too but I am not sure as I want to show results randomly in list. I mean I want to show a video feed after 2 or 3 normal post. – Devraj Sep 09 '16 at 10:38
  • But every instance of object might be one, or another. Will be random unless you verify which kind of object it is and rearrange your array after you populate it – Ricardo Sep 09 '16 at 10:56
  • @RicardoBarroca is it good if I pass both arraylist in same adapter? like this searchRecipeListAdapter = new RecommendationRecipeListAdapter(this, categoryDetailsArrayList, mVideoItems) – Devraj Sep 09 '16 at 11:19
  • @RicardoBarroca so u want me to create two object for same model class like this private ArrayList categoryDetailsArrayList = new ArrayList<>(); and private ArrayList categoryDetailsArrayList2 = new ArrayList<>(); – Devraj Sep 09 '16 at 11:44
  • 1
    Lets say you have a model called categoryVideoItems that contains all the attributes from categoryDetails and VideoItems. Each time you create an object from that model it will either be a categoryDetail or VideoItem depending on the fields that are filled. From there you only need one ArrayList to set to your adapter – Ricardo Sep 09 '16 at 12:41

1 Answers1

0

As far as I can see views for different models should be different, right?

I would do the following.

1) created interface for ListViewItem:

public interface MyListViewItem {}

2) implemented this interface in CategoryDetails and VideoItem, i.e.

public class CategoryDetails implements MyListViewItem {...}

public class VideoItem implements MyListViewItem {...}

3) created ArrayAdapter<MyListViewItem>

4) passed ArrayList<MyListViewItem> which contains ALL items of all types

5) followed instruction how to create and use Adapter with multiple types of models. Like it is shown here

Inside Adapter code, when you would like to access some information from model, you will need to check class name and then cast to corresponding type, i.e.:

MyListViewItem item = getItem(position);
if (item instanceof CategoryDetails) {
    CategoryDetails details = (CategoryDetails) item;
    // do stuff
} else if (item instanceof VideoItem) {... //you got idea }

Hope it helps

Community
  • 1
  • 1
krossovochkin
  • 12,030
  • 7
  • 31
  • 54
  • what is this interface for? what I know that we use interface when we want to callback or return something to class. – Devraj Sep 09 '16 at 10:49
  • I totally understood the adapter class but that interface part. – Devraj Sep 09 '16 at 10:50
  • Into Adapter you can pass normally one list (ArrayList). ArrayList can contain items of one type only. So it can't contain simultaneously CategoryDetails and VideoItem, unless these items implement the same interface (or extend the same class) – krossovochkin Sep 09 '16 at 10:53