1

listview want to merge two different arraylist into one adapter. i try using the position value of the getView() method but it not display correct data to listviewfirst it display data from the one arrayList after it complete second arraylist should be start to display.

ArrayList<A> list1 = new  ArrayList<A>;
ArrayList<B> list2 = new ArrayList<B>;

i want to add this two list into one adapter.first i need to add list1 after list1 is complete data from list2 is display.i tried using position variable of getView() method but it not display correct data to me.

how to achieve this ?

ingsaurabh
  • 15,249
  • 7
  • 52
  • 81
Megha
  • 1,581
  • 2
  • 18
  • 33

2 Answers2

2

This can be achieved using adding both list into one list and set this common list to adapter.

ArrayList<A> list1 = new  ArrayList<A>();
ArrayList<Object> object = new ArrayList<Object>();
list1.add(new A());
object.add(new A());
ArrayList<B> list2 = new ArrayList<B>();
list2.add(new B());
object.add(new B());
object.add(new B());
listadapter mAdapterSeeLatest ;
mAdapterSeeLatest = new listadapter (getParent(),R.layout.help_row,object); 
mCheckInListView.setAdapter(mAdapterSeeLatest);
private class listadapter extends ArrayAdapter {

    ArrayList<Object> ListObject;
    int srNumber = 1;
    public listadapter (Context context, int textViewResourceId,
                ArrayList<Object> objects) {
        super(context, textViewResourceId, objects);
        ListObject = objects;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null)
        {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);    
        } 
        if(ListObject.get(position) instanceof A)
        {   

        }
        else if(ListObject.get(position) instanceof B)
        {
        }
    }

//now set this object ArrayList() into the List.
Kaushik
  • 6,150
  • 5
  • 39
  • 54
Megha
  • 1,581
  • 2
  • 18
  • 33
1

Can't you just combine those two into one ArrayList?

List<String> newList = new ArrayList<String>(listOne);
newList.addAll(listTwo);


Refer to this topic: How do I join two lists in Java?

Community
  • 1
  • 1
Pete Houston
  • 14,931
  • 6
  • 47
  • 60