2015-12-06 20 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

很高興聽到它現在的作品!祝你的代碼! –