0

请在下面检查我的代码:

    public class Expandable extends ExpandableListActivity implements OnChildClickListener {

ExpandableListView exp;

String[] parentList = {
        "Timeless", "The Gap", "Series Break", "Will You", "Casting Call", "Beyond Normal"
};

String[][] childList = {
        {
            "God In The Past", "God In The Present", "God In The Future"
        },
        {
            "Compassionate God", "Compassionate People"
        },
        {
            "Living The Life In The Kingdom", "Characteristics Of Early Church"
        },
        {
            "Be My Friend", "Be My Valentine", "Marry Me"
        },
        {
            "Casting Call"
        },
        {
            "Divine Courage", "Divine Guidance", "Divine Intervention"
        }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);

    exp = (ExpandableListView) findViewById (R.id.expandableListView1);
    exp.setAdapter(new MyAdapter(this));

    exp.setOnChildClickListener(this);

}

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

            if(childList[childPosition].equals("God In The Past")){
                Intent timeless = new Intent(this, MainActivity.class);
                startActivity(timeless);
            }

          return true;
         }



public class MyAdapter extends BaseExpandableListAdapter {

    private Context context;


    public MyAdapter(Context context) {
        // TODO Auto-generated constructor stub
        this.context = context;
    }

    @Override
    public Object getChild(int arg0, int arg1) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getChildId(int arg0, int arg1) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, 
            boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        TextView tv = new TextView(context);
        tv.setText(childList[groupPosition][childPosition]);
        tv.setPadding(50, 10, 10, 10);          
        return tv;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return childList[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return parentList.length;
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        TextView tv = new TextView(context);
        tv.setText(parentList[groupPosition]);
        tv.setPadding(30, 10, 10, 10);
        return tv;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isChildSelectable(int arg0, int arg1) {
        // TODO Auto-generated method stub
        return true;
    }




}



}

但在一线

 if(childList[childPosition].equals("God In The Past")){
                String TAG = null;
                Log.e(TAG, "God in the past");
                Intent timeless = new Intent(this, MainActivity.class);
                startActivity(timeless);
            }

它似乎没有得到选定的子视图?我在这里缺少什么?

任何帮助表示赞赏。谢谢。

4

1 回答 1

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

    if(childList[groupPosition][childPosition].equals("God In The Past")){
        //Intent timeless = new Intent(this, MainActivity.class);
        //startActivity(timeless);
        Log.d(getClass().getName(), "I am pressed");
    }

    return true;
}

像这样改变你的onChildClick功能,你没有考虑groupPosition.

还有一件事getGroupView像这样改变你的功能,getChildView也这样做。因为AdpaterViews通常重用他们的视图,所以不需要一次又一次地创建它。

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
       // TODO Auto-generated method stub
       TextView tv = (convertView == null ) ? new TextView(context) : (TextView)convertView;
       tv.setText(parentList[groupPosition]);
       tv.setPadding(30, 10, 10, 10);
       return tv;
}
于 2013-04-17T11:59:29.273 回答