How to fire an event on a child section of a click in a dropdown in android
as edwin said you can make a costum adapter. In which you can set the OnClickListner () method for each view. how i did it here.
class CustomAdapter extends ArrayAdapter<Contact>
{
LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
public CustomAdapter(Context context, int textViewResourceId,
ArrayList<Contact> strings) {
//let android do the initializing :)
super(context, textViewResourceId, strings);
}
//class for caching the views in a row
private class ViewHolder
{
TextView id,name,phn_no;
}
ViewHolder viewHolder;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
//inflate the custom layout
convertView=inflater.inflate(R.layout.activity_main, null);
viewHolder=new ViewHolder();
//cache the views
viewHolder.id=(TextView) convertView.findViewById(R.id.contact_id_txt);
viewHolder.name=(TextView) convertView.findViewById(R.id.contact_name_txt);
viewHolder.phn_no=(TextView) convertView.findViewById(R.id.contact_ph_no_txt);
viewHolder.id.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Hi!!", Toast.LENGTH_SHORT).show();
}
});
viewHolder.name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Hello!!", Toast.LENGTH_SHORT).show();
}
});
viewHolder.phn_no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Heya!!!", Toast.LENGTH_SHORT).show();
}
});
//link the cached views to the convertview
convertView.setTag(viewHolder);
}
else
viewHolder=(ViewHolder) convertView.getTag();
//set the data to be displayed
viewHolder.id.setText(contacts.get(position).get_id()+"");
viewHolder.name.setText(contacts.get(position).get_name());
viewHolder.phn_no.setText(contacts.get(position).get_phn_no());
//return the view to be displayed
return convertView;
}
}
ExpandableListView expandableListView;
expandableListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
//////////////////////////Change your child text values here.
return false;
}
});
source to share
Try this way:
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater infalInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.layout, null);
}
tv_ChildFieldId = (TextView) convertView.findViewById(R.id.tv1);
tv_ChildFieldId.setText(getChildIdFromDB(groupPosition, childPosition).toString());
tv_ChildFieldId.setOnClickListener(new ID_OnClickListener());
//And so on
return convertView;
}
/**Onclick Listener for the Textview*/
private class ID_OnClickListener implements OnClickListener
{
@Override
public void onClick(View v)
{
}
});
source to share
So, you have to create a Custom adapter for the expandable list. Then execute onClickListener
for your interface with different widgets in custom adapter class
In your custom adapter class
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_layout, null);
}
//Give your On click Implementations Here
return convertView;
}
Carry out your activity withOnChildClickListener
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(MainActivity.this, "Clicked On Child",
Toast.LENGTH_SHORT).show();
return true; //or false
}
source to share
Take a look here You can define your custom extensible adapter with
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
Inside this method, you can define a child position in relation to the parent element and set the click listener you want
source to share
Method error from @edwin above! The ExpandableListView.onchildItemClick implementation in the activity makes no calls.
The problem arises in a special way if you have a custom and complex ChildView.
It worked for me by setting the onclickListener on the convertView . I get perfect clicks from inside the adapter.
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(_context, groupPosition + " clicked "
+ childPosition, Toast.LENGTH_SHORT).show();
}
});
Make sure you write this outside
if(convertView==null){.....layout inflation}
bracket.
source to share
Try it, it will help
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
if (groupPosition == 0){
if (childPosition == 0){
Intent intent = new Intent(MarketJaipur.this, ClothesMarket.class);
startActivity(intent);
}
}
return false;
}
});
source to share