neknek mouh
neknek mouh

Reputation: 1802

Getting selected child from expandable listview then go to new activty in android

Please check my code below:

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




}



}

But in the line

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

It doesn't seem to be getting the selected child view? What am I missing in here?

Any help is appreciated. Thanks.

Upvotes: 0

Views: 1591

Answers (1)

Triode
Triode

Reputation: 11359

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

Change your onChildClick function like this, you are not considering the groupPosition.

One more thing change your getGroupView function like this do the same with getChildView also. Because AdpaterViews normally reuse their Views so no need to create it again and again.

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

Upvotes: 1

Related Questions