witco
witco

Reputation: 15

How to add different layouts into BaseAdapter?

I have two kinds of layout.I want to add them into ExpandableListView programming.I had tried to setTag with ViewHolder.But when I tried to getTag that always be NullpointException. Here is my code:

        SeekBarHolder sh=null;
        TickHolder th=null;
        if(convertView==null){
            switch((Integer)childData.get(groupPosition).get(childPosition).get("type")){
            case ProtocolConst.EXPANDABLE_CHILD_TYPE_SEEKBAR:
                sh=new SeekBarHolder();
                convertView=inflater.inflate(R.layout.fragment_left_listview_child_seekbar_item, null);
                sh.text=(TextView)convertView.findViewById(R.id.Left_ExpandableListView_Child_SeekBar_TextView);
                sh.seekbar=(SeekBar)convertView.findViewById(R.id.Left_ExpandableListView_Child_SeekBar_SeekBar);
                convertView.setTag(sh);

            case ProtocolConst.EXPANDABLE_CHILD_TYPE_TICK:
                th=new TickHolder();
                convertView=inflater.inflate(R.layout.fragment_left_listview_child_seekbar_item, null);
                th.text=(TextView)convertView.findViewById(R.id.Left_ExpandableListView_Child_Tick_TextView);
                th.tick=(ImageView)convertView.findViewById(R.id.Left_ExpandableListView_Child_Tick_ImageView);
                convertView.setTag(th);

            }}else{
           switch((Integer)childData.get(groupPosition).get(childPosition).get("type")){
            case ProtocolConst.EXPANDABLE_CHILD_TYPE_SEEKBAR:
                sh=(SeekBarHolder)convertView.getTag();
                break;
            case ProtocolConst.EXPANDABLE_CHILD_TYPE_TICK:
                th=(TickHolder)convertView.getTag();
                break;
            }
}

Upvotes: 0

Views: 154

Answers (2)

TommyNecessary
TommyNecessary

Reputation: 1377

You should imlpement additional method to your addapter. Adapter is using different layouts in other way. Use mehod getViewTypeCount() { to tell adapter how many views you'll use and getItemViewType(int position) to switch between views in getViewMethod.

Additionaly for ExpadnableListView you should use others methods, which allow you to use add other layout for your list.

Upvotes: 1

Lalit Poptani
Lalit Poptani

Reputation: 67286

If you are using ExpandableListView then you should use BaseExpandableListAdapter which has methods getChildView (int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) and getGroupView (int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) that allows you to inflate seperate layouts for group and child. Here is one of the tutorials

Upvotes: 0

Related Questions