Pongpat
Pongpat

Reputation: 13348

How to recognize 'onClick view' and get 'groupId' in ExpandableView

From the code below (In ExpandableView Adapter) how can I get group id after I click on the image (iv). If user click on the Group list I want to expand it as it usual, but if user click on the image in the Group list I want to invoke some other action and the SampleGroup's id is needed.

public View getGroupView(int groupPosition, boolean isExpanded, View converView, ViewGroup parent){

     SampleGroup group = (SampleGroup)getGroup(groupPosition);

     if(convertView == null){
          LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          convertView = layoutInflater.inflate(R.layout.list_group, null);
     }

     ImageView iv = (ImageView)convertView.findViewById(R.id.some_iv):
     TextView tv = (TextView)convertView.findViewById(R.id.some_tv);

     iv.setImageDrawable(group.getImage());
     iv.setOnClickListener(onClickLintener);
     tv.setText(group.getText());
}


OnClickListener onClickLintener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        //How to get group id here???
    }
};

public class SampleGroup{
     private int id;
     private String name;
     private Drawable image;

     public SampleGroup(int id, String name, Drawable image){
          this.id = id;
          this.name = name;
          this.image = image;
     }

     public int getId(){
          return id;
     }

     public void setId(int id){
          this.id = id;
     }

     public String getName(){
          return name;
     }

     public void setName(String name){
          this.name = name
     }

     public Drawable getImage(){
          return image;
     }

     public void setImage(Drawable image){
          this.image = image
     }
}

Upvotes: 1

Views: 444

Answers (1)

zetsin
zetsin

Reputation: 303

try this one:

public View getGroupView(int groupPosition, boolean isExpanded, View converView, ViewGroup parent){
  ......
  ImageView iv = (ImageView)convertView.findViewById(R.id.some_iv);
  iv.setTag(group.id);
}

then

OnClickListener onClickLintener = new OnClickListener() {
  @Override
  public void onClick(View v) {
      //How to get group id here???
      int groupId = (Integer) v.getTag();
  }
};

Upvotes: 3

Related Questions