2017-08-10 75 views
3

https://i.stack.imgur.com/vUpUM.pngnotifyDataSetChanged()不BaseAdapter正在與複選框列表中的Android

我採取了ListView和複選框爲listItems中。正如你在圖像描述中看到的那樣,動作母版是父母和添加,刪除是child.Parent,孩子由編碼分隔。現在操作母版被選中,添加和刪除也應該被選中,反之亦然。 它的一切運作良好,但主要問題是當我們點擊行動大師整個列表應刷新,ADD和DELETE應該檢查,但它不會出現在用戶界面,inshort我正在登錄,但不是在UI中獲取。

這裏是我的代碼,當它是填寫:

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

這裏是自定義適配器的代碼:

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; 
    } 
} 

即使我曾寫過dataAdapter.notifyDataSetChanged();仍然不刷新我已經在適配器中編寫了複選框單擊事件,因此如何刷新多餘的問題,點擊每個複選框,它應該刷新,以便我可以獲得所需的輸出。 欣賞的help.Thank你

+0

喲DON不需要聲明自定義適配器,您必須修改清單(在適配器中傳遞)複選框單擊事件,並且在列表中更新後使用dataAdapter.notifyDataSetChanged()。希望它會爲你工作 –

+0

你能告訴我一些代碼因爲我沒有得到... @GuptaShashwat –

回答

0

假設你有一個清單和適配器:

ArrayList<String> my_list=new ArrayList<>(); 
my_list.add(contents); 
ArrayAdapter<> my_adapter = new ArrayAdapter<>(AgentCreateIntimation.this, android.R.layout.simple_spinner_dropdown_item, my_list); 

找到你的複選框

CheckBox ChkBx = (CheckBox) findViewById(R.id.checkbox); 
ChkBx.setOnCheckedChangeListener(new OnCheckedChangeListener() 
{ 
    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
    { 
     if (isChecked) 
     { 
my_list.add(contents); 
my_adapter.notifyDataSetChanged() 
      // perform logic 
     } 

    } 
}); 
+0

@ GuptaShashweat ..謝謝你的迴應...代碼不適合我... –

相關問題