开发者

append two different ArrayList into one adpter

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 list开发者_如何学运维1 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 ?


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜