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