1

Im trying to create recyclerview that displays the results of two different arrays from one single object (ObjectMaster). I dont care the order so much, just as long as both arrays are displayed in the list. Any ideas or suggestions would be extremely appreciated.

Model

public ObjectMaster {
  List<Object1> first_objects;
  List<Object2> second_objects;
}

Adapter

public class ObjectMasterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

 private ObjectMaster object;
 private static final int TYPE_OBJECT1 = 0;
 private static final int TYPE_OBJECT2 = 1;

 public ObjectMasterAdapter(ObjectMaster object) {
  this.object = object;
}

public class Object1Holder extends RecyclerView.ViewHolder {
  private TextView title;
  private TextView subtitle;

  public Object1Holder(View itemView) {
   super(itemView);
   title = (TextView) itemView.findViewById(R.id.title);
   subtitle = (TextView) itemView.findViewById(R.id.subtitle);
  }
  public void bindObj(Object1 object) {
    title.setText(object.getTitle());
    subtitle.setText(object.getSubtitle());
  }
}
public class Object2Holder extends RecyclerView.ViewHolder {
  private TextView title;

  public Object2Holder(View itemView) {
   super(itemView);
   title = (TextView) itemView.findViewById(R.id.title);
  }
  public void bindObj(Object2 object) {
    title.setText(object.getTitle());
  }
}

 @Override
 public int getItemViewType(int position) {

  // I need to figure out the best way to determine if its already listed objects from the first object array (Object1)
 }

 @Override
 public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

  View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.object_item, viewGroup, false);
  switch(i) {
   case TYPE_OBJECT1: return new Object1Holder(view);
   case TYPE_OBJECT2: return new Object2Holder(view);
  }
  return null;
}

 public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {

    switch (viewHolder.getItemViewType()) {
        case TYPE_OBJECT1:
            Object1Holder object1 = (Object1Holder) viewHolder;
            object1.bindObj(object.first_objects.get(i));
            break;

        case TYPE_OBJECT2:
            Object2Holder object2 = (Object2Holder) viewHolder;
            object2.bindObj(object.second_objects.get(i));
            break;
    }
 }

 @Override
 public int getItemCount() {
     return object.first_objects.size() + object.second_objects.size();
 }
Jaison Brooks
  • 5,648
  • 7
  • 39
  • 76

1 Answers1

4

I need to figure out the best way to determine if its already listed objects from the first object array (Object1)

If position is bigger than (or equal to) the length of the first array, you are now into the second array. Subtract the length of the first array from the position to get the index into the second array.

Also note that you have a bug in onBindViewHolder(), in that you are not performing a similar subtraction to change the position (the i value) into the index into your second array, when it is the second array's view type that you're working on.

CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253
  • Thanks @CommonsWare , You were right, simple as subtracting the size of the first array when getting the start of the index on the 2nd array. It is working as expected now. – Jaison Brooks May 11 '15 at 01:52
  • @CommonsWare: I am facing a related problem ... could you please help me out with my question [here](http://stackoverflow.com/q/34431734/3287204) ? – Y.S Dec 27 '15 at 10:18