Neha
Neha

Reputation: 681

How to add different children in each section of an expandable list view in android?

I have an expandable list view. It has 4 sections. In the first two sections i want to display text. In third one i want to display image and in fourth one i want to display a video. In short every parent is having a different child. How to implement this in an expandable list in android?

Thanks, Neha

Upvotes: 0

Views: 1660

Answers (2)

Frank Harper
Frank Harper

Reputation: 3156

If you are extending CursorTreeAdapter to make your ExpandableListAdapter, you should manage creating the different types of views in newChildView() and binding them in bindChildView(). You can use the data in the Cursor to differentiate between the different cases.

Example code

    @Override
    protected View newChildView(
        Context context,
        Cursor cursor,
        boolean isLastChild,
        ViewGroup parent )
    {
        LayoutInflater mInflater = LayoutInflater.from( context );
        String firstColumnName = cursor.getColumnName( 0 );
        if( firstColumnName.equals( "_id" )) {
            return mInflater.inflate( R.layout.main_list_item, parent, false );                
        } else if( firstColumnName.equals( "name" )){
            return mInflater.inflate( R.layout.search_list_item, parent, false );                
        } else {
            throw new IllegalArgumentException( "Unknown firstColumnName:"
                + firstColumnName );
        }
    }

    @Override
    protected void bindChildView(
        View view,
        Context context,
        Cursor cursor,
        boolean isLastChild )
    {
        String firstColumnName = cursor.getColumnName( 0 );
        if( firstColumnName.equals( "_id" )) {
            bindMainView( view, context, cursor, isLastChild );
        } else if( firstColumnName.equals( "name" )){
            bindSearchView( view, context, cursor, isLastChild );
        } else {
            throw new IllegalArgumentException( "Unknown firstColumnName:"
                + firstColumnName );
        }
    }

Upvotes: 0

sksamuel
sksamuel

Reputation: 16397

You need to use an ExpandableListAdapter where you return different types of views depending on what group the item is in.

So in your list adapter you override

getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)

and do something depending on the groupPosition, for example

getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (groupPosition == 0) return text views for this child
if (groupPositon == 1) return image views for this child
}

That should get your started. It's quite easy from there.

Upvotes: 1

Related Questions