kunwar abhishek
kunwar abhishek

Reputation: 91

How to fire event on child click section in expandable list view in android

In expandble list on a parent click, I have a child in which I have 3 text view and i want to fire different event when i click different textviews for example.

for text view 1 output is hello
for text view 2 output is hi
for text view 3 output is heya

Upvotes: 3

Views: 11567

Answers (7)

Akhi Prajapati
Akhi Prajapati

Reputation: 147

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

Upvotes: 0

sud007
sud007

Reputation: 6141

Method from @edwin above failed! Implementing the ExpandableListView.onchildItemClick in activity doesn't give any calls.

The issue appears speacially if you have a custom and complicated ChildView.

For me it worked by setting a onclickListener on the convertView itself. 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 that outside the

if(convertView==null){.....layout inflation}

parenthesis.

Upvotes: 0

user1782231
user1782231

Reputation:

as edwin said you can make a costum adapter. In which you can setOnClickListner() method on each View. like I did in 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;
   }

  }

Upvotes: 2

edwin
edwin

Reputation: 8081

So you must create Custom adapter for expandable list . Then implement the onClickListener for your different widgets interface 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;
}

Go Through This and this

The implement your activity with OnChildClickListener

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

Upvotes: 0

GrIsHu
GrIsHu

Reputation: 23638

Try out 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) 
    {
    }
});

Upvotes: 1

Georgy Gobozov
Georgy Gobozov

Reputation: 13731

Take a look here You can define your custom expandable adapter that have

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

Inside this method you can determine child position at relative to parent and set oc click listener you want

Upvotes: 0

Venkatesh S
Venkatesh S

Reputation: 5494

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;

            }
        });

Upvotes: 2

Related Questions