android developer
android developer

Reputation: 1253

onChildClickListener not responding in ExapandableListView

Why is the Log not getting printed? The log for all the others is working but not for child.

The log for the text view does appear but when I make use of edit text, it does not work.

Why so? Where am I going wrong?

ExpandableListView expListView;

expListView = getExpandableListView();

expListView.setAdapter(new ExAdapter(this));

        expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
                // TODO Auto-generated method stub

            }

        });

        expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int groupPosition) {
                // TODO Auto-generated method stub

            }

        });

        expListView.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                Log.i("Tag", "-------------------------------------------------");
                return true;
            }

        });

code edited---------

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

    if(convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.name_child, null);

    }

    TextView txtFName = (TextView) convertView.findViewById(R.id.txtFname);
    txtFName.setText(arrChildhintelements[groupPosition][childPosition]);


    editFName = (EditText) convertView.findViewById(R.id.editFName);
    editFName.setHint(arrChildhintelements[groupPosition][childPosition]);


    editFName.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            //doing somthing....

            return false;
        }

    }); 

    return convertView;
} 

Upvotes: 1

Views: 1022

Answers (3)

Dmitriy Pavlukhin
Dmitriy Pavlukhin

Reputation: 419

Put

myList.setOnChildClickListener(listAdapter);

in your onCreate method.

Then onChildClick method instantly starts working.

Upvotes: 0

jeet
jeet

Reputation: 29199

public class TryTestActivity extends ExpandableListActivity {

    ExpandableListAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String[] titles = {"A","B","C"};
        String[] fruits = {"a1","a2"};
        String[] veggies = {"b1","b2","b3"};
        String[] meats = {"c1","c2"};
        String[][] contents = {fruits,veggies,meats};
        String[][] edit_contents = {fruits,veggies,meats};

        SimplerExpandableListAdapter adapter = new SimplerExpandableListAdapter(this,titles, contents, edit_contents);
        setListAdapter(adapter);


        getExpandableListView().setOnChildClickListener(this);



    }



}
class SimplerExpandableListAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private String[][] mContents;
    private String[] mTitles;
    LayoutInflater inflater;
    private CharSequence[][] mEditContents;

    public SimplerExpandableListAdapter(Context context, String[] titles, String[][] contents, String[][] editContents) {
        super();

        if(titles.length != contents.length) {
            throw new IllegalArgumentException("Titles and Contents must be the same size.");
        }
        mContext = context;
        mContents = contents;
        mTitles = titles;
        mEditContents = editContents;

        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mEditContents[groupPosition][childPosition];
    }

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

    @Override
    public View getChildView(final int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) { 


        //if(convertView == null) 
        {

            convertView = inflater.inflate(R.layout.child_main, null);
        }

        TextView row1 = (TextView)convertView.findViewById(R.id.txt1);
        EditText row2 = (EditText)convertView.findViewById(R.id.txt2);

        row1.setText(mContents[groupPosition][childPosition]);
        row2.setText(mEditContents[groupPosition][childPosition]);

        row2.addTextChangedListener(new TextWatcher(){
            public void afterTextChanged(Editable s) {
                mEditContents[groupPosition][childPosition]=s.toString();
            }
            public void beforeTextChanged(CharSequence s, int start, int count, int after){}
            public void onTextChanged(CharSequence s, int start, int before, int count){
                //mEditContents[groupPosition][childPosition]=s.toString();
            }
        });
        //row2.on
        /*row2.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    //some code...
                    return false;
                }

            });*/

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mContents[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mContents[groupPosition];
    }

    @Override
    public int getGroupCount() {
        return mContents.length;
    }

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        TextView row = (TextView)convertView;
        if(row == null) {
            row = new TextView(mContext);
        }
        row.setTypeface(Typeface.DEFAULT_BOLD);
        row.setText(mTitles[groupPosition]);
        return row;
    }

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

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

Upvotes: 0

user647826
user647826

Reputation:

Without seeing code this is just a guess but the childCLickListener may be consuming the event so its not reaching the edit text, try returning false in childClickListener

Edit

Okay I just created a small project with your code and I couldn't get the onCildClickListener to fire, changing it to a TextView and it works fine. Its something to do with the EditText (perhaps its consuming the event or its not registering as a click but as a change in focus but I'm just guessing at this point).

So in answer to your question, is it possible - I don't think so. But I did make a small workaround that may be of use.

Attach an onTouchListener() to the EditText and check for MotionEvent.ACTION_DOWN

Upvotes: 3

Related Questions