NRahman
NRahman

Reputation: 2757

Add click event to Expandable List View child while extending Activity instead of Activity

I want to add click event of the child of the Expandable ListView. I don't want to Extend the Activity because I have add another Listview here below. Now I want to add click event of the child.

package com.example.events;

import java.util.ArrayList;
import java.util.List;

import com.example.events.GroupEntity.GroupItemEntity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;

public class MainActivity extends Activity implements OnChildClickListener {
    private ExpandableListView mExpandableListView;
    private List<GroupEntity> mGroupCollection;
    String URL;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.event_mainactivity);
        prepareResource();
        initPage();

    }

    private void prepareResource() {

        mGroupCollection = new ArrayList<GroupEntity>();

        for (int i = 1; i < 3; i++) {
            GroupEntity ge = new GroupEntity();
            ge.Name = "Group" + i;

            for (int j = 1; j < 4; j++) {
                GroupItemEntity gi = ge.new GroupItemEntity();
                gi.Name = "Child" + j;
                ge.GroupItemCollection.add(gi);
            }

            mGroupCollection.add(ge);
        }

    }

    private void initPage() {
        mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
        ExpandableListAdapter adapter = new ExpandableListAdapter(this,
                mExpandableListView, mGroupCollection);

        mExpandableListView.setAdapter(adapter);
    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {
        Toast.makeText(getApplicationContext(), childPosition+"Clicked", Toast.LENGTH_LONG).show();
        return true;
    }

}

Upvotes: 4

Views: 9240

Answers (1)

Yakiv Mospan
Yakiv Mospan

Reputation: 8224

You forgot to add onChildClick to view. Try this :

private void initPage() {
    mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
    ExpandableListAdapter adapter = new ExpandableListAdapter(this,
            mExpandableListView, mGroupCollection);

    mExpandableListView.setAdapter(adapter);
    mExpandableListView.setOnChildClickListener(this);
}

Best wishes.

Upvotes: 6

Related Questions