androidandroid-fragmentsexpandablelistviewandroid-fragmentactivityexpandablelistadapter

How to send selected child view data of expandable list view from fragment to parent activity?


I have one ParentActivity which has TextView tvCount, one fragment which has expandable list view. I need to update the number of children count selected in the expandable list view.

In my fragment i have an adapter -

 mExpandableListView = (ExpandableListView) view.findViewById(R.id.lvExp);
 mAdapter = new ExpandableServiceListViewAdapter(getActivity(),
            mGroups);

ExpandableServiceListViewAdapter.java

public class ExpandableServiceListViewAdapter extends BaseExpandableListAdapter {


      private static SparseArray<GroupModel> mGroups;
      public LayoutInflater mInflater;
      public Activity mActivity;



public ExpandableServiceListViewAdapter(Activity act, SparseArray<GroupModel> groups) {
    mActivity = act;
    this.mGroups = groups;
    mInflater = act.getLayoutInflater();
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return mGroups.get(groupPosition).children.get(childPosition);
}

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


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

    final GroupModel children = (GroupModel) getChild(groupPosition, childPosition);
    TextView title, duration, gender, actualprice, finalprice;
    final TextView tvCounter, tvTotalCost;
    ImageView offericon = null;
    final CheckBox checkBox;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.services_item, null);
    }
    title = (TextView) convertView.findViewById(R.id.tv_title);
    duration = (TextView) convertView.findViewById(R.id.tv_duration);
    gender = (TextView) convertView.findViewById(R.id.tv_gender);
    actualprice = (TextView) convertView.findViewById(R.id.tv_actualprice);
    finalprice = (TextView) convertView.findViewById(R.id.tv_finalprice);
    offericon = (ImageView) convertView.findViewById(R.id.iv_offer);
    checkBox = (CheckBox) convertView.findViewById(R.id.cb_check);
    tvTotalCost = (TextView) mActivity.findViewById(R.id.tv_cost);
    tvCounter = (TextView) mActivity.findViewById(R.id.tv_counter);

    title.setText(children.getmServiceModel().getmServiceTitle());
    duration.setText("• " + children.getmServiceModel().getmDuration() + "min");
    gender.setText("• " + children.getmServiceModel().getmGender());
    actualprice.setText("Rs " + children.getmServiceModel().getmActualPrice());
    actualprice.setPaintFlags(actualprice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
    finalprice.setText("\u20B9" + children.getmServiceModel().getmFinalPrice());

    if (children.getmServiceModel().getmHasOffer()) {
        offericon.setVisibility(View.VISIBLE);
    } else {
        offericon.setVisibility(View.INVISIBLE);
    }


    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            checkBox.toggle();
            if (checkBox.isChecked()) {
                ((BaseActivity) mActivity).showToast("Yes");
                BusinessActivity.mSelectedMap.put((groupPosition * 1000) + childPosition, children);



            } else {
                ((BaseActivity) mActivity).showToast("No");
                BusinessActivity.mSelectedMap.remove((groupPosition * 1000) + childPosition);

            }
        }
    });
    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return mGroups.get(groupPosition).children.size();
}

@Override
public Object getGroup(int groupPosition) {
    return mGroups.get(groupPosition);
}

@Override
public int getGroupCount() {
    return mGroups.size();
}

@Override
public void onGroupCollapsed(int groupPosition) {
    super.onGroupCollapsed(groupPosition);
}

@Override
public void onGroupExpanded(int groupPosition) {
    super.onGroupExpanded(groupPosition);
}

@Override
public long getGroupId(int groupPosition) {
    return 0;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.services_group_header, null);
    }
    GroupModel group = (GroupModel) getGroup(groupPosition);
    TextView title = (TextView) convertView.findViewById(R.id.tv_title);
    title.setText(group.getmTitle());
    TextView count = (TextView) convertView.findViewById(R.id.tv_count);
    count.setText("(" + group.getmCount() + ")");

    ImageView iv = (ImageView) convertView.findViewById(R.id.iv_offer);
    if (group.getmSubCategory().getmHasOffers())
        iv.setVisibility(View.VISIBLE);
    else
        iv.setVisibility(View.INVISIBLE);


    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

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

Here i am trying to create a map of selected children which i need to send it to the parent activity to update the count.

BusinessActivity.java is the parent activity which has a fragment. I have defined mSelectedMap in the activity.I am adding the entry to this map when selected and removing when unselected.

Can someone help me how do i set the count value which is in the parent activity from this adapter.


Solution

  • There are 3 ways to solve your problem.

    1. Use global static variable, define its value in your fragment and display its value in your activity.
    2. Create method in your activity and call it from fragment with appropriate parameters.
    3. Use EventBus.

    EventBus simplifies the communication between components. It performs well with Activities, Fragments, and background threads.

    Here is nice example of EventBus.