2015-12-06 22 views
0

我主要的代码是这样的setOnClickListener不具有可扩展的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(新OnChildClickListener(){

上面的onChildclick它似乎setOnChildClickListener不工作...

它说:“不能解析符号setOnchildClickListener”

所以我想从父按一个孩子,并打开一个新的活动,我很接近成功,但我甚至无法找到我在做什么错误或正在发生的事情。

回答

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

嗨马丁,感谢太多回应,但似乎问题仍然存在,检查照片 HTTP: //i.snag.gy/AVMn9.jpg 我已经宣布 mExpandableList.setOnChildClickListener(this); 之后,我把setonchildlistener放在我的onclick上面,但它保持geting错误,并且我不能打开一个新的活动,即时寻找解决这个问题,因为很长一段时间:( – gioton

+0

OMG现在工作,感谢所以这么多马丁我爱你! – gioton

+0

很高兴听到它现在的作品!祝你的代码! –