开发者

How to handle multiple object TYPES within one array for android listview adapter?

I building a ListView in android. I have 5 different lists.

MyListAdapter extends BaseAdapter{

        ArrayList object1Array<Object1> = new ArrayList<Object1>();
        ArrayList object2Array<Object2> = new ArrayList<Object2>();
        ArrayList object3Array<Object3> = new ArrayList<Object3>();
        ArrayList object4Array<Object4> = new ArrayList<Object4>();
        ArrayList object5Array<Object5> = new ArrayList<Object5>();


        public View getView(int position, View convertView, ViewGroup parent)
        {
                if (convertView == null) {
                   .... inflate my layout xml
                }
             ....

I have built my adapter in the above manner. In this case instead of one single data set i have 5 different list to be rendered using the same listview. the reason behind using only 1 listview is the scrolling. I dont want 5 different scrolls.

I need to render object1Array and when i reach the end o开发者_运维百科f this list I would like to append the Object2Array and so on.

I solution I have on hand now is to wrap the object types with a common and create one single list of wrapper object and in the getView method based on the object type inside the wrapper render the data accordingly.

I would like to know i there is any better way of doing this...

Thanks in advance.


You can create class containing these 5 lists

Class ListHolder{
        ArrayList object1Array<Object1> = new ArrayList<Object1>();
        ArrayList object2Array<Object2> = new ArrayList<Object2>();
        ArrayList object3Array<Object3> = new ArrayList<Object3>();
        ArrayList object4Array<Object4> = new ArrayList<Object4>();
        ArrayList object5Array<Object5> = new ArrayList<Object5>();
}

Now make a list of ListHolder class

MyListAdapter extends BaseAdapter{
 ArrayList object1Array<ListHolder> = new ArrayList<ListHolder>();
}


    public static enum ObjectArrayTypeEnum{
            ObjectType1, ObjectType2, ObjectType3, ObjectType4, ObjectType5;
    }

    MyListAdapter<k extends ObjectArrayTypeEnum<k>> extends BaseAdapter{

            ArrayList myArray<k extends ObjectArrayTypeEnum<k>> = 
                                 new ArrayList<k extends ObjectArrayTypeEnum<k>>();

            myArray.addAll(Arraylist that contains Object1 elements)
            myArray.addAll(Arraylist that contains Object2 elements)
            myArray.addAll(Arraylist that contains Object3 elements)
            myArray.addAll(Arraylist that contains Object4 elements)
            myArray.addAll(Arraylist that contains Object5 elements)
       //This way I can restrict my adapter's dataset to contain only objects of type
       // which I wanted.


            public View getView(int position, View convertView, ViewGroup parent)
            {
                    if (convertView == null) {
                       .... inflate my layout xml
                    }
                 ....
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜