在扩展活动而不是活动时,将Click事件添加到可扩展列表视图子项?

NRa*_*man 4 android expandablelistview

我想添加Expandable ListView子项的click事件...我不想扩展Activity,因为我在这里添加了另一个Listview ...吼...现在我想添加孩子的click事件... .请帮忙....

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

}
Run Code Online (Sandbox Code Playgroud)

Yak*_*pan 6

你忘了添加onChildClick来查看.试试这个 :

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

    mExpandableListView.setAdapter(adapter);
    mExpandableListView.setOnChildClickListener(this);
}
Run Code Online (Sandbox Code Playgroud)

最好的祝愿.