从expandablelistview中获取所选子项然后转到android中的新活动

时间:2013-04-17 11:37:10

标签: android android-intent expandablelistview expandablelistadapter

请检查下面的代码:

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

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

感谢任何帮助。感谢。

1 个答案:

答案 0 :(得分: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;
}