ExpandableListView Adapter child element onClick event same result

I’m displaying a textview and a button for bookmark as child elements in my expandable list view adapter. On click of button the the button should change the text to “Remove Bookmark” and On click of “remove bookmark ” change text of button to “Add Bookmark”.

The problem is when I click on first item in list button, second and fourth ..so on changes .Could you please help me out with this problem.

Here is my code for expandable list adapter:

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Activity context;
private Map<String, String> itemCollections;
private List<String> answers;
// private ViewSwitcher viewSwitcher1;
//  Button imgAddFav;

boolean[] fav;
boolean flag1;

ViewHolder holder;

public ExpandableListAdapter(Activity context, List<String> answers,
                             Map<String, String> itemCollections) {
    this.context = context;
    this.itemCollections = itemCollections;
    this.answers = answers;
    fav = new boolean[itemCollections.size()];
}

public Object getChild(int groupPosition, int childPosition) {
    return itemCollections.get(answers.get(groupPosition));
}

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


public class ViewHolder {
    TextView item;
    Button imgAddFav;
    // ImageView img;
}


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


    String result = (String) getChild(groupPosition, childPosition);
    LayoutInflater inflater = context.getLayoutInflater();


    if (convertView == null) {
        convertView = inflater.inflate(R.layout.reading_childlist, null);
        holder = new ViewHolder();
        convertView.setTag(holder);
        holder.item = (TextView) convertView.findViewById(R.id.txtExpandedList);
        //  viewSwitcher1 = (ViewSwitcher) convertView.findViewById(R.id.my_switcher1);
        holder.imgAddFav = (Button) convertView.findViewById(R.id.imgBtnAddFav);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    //fav = new boolean[groupPosition];

    // imgNoFav =(ImageButton) viewSwitcher1.findViewById(R.id.imgBtnNoFav);

    holder.imgAddFav.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            addFavourite(groupPosition);

        }
    });

    //convertView.setTag(holder);

    // imgNoFav.setOnClickListener(new View.OnClickListener() {

    ///    public void onClick(View v) {


    //  }
    // });


    //  tBtnFav.setChecked(onOff[groupPosition]);


//        favoriteButton.put(groupPosition,tBtnFav);

    holder.item.setText(result);
    //convertView.setTag(holder);

    return convertView;
}


public int getChildrenCount(int groupPosition) {
    return 1;
}

public Object getGroup(int groupPosition) {
    return answers.get(groupPosition);
}

public int getGroupCount() {
    return answers.size();
}

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

public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    String laptopName = (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.reading_grouplist,
                null);
    }
    TextView item = (TextView) convertView.findViewById(R.id.txtListTitle);
    item.setTypeface(null, Typeface.BOLD);
    item.setText(laptopName);

    return convertView;
}

public boolean hasStableIds() {
    return true;
}

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


public void addFavourite(int position){
    System.out.println("position" +position);
   // if(fav[position])  {
        //imgAddFav.setBackgroundResource(R.mipmap.ic_favorite_black_18dp);
       if(!flag1){
        holder.imgAddFav.setText("Remove Bookmark");

        System.out.println("Add favourite" +fav[position]);
        fav[position] =false;
    }else if(flag1){
       // imgAddFav.setBackgroundResource(R.mipmap.ic_favorite_border_black_24dp);
        holder.imgAddFav.setText("Add Bookmark");

        System.out.println("Remove favourite" +fav[position]);
        fav[position] =true;
    }







   /* if (onOff[groupPosition]) {
        tBtnFav.setBackgroundResource(R.mipmap.ic_favorite_border_black_24dp);
        tBtnFav.setChecked(false);
        onOff[groupPosition] = false;



    } else {
        tBtnFav.setBackgroundResource(R.mipmap.ic_favorite_black_18dp);
        onOff[groupPosition] = true;
        tBtnFav.setChecked(true);*/


    }


}


Source: bookmarks

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.