setOnClickListener不适用于可扩展的ListView

时间:2015-12-06 18:56:48

标签: android listview expandablelistview

我的主要代码是

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无法正常工作......

它说&#34;无法解析符号setOnchildClickListener&#34;

因此,我试图向父母施压,并开启一项新活动,我接近成功,但我甚至无法找到我做错了什么或者做什么正在发生。

1 个答案:

答案 0 :(得分:0)

首先你应该做

mExpandableList.setOnChildClickListener(this);

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

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

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