setOnClickListener 不适用于可扩展的 ListView

setOnClickListener doesn't work with expandable ListView

我的主要代码是这样的

package rotiseria.gioton.com.expandablelistview;


    import android.app.Activity;
    import android.app.ExpandableListActivity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ExpandableListView;
    import java.util.ArrayList;

    public class MainActivity extends Activity implements ExpandableListView.OnChildClickListener {


private ExpandableListView mExpandableList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    mExpandableList = (ExpandableListView) findViewById(R.id.expandable_list);

    ArrayList<Parent> arrayParents = new ArrayList<Parent>();
    ArrayList<String> arrayChildren = new ArrayList<String>();

    //here we set the parents and the children
    for (int i = 0; i < 5; i++) {
        //for each "i" create a new Parent object to set the title and the children
        Parent parent = new Parent();
        parent.setTitle("Parent " + i);

        arrayChildren = new ArrayList<String>();
        for (int j = 0; j < 5; j++) {
            arrayChildren.add("Child " + j);
        }
        parent.setArrayChildren(arrayChildren);

        //in this array we add the Parent object. We will use the arrayParents at the setAdapter
        arrayParents.add(parent);
    }


    mExpandableList.setAdapter(new MyCustomAdapter(MainActivity.this, arrayParents));



}


@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
                            int childPosition, long id) {


    switch (groupPosition) {
        case 1:
            switch (childPosition) {
                case 0:
                    Intent myIntent = new Intent(v.getContext(), nuevaActivity.class);
                    startActivityForResult(myIntent, 0);
                    break;
                case 1:
                    Intent Open2 = new Intent(MainActivity.this, nuevaActivity.class);
                    startActivity(Open2);
                    break;
            }
        case 2:
            switch (childPosition) {
                case 2:
                    Intent asariIntent = new Intent(MainActivity.this, nuevaActivity.class);
                    startActivity(asariIntent);
                    break;
            }
    }
    return false;
}
}

当我尝试添加这块时

mExpandableList.setOnChildClickListener(new OnChildClickListener() {

onchildclick 上方似乎 setOnChildClickListener 不工作...

它说 "cannot resolve symbol setOnchildClickListener"

所以我试图从 parent 中按 child 并打开一个新的 activity,我快要成功了,但我什至找不到我想要的我做错了或正在发生什么。

首先你应该做

mExpandableList.setOnChildClickListener(this);

因为您的 class 已经在实现该接口。

但是如果你真的想要另一个匿名监听器,那么正确的语法是:

    mExpandableList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(final ExpandableListView parent, final View v, final int groupPosition, final int childPosition, final long id) {
            return false;
        }
    });