0

I follow the give link: Android ListView adapter with two ArrayLists

But on above link solution add both arraylist to a single ArrayList. can please suggest any answer as my requirement given below:

Requirement: I am getting data from server that is both different API so how to set to listview after parsing?

Please suggest any solution Thanks in advance

Community
  • 1
  • 1
  • I assume that you need to put heterogeneous items in the list. Right? You can create an additional class and put only those fields from both which is required for use. Alternatively, there are many other ways to achieve the same. – Yasir Tahir Apr 05 '16 at 05:52
  • Yes, can you please share any link please – Chandrama Prasad Apr 05 '16 at 05:56

3 Answers3

1

there are a few things you can do in this case:

  1. model your data in an inheritance (or interface) model to allow abstracting your data to a unified type and display it while using getItemViewType do differentiate the data

  2. use a 3rd party library such as mergelist and use 2 different adapters

thepoosh
  • 12,497
  • 15
  • 73
  • 132
1

When your Model isn't same for both of the two, then you can create a interface named CustomInterface and implement it to both of your models. Then in the adapter, instead of the model, use CustomInterface and in your getView, you have to check the current model like:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater theInflater = LayoutInflater.from(getContext());
View theView = theInflater.inflate(R.layout.custom_row, parent, false);
if(entity instanceof Obj1){
    // Model 1
} else if(entity instanceof Obj2){
    // Model 2
}
return theView;
}

Remember that, List works only for homogeneous collections. If you have heterogeneous collection, you can implement interface on all of the models and make a List of the interface and then can put any model within that list which implement same interface.

Yasir Tahir
  • 790
  • 1
  • 11
  • 31
1

sample here.

  1. interface

    public interface DataType {
            String getTitle();
            String getAddress();
    }
    
  2. data class

    public class AData implements DataType {
        String titleA;
        String addressA;
    
        @Override
        public String getTitle() {
            return titleA;
        }
    
        @Override
        public String getAddress() {
            return addressA;
        }
    
        // getter & setter
    }
    
    public class BData implements DataType {
        String titleB;
        String addressB;
    
        @Override
        public String getTitle() {
            return titleB;
        }
    
        @Override
        public String getAddress() {
            return addressB;
        }
    
        // getter & setter
    }
    
  3. adapter

    public class CustomAdapter implements BaseAdapter {
    
    List<DataType> arrayList;
    
    // getCount...
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // inflating views, bindings, reuse, etc
    
        DataType data = arrayList.get(position);
        textView.setText(data.getTitle());
        textView.setText(data.getAddress());
    
        // return ...
    
    }
    
myoungjin
  • 895
  • 1
  • 13
  • 27