kanchan
kanchan

Reputation: 77

ExpandableListView onChildClickListener

I am a new bee to android.

I am working with ExpandableListViewAdapter and facing a problem.

The onChildClickListener is not working when I click on the edit text box.

My question is will it work on edit text box or not.

I am implementing the BaseExpandableListAdapter.

Thanks in advance.

public class TryExpandableListViewActivity extends Activity implements     OnChildClickListener {

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

    llayout = (LinearLayout) findViewById(R.id.llayout);

    ExpandableListView list = new ExpandableListView(this);
    list.setGroupIndicator(null);
    list.setChildIndicator(null);
    String[] titles = {"A","B","C"};
    String[] fruits = {"a1","a2"};
    String[] veggies = {"b1","b2","b3"};
    String[] meats = {"c1","c2"};
    String[][] contents = {fruits,veggies,meats};
    SimplerExpandableListAdapter adapter = new SimplerExpandableListAdapter(this,titles, contents);

    list.setAdapter(adapter);

    llayout.addView(list);

    list.setOnChildClickListener(this);
}

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

}

class SimplerExpandableListAdapter extends BaseExpandableListAdapter {

private Context mContext;
private String[][] mContents;
private String[] mTitles;

public SimplerExpandableListAdapter(Context context, String[] titles, String[][] contents) {
    super();
    if(titles.length != contents.length) {
      throw new IllegalArgumentException("Titles and Contents must be the same size.");
    }
    mContext = context;
    mContents = contents;
    mTitles = titles;

  }

@Override
public Object getChild(int groupPosition, int childPosition) {
    return mContents[groupPosition][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) { 

        EditText row = (EditText)convertView;
        if(row == null) {
          row = new EditText(mContext);
        }
        row.setTypeface(Typeface.DEFAULT_BOLD);
        row.setText(mContents[groupPosition][childPosition]);
        return row;

}

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

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

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

}

Upvotes: 3

Views: 7835

Answers (5)

Bhagirath
Bhagirath

Reputation: 161

I faced the same prob...N referring to the above answers i coded...here's what i found...

  • Make sure SimplerExpandableListAdapters extends BaseExpandableListAdapter and implements OnChildClickListener.
  • Add onchildclick as customExpandableList.setOnChildClickListener(new MyCustomAdapter(MainActivity.this, )). *"argument" is what u receive in the SimplerExpandableListAdapter class. *"MyCustomAdapter" should be replaced with your ExpandableListAdapter(SimplerExpandableListAdapter in ur case)

I'm jus adding a code snippet for reference...

    public class MyCustomAdapter extends BaseExpandableListAdapter implements OnChildClickListener {
      private LayoutInflater inflater;
      private ArrayList<Parent> mParent;

And add the onchildclick inside the Adapterclass. As shown

    public class MyCustomAdapter extends BaseExpandableListAdapter implements OnChildClickListener {
      private LayoutInflater inflater;
      private ArrayList<Parent> mParent;
      public MyCustomAdapter(Context context, ArrayList<Parent> parent) {
        mParent = parent;
        inflater = LayoutInflater.from(context);
      }

      @Override
      public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2, int arg3, long arg4) {
        // TODO Auto-generated method stub
        Log.d("trial", "ExpandableList= "+arg0+" ,View= "+arg1+" ,arg2= "+arg2+", arg3= "+arg3+", arg4= "+arg4);
        return true;
      }
    }

Add call the the onChildclickListener in the activity where the Expandableview is coded.(MainActivity class in my example).

    mExpandableList.setOnChildClickListener(new MyCustomAdapter(MainActivity.this, arrayParents));

Upvotes: 1

Opiatefuchs
Opiatefuchs

Reputation: 9870

Late Answer, but maybe it helps others. I had the same Problem and to set the ExpandableListView to onItemClickListener() or onItemLongClickListener() does it:

    mExpandListView
            .setOnItemLongClickListener(new OnItemLongClickListener() {

                @Override
                public boolean onItemLongClick(AdapterView<?> adapterView,
                        View view, int position, long id) {
                    // TODO Auto-generated method stub
                    //do Your Code here

                    return true;
                }

            });

Upvotes: 0

Rodion Altshuler
Rodion Altshuler

Reputation: 1783

Have got the same problem (OnChildClickListener doesn't handle click event). In order to handle child items clicks, make sure your ExpandableListAdapter returns "true" (it "fales" by default) in isChildSelectable:

public class MyExpandableListAdapter extends BaseExpandableListAdapter  {

...

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

}

Upvotes: 3

Swapnil Kadam
Swapnil Kadam

Reputation: 4293

what is happening over here is your edittext is taking the focus. So the onChildClick is not getting fired.Try to make editText focus property to false through code.If you do it in .xml file it still wont work.Hopefully it works for you.:)

Upvotes: 3

triggs
triggs

Reputation: 5900

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