mercredi 9 août 2017

notifyDataSetChanged() is not working in BaseAdapter with Checkbox list in android

http://ift.tt/2vHyXl2

I had taken listview and checkbox as a listItems. As you see in the Image Description Action Master is a parent and Add,Delete are the child.Parent and child are separated by coding.Now Action Master is checked,Add and Delete should also checked and vice versa.Its all working well but the main issue is when we clicked on Action Master the whole list should refresh, ADD and DELETE should be checked but it is not appearing on UI ,inshort I am getting in log but is not obtain in UI.

Here is my code when it is fill:

dataAdapter = new MyCustomAdapter(AddRolePermission.this, listPageActionMaster);
listView.setAdapter(dataAdapter);

here is the code of custom adapter:

class MyCustomAdapter extends BaseAdapter{


    AddRolePermission addRolePermission;
    List<ClsPageActionMaster> list_role_master;


    public MyCustomAdapter(AddRolePermission addRolePermission, List<ClsPageActionMaster> lstDemo) {
        this.addRolePermission=addRolePermission;
        this.list_role_master=lstDemo;
    }

    @Override
    public int getCount() {
        return list_role_master.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            ///
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.permissions_checkbox, null);



            CheckBox checkBox1 = (CheckBox) convertView.findViewById(R.id.checkBox1);
            TextView pageid = (TextView) convertView.findViewById(R.id.pageid);

            ClsPageActionMaster ObjPageAction = new ClsPageActionMaster();
            ObjPageAction = list_role_master.get(position);


            Log.v("RolePermissionData", "BEFORE CHECKED");
            Log.v("RolePermissionData", "CheckBox-" + String.valueOf(ObjPageAction.getSelected()));
            Log.v("RolePermissionData", "PageID-" + String.valueOf(ObjPageAction.getPageid()));
            Log.v("RolePermissionData", "PageName-" + String.valueOf(ObjPageAction.getPage_name()));
            Log.v("RolePermissionData", "type" + String.valueOf(ObjPageAction.getType()));
            Log.v("RolePermissionData", "position" + String.valueOf(position));
            Log.v("RolePermissionData", "ActionName" + String.valueOf(ObjPageAction.getAction_name()));


            if (ObjPageAction.getType().toString().equalsIgnoreCase("parent")) {
                checkBox1.setText("  " + ObjPageAction.getPage_name());
                checkBox1.setTypeface(null, Typeface.BOLD);

            } else {
                checkBox1.setText("        " + ObjPageAction.getAction_name());
            }
            checkBox1.setChecked(ObjPageAction.getSelected());
            pageid.setText(String.valueOf(ObjPageAction.getPageid()));
            checkBox1.setTag(String.valueOf(position));

            checkBox1.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    CheckBox cb = (CheckBox) v;

                    Toast.makeText(AddRolePermission.this, cb.getText().toString()+"-"+cb.isChecked(), Toast.LENGTH_SHORT).show();

                    Integer _POSSITION = Integer.parseInt(String.valueOf(cb.getTag()));
                    ClsPageActionMaster _ObjAction = list_role_master.get(_POSSITION);

                    //check is parent? or not
                    //if yes then find all chied and check them if not
                    if (_ObjAction.getType().equalsIgnoreCase("parent")) {

                        Log.v("CHECKBOXlist", "parent true -" + String.valueOf(_ObjAction.getSelected()));
                        Integer _parentPageID = _ObjAction.getPageid();
                        Log.v("CHECKBOXlist", "_parentPageID -" + String.valueOf(_parentPageID));
                        for (ClsPageActionMaster ObjPageActionMaster : list_role_master) {
                            if (_parentPageID == ObjPageActionMaster.getPageid()) {
                                Log.v("CHECKBOXlist", "pageid == actionPagID -" + String.valueOf(_ObjAction.getPageid()));
                                Log.v("CHECKBOXlist", "_parentPageID -" + String.valueOf(ObjPageActionMaster.getPageid()));
                                ObjPageActionMaster.setSelected(cb.isChecked());
                                list_role_master.set(list_role_master.indexOf(ObjPageActionMaster), ObjPageActionMaster);
                            }
                        }

                        dataAdapter.notifyDataSetChanged();
                    } else {
                        Log.v("CHECKBOXlist", "CHK_CHECKED-" + String.valueOf(_ObjAction.getSelected()));
                        _ObjAction.setSelected(cb.isChecked());
                        list_role_master.set(_POSSITION, _ObjAction);
                    }


                    Log.v("CHECKBOXlist", "AFTER CHECKED");
                    Log.v("CHECKBOXlist", "CHK_POSITION-" + String.valueOf(_POSSITION));
                    Log.v("CHECKBOXlist", "CHK_ID-" + String.valueOf(_ObjAction.getId()));
                    Log.v("CHECKBOXlist", "chk_name-" + String.valueOf(_ObjAction.getAction_name()));
                    Log.v("CHECKBOXlist", "type" + String.valueOf(_ObjAction.getType()));
                    Log.v("CHECKBOXlist", "Position" + String.valueOf(position));
                    Log.v("CHECKBOXlist", "CHK_CHECKED-" + String.valueOf(_ObjAction.getSelected()));
                }
            });

        }
        return convertView;
    }
}

Even I had written dataAdapter.notifyDataSetChanged(); still its not refreshing I had written the checkbox click event in adapter so how can I refresh thats the issue,By every checkbox click,it should be refresh so that i can get the required output. Appreciate for help.Thank you




Aucun commentaire:

Enregistrer un commentaire