1

I have expndable list view where each child holds a switch button. when user change the switch state some code needs to run. the question is what will be the best practice to handle the click event?

the activity class and adapter class are separated classes.

below you can see the adapter code. the code works as expected however when the user change the switch state i am calling a method which lies in the fragment. I have created an instance of the fragment class called "mSSLLB_Group_frag" in order to call the method.

the issue starts when global objects configured on fragment class are null unless i declare them as static.

I dont want to use static variables as I believe they are bad practice and can result in memory leak.

The reason i asked for best practice is that the fragment class has values that are passed using bundle and to handle the event of the switch I will have to pass some of the variables to the adapter class.

do you have any suggestions? or at least an idea why objects are null inside the method that is being called from the adapter?

public class SSLLB_GRP_EXP_LST_ADAPTER extends BaseExpandableListAdapter{

    private Context CNTX;
    private List<SSLLB_GROUP_get_set>SSLLB_GRP_PARENT=new ArrayList<>() ;
    private LinkedHashMap<Integer, List<SSLLB_GROUP_get_set_child>> listDataChildResult = new LinkedHashMap<>();
    protected static Boolean oper_failed=false;
    private static final SSLLB_Group_frag mSSLLB_Group_frag=new SSLLB_Group_frag();




    public SSLLB_GRP_EXP_LST_ADAPTER(List<SSLLB_GROUP_get_set> _SSLLB_GRP_PARENT,LinkedHashMap<Integer, List<SSLLB_GROUP_get_set_child>>  _listDataChildResult,Context _CNTX){
        this.SSLLB_GRP_PARENT=_SSLLB_GRP_PARENT;
        this.listDataChildResult=_listDataChildResult;
        this.CNTX=_CNTX;
    }

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

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

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

    @Override
    public SSLLB_GROUP_get_set_child getChild(int groupPosition, int childPosition) {
        return listDataChildResult.get(groupPosition).get(childPosition);
    }

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

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

    @Override
    public boolean hasStableIds() {

        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {

        View parentView =convertView;
        ViewHolder holder=new ViewHolder();

        LayoutInflater inflater = (LayoutInflater)this.CNTX.getSystemService
                  (Context.LAYOUT_INFLATER_SERVICE);

        if(parentView==null)
        {

            parentView = inflater.inflate(R.layout.grp_inx_list_layout, parent, false);
            holder.grp_names=(TextView)parentView.findViewById(R.id.tv_Grp_Names);
            holder.grp_metrics=(TextView)parentView.findViewById(R.id.tv_Grp_Metrics);
            holder.grp_he=(TextView)parentView.findViewById(R.id.tv_Grp_He);
            parentView.setTag(holder);
        }
        else{

            holder=(ViewHolder)parentView.getTag();
        }

        holder.grp_names.setText(SSLLB_GRP_PARENT.get(groupPosition).Get_Grp_Inx());
        holder.grp_metrics.setText(SSLLB_GRP_PARENT.get(groupPosition).Get_Grp_Metrics());
        holder.grp_he.setText(SSLLB_GRP_PARENT.get(groupPosition).Get_Grp_He());




        return parentView;
    }

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

        View childView=convertView;
        LayoutInflater inflaterchild = (LayoutInflater)this.CNTX.getSystemService
                  (Context.LAYOUT_INFLATER_SERVICE);

        ViewHolder holder=new ViewHolder();

        if(childView==null){

            childView = inflaterchild.inflate(R.layout.group_exp_list_child_layout, parent, false);
            holder.grp_rl_inx=(TextView)childView.findViewById(R.id.tv_rl_SR_name_in_GRP);
            holder.grp_rl_stat=(TextView)childView.findViewById(R.id.tv_Rl_SR_stat);
            holder.OPR_R_SR=(Switch)childView.findViewById(R.id.sw_rl_dis_en);
            childView.setTag(holder);
        }

        else {
            holder=(ViewHolder)childView.getTag();
        }



        holder.grp_rl_inx.setText(getChild(groupPosition,childPosition).get_sr_inx());
        holder.grp_rl_stat.setText(getChild(groupPosition,childPosition).getRL_SR_STAT_STR());

        if(getChild(groupPosition,childPosition).get_sr_stat()==1){
            holder.OPR_R_SR.setChecked(true);


        }
        else {
                holder.OPR_R_SR.setChecked(false);

        }

        holder.OPR_R_SR.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Switch cb_state = (Switch) v.findViewById(R.id.sw_rl_dis_en);

                try {

                    if (cb_state.isChecked()) {
                        mSSLLB_Group_frag.set_SR_STAT(getChild(groupPosition, childPosition), 1);
                    }

                    else  {

                         mSSLLB_Group_frag.set_SR_STAT(getChild(groupPosition, childPosition), 2);
                    }
                }
                catch (Exception e){
                    e.printStackTrace();
                }
            }

            });



        return childView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {

        return true;
    }






public final class  ViewHolder
{
   TextView grp_names;
   TextView grp_metrics;
   TextView grp_he;
   TextView grp_rl_inx;
   TextView grp_rl_stat;
   Switch   OPR_R_SR;
}


}

Thank you for your help

user2145673
  • 363
  • 1
  • 9
  • 23
  • 1
    Post some code. Do already have Click Listener for your list items ? Or Implemented OnItemClick Listener ? What you have tried, and what didn't work ? – osayilgan Apr 13 '15 at 19:36
  • like @osayilgan said post some code. You should handle the click events within the adapter. – Eugene H Apr 13 '15 at 20:14
  • @osayilgan Hi Guys , I have added some code and some more details. do you have any idea? – user2145673 Apr 14 '15 at 18:42

1 Answers1

1

As far as I could understand from your question, you have a fragment and have an expandable listview inside it. And what you want is to send callbacks to fragment when any action is taken on the list item (or on it's child).

If I understood your question correctly then the answer is simple. You cannot create the Fragment's instance inside your fragment, which is the parent view of your adapter class. Instead, you can create an interface in the Adapter Class and implement that interface in the Fragment. So once you trigger the callback methods, it will fire the event in the Fragment.

If you are new on this subject and don't know how to start, I just found an SOF Question regarding to same problem. Check it out to learn how to create a communication between Fragment and the Adapter via an interface.

Community
  • 1
  • 1
osayilgan
  • 5,873
  • 7
  • 47
  • 68
  • Dude, thank you very much!!!! this was exactly what I was looking for and it works perfect. I just wanted to make sure that I understand the solution. what basically we are doing is creating an interface that will hold a reference to the fragment? and this is done by the casting assignment am I right? – user2145673 Apr 14 '15 at 20:22
  • Yes, exactly. Fragment implements the interface which was created in the Adapter (It doesn't have to be inside of the Adapter). It's quite usual way of using interfaces. – osayilgan Apr 14 '15 at 20:52
  • sorry im stil new to this :-). – user2145673 Apr 15 '15 at 09:02