4

我想添加可扩展列表视图的子项的单击事件。我不想扩展 Activity,因为我在下面添加了另一个 Listview。现在我想添加孩子的点击事件。

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;
    }

}
4

1 回答 1

6

您忘记添加 onChildClick 以查看。尝试这个 :

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

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

最好的祝愿。

于 2013-06-21T10:43:14.840 回答