ExpandableListView OnChildClickListener not work
Asked Answered
A

3

17

i have my problem with my Expandable ListView on my Android Application

this my code

package proyek.akhir;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.TextView;

public class list_tempat extends Activity {
private List<String> groupData;
private List<List<String>> childrenData;
static String asalfromList ;
protected static final Object[] String = null;
static String l1 ;
private void loadData() {
    groupData = new ArrayList<String>();
    groupData.add("Group 1");
    groupData.add("Group 2");
    groupData.add("Group 3");

    childrenData = new ArrayList<List<String>>();
    List<String> sub1 = new ArrayList<String>();
    sub1.add("G1 Item 1");
    sub1.add("G1 Item 2");
    childrenData.add(sub1);
    List<String> sub2 = new ArrayList<String>();
    sub2.add("G2 Item 1");
    sub2.add("G2 Item 2");
    sub2.add("G2 Item 3");
    sub2.add("G2 Item 4");
    childrenData.add(sub2);
    List<String> sub3 = new ArrayList<String>();
    sub3.add("G3 Item 1");
    sub3.add("G3 Item 2");
    sub3.add("G3 Item 3");
    sub3.add("G3 Item 4");
    sub3.add("G3 Item 5");
    childrenData.add(sub3);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.expandable_list_view);

    l1 = "l1";

    loadData();

    ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandable_list_view);
    expandableListView.setAdapter(new ExpandableAdapter());
    expandableListView.setOnGroupClickListener(new OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View clickedView, int groupPosition, long groupId) {
            return false;
        }
    });
    expandableListView.setOnChildClickListener(new OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView expandablelistview,
                View clickedView, int groupPosition, int childPosition, long childId) {
            Intent i = getIntent();

            //asalfromList =(String) ((TextView) view).getText();
            Intent intent = new Intent(list_tempat.this, ruteangkot.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra("l1", String);
            intent.putExtra("textAsal",asalfromList );

            if (i.getStringExtra("tujuan") != null){
                intent.putExtra("textTujuan",list_tempat2.tujuanfromList );
                System.out.println("adaan");
            }else{
                System.out.println("eweh");
                intent.putExtra("textTujuan","");
            }
            menuutama.mu = "";
            list_tempat2.l2 = "";

            startActivity(intent);
            finish();
            System.out.println("wkwkwk");
            showMessage("hahasu" + ((TextView)clickedView).getText());
            return false;
        }
    });
    expandableListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
        @Override
        public void onGroupCollapse(int groupPosition) {

        }
    });
    expandableListView.setOnGroupExpandListener(new OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {

        }
    });
}

private class ExpandableAdapter extends BaseExpandableListAdapter {

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childrenData.get(groupPosition).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        TextView text = null;
        if (convertView != null) {
            text = (TextView)convertView;
            text.setText(childrenData.get(groupPosition).get(childPosition));
        } else {
            text = createView(childrenData.get(groupPosition).get(childPosition));
        }
        return text;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childrenData.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupData.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return groupData.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        TextView text = null;
        if (convertView != null) {
            text = (TextView)convertView;
            text.setText(groupData.get(groupPosition));
        } else {
            text = createView(groupData.get(groupPosition));
        }
        return text;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    private TextView createView(String content) {
        AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(  
                ViewGroup.LayoutParams.FILL_PARENT, 38);  
        TextView text = new TextView(list_tempat.this);  
        text.setLayoutParams(layoutParams);  
        text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);  
        text.setPadding(40, 0, 0, 0);  
        text.setText(content);
        return text;
    }
}

private void showMessage(String message) {
    Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}

the expandableListView.setOnChildClickListener not work i want catch the data from that child listview, anyone can help me? thanks

Aaronson answered 17/7, 2012 at 19:27 Comment(5)
You need to be more specific. Post error logs, give us more information.Togliatti
doesn't have error in error logs, setOnChildClickListener just not workAaronson
Put some log statements in the ``OnChildClickListener` and make sure it is not being called.Lotetgaronne
i put log code : Log.i("Tag", "-------------------------------------------------");Aaronson
but doesn't show any error log at logchat, help meeeeAaronson
R
62

You must activate that your children are selectable! To do that return true in your (overriden) isChildSelectable method of class ExpandableListAdapter.

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
Revoke answered 5/9, 2012 at 14:38 Comment(0)
R
29

If you are using OnChildClickListener and/or OnGroupClickListener then each child view within the group's rows and children's rows must not be focusable. For example, if you have a checkbox in the child then set the checkbox to not focusable:

checkBox.setFocusable(false);

Also, if you set the group/child convertView to clickable it will prevent the clicks from reaching the OnChildClickListener and OnGroupClickListener. If this is the case go to getGroupView, in your expListViewAdapter, and set:

convertView.setClickable(false);

Then go to getChildView, in your expListViewAdapter, and set:

convertView.setClickable(false);

After this both OnGroupClickListener and OnChildClickListener should work - granted you set the listeners in the first place (using expandableListView.setOnGroupClickListener(...) and expandableListView.setOnChildClickListener(...))

Relieve answered 26/12, 2013 at 22:58 Comment(0)
T
0

You have to change return value is true instead of false. Make your child view selectable.

Use this:

  @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

Instead Of:

@Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return false;
        }
Teen answered 25/5, 2015 at 5:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.