2014-04-15 140 views
0
public class CustomExpandableListAdapter extends BaseExpandableListAdapter implements OnCheckedChangeListener { 

     private CoreWorkFlowActivity context; 
     private List<ActivationType> listDataHeader=new ArrayList<ActivationType>(); // header titles 
     // child data in format of header title, child title 
     ArrayList<Integer> arrIVRetest = new ArrayList<Integer>(); 
     private HashMap<String, List<OrderLineItem>> listDataChild= new HashMap<String, List<OrderLineItem>>(); 
     public static int count=0; 
     public static int countIVRetest=0; 
     public CustomExpandableListAdapter(CoreWorkFlowActivity context, ArrayList<ActivationType> listDataHeader, 
       HashMap<String, List<OrderLineItem>> listDataChild) { 
      this.context = context; 
      this.listDataHeader = listDataHeader; 
      this.listDataChild = listDataChild; 
     } 

     @Override 
     public Object getChild(int groupPosition, int childPosititon) { 

      return this.listDataChild.get(this.listDataHeader.get(groupPosition).getName()) 
        .get(childPosititon).getProductName().getValue().toString(); 
     } 

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

      return childPosition; 
     } 
     ArrayList<Integer> arrActivation = new ArrayList<Integer>(); 

     @Override 
     public View getChildView(final int groupPosition, final int childPosition, 
       boolean isLastChild, View convertView, ViewGroup parent) 
     { 
      //listDataHeader.clear(); 
      //arrIVRetest.clear(); 
      //listDataChild.clear(); 

      final OrderLineItem childObj=this.listDataChild.get(this.listDataHeader.get(groupPosition).getName()).get(childPosition); 
      final String childText = (String) getChild(groupPosition, childPosition); 
      final String groupName=this.listDataHeader.get(groupPosition).getName(); 

      if (convertView == null) { 
       LayoutInflater infalInflater = (LayoutInflater) this.context 
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       convertView = infalInflater.inflate(R.layout.olilist_expanded, null); 
      } 

      TextView txtListChild = (TextView) convertView 
        .findViewById(R.id.oliexpandedTextView); 

      txtListChild.setText(childText); 
      txtListChild.setTypeface(RobotoTypeface.sRobotoRegular(context)); 
      TextView txtlistStatus=(TextView)convertView.findViewById(R.id.oliexpandedstatus); 
      txtlistStatus.setTypeface(RobotoTypeface.sRobotoItalic(context)); 
      CheckBox cbChild=(CheckBox)convertView.findViewById(R.id.checkboxoli); 
      cbChild.setOnCheckedChangeListener(new OnCheckedChangeListener() 
      { 


       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 

        //IVRETEST 
        if(isChecked && groupName.equals(OLIConstants.IV_RETEST)) 
        { 

         countIVRetest=countIVRetest+1; 
         arrIVRetest.add(childPosition);//adding oli which r checked 
         context.getSummaryFragment().getOli2(arrIVRetest,groupPosition); 

         if(!context.getSummaryFragment().ivretestAll.isEnabled()) 
         { 
          context.getSummaryFragment().enableButtonIVRetest(true); 
         } 

        } 
        else if(!isChecked && groupName.equals(OLIConstants.IV_RETEST)) 
        { 
         countIVRetest=countIVRetest-1; 
         Integer arrv = arrIVRetest.indexOf(childPosition); 
         arrIVRetest.remove(arrv); 
         context.getSummaryFragment().getOli2(arrIVRetest,groupPosition); 
         if(context.getSummaryFragment().ivretestAll.isEnabled() && countIVRetest<=0) 
         { 
          context.getSummaryFragment().enableButtonIVRetest(false); 
         } 
        } 




        //Activation 
        if(isChecked && groupName.equals(OLIConstants.FAILED_TO_ACTIVATE)) 
        { 
         count=count+1; 
         arrActivation.add(childPosition); 
         context.getSummaryFragment().getOli(arrActivation,groupPosition); 

         if(!context.getSummaryFragment().activateSystem.isEnabled()) 
         { 
          context.getSummaryFragment().enableButton(true); 
         } 

        } 
        else if(!isChecked && groupName.equals(OLIConstants.FAILED_TO_ACTIVATE)) 
        { 
         count=count-1; 

         arrActivation.remove(arrActivation.indexOf(childPosition)); 
         context.getSummaryFragment().getOli(arrActivation,groupPosition); 
         if(context.getSummaryFragment().activateSystem.isEnabled() && count<=0) 
         { 
          context.getSummaryFragment().enableButton(false); 
         } 


        } 

       } 

      }); 


      ProgressBar pbchild=(ProgressBar)convertView.findViewById(R.id.progressBaroli); 
      ImageView backArrow=(ImageView)convertView.findViewById(R.id.backwardArrow); 


      if(groupName.equals(OLIConstants.READY_TO_ACTIVATE)||groupName.equals(OLIConstants.ACTIVATED_EQUIPMENT) || groupName.equals(OLIConstants.NO_ACTION)||groupName.equals(OLIConstants.COMPLETED_WORK_ITEMS)||groupName.equals(OLIConstants.PENDING_ITEMS)) 
      { 
       backArrow.setVisibility(View.GONE); 

      } 
      else//added 
      { 
       backArrow.setVisibility(View.VISIBLE); 
      } 

      if(groupName.equals(OLIConstants.IV_RETEST)) 
      { 

       txtlistStatus.setVisibility(View.VISIBLE); 
       txtlistStatus.setText(childObj.getStatus().getValue().toString()); 
      } 
      else 
      { 
       txtlistStatus.setVisibility(View.GONE); 
      } 
    // 


      if(!childObj.isSelectable()) 
      { 
       cbChild.setVisibility(View.GONE); 
       pbchild.setVisibility(View.GONE); 
      } 
      else if(childObj.isSelectable() == true && (groupName.equals(OLIConstants.FAILED_TO_ACTIVATE)||groupName.equals(OLIConstants.IV_RETEST))) 
      { 
       cbChild.setVisibility(View.VISIBLE); 
       //childObj.setSelectable(false); 
       if(groupName.equals(OLIConstants.IV_RETEST)) 
       { 
        if(childObj.isChecked()==true)//giving this only for ivretest 
        { 
         if(childObj.getStatus().getValue().toString().equalsIgnoreCase("Open") && childObj.getTechAction().getValue().toString().equalsIgnoreCase("IV Retest")) 
         { 
          cbChild.setChecked(true); 
          childObj.setChecked(false);//for making it not to select again 
         } 
         else{ 
          cbChild.setChecked(false); 
         } 

         if(countIVRetest>0) 
         {context.getSummaryFragment().ivretestAll.setEnabled(true);} 
        } 
        else{ 
         cbChild.setChecked(false); 

        } 
       } 

       /*else{ 
        cbChild.setChecked(false); 
       }*/ 

      } 
      //*************************************************************************************** 
      /*else if(childObj.isSelectable() == true && groupName.equals(OLIConstants.FAILED_TO_ACTIVATE)) 
      { 

       cbChild.setVisibility(View.VISIBLE); 
       childObj.setChecked(false); 

      } 
      else if(childObj.isSelectable() == true && groupName.equals(OLIConstants.IV_RETEST)) 
      { 
       cbChild.setVisibility(View.VISIBLE); 
       if(groupName.equals(OLIConstants.IV_RETEST)) 
       { 
        if(childObj.isChecked()==true)//giving this only for ivretest 
        { 
         if(childObj.getStatus().getValue().toString().equalsIgnoreCase("Open") && childObj.getTechAction().getValue().toString().equalsIgnoreCase("IV Retest")) 
         { 
          cbChild.setChecked(true); 
          childObj.setChecked(false);//for making it not to select again 
         } 
         else{ 
          cbChild.setChecked(false); 
         } 

         if(countIVRetest>0) 
         {context.getSummaryFragment().ivretestAll.setEnabled(true);} 
        } 
        else{ 
         cbChild.setChecked(false); 

        } 
       } 


      }*/ 
      //******************************************************************************** 


      return convertView; 
     } 

     @Override 
     public int getChildrenCount(int groupPosition) { 
      return this.listDataChild.get(this.listDataHeader.get(groupPosition).getName()) 
        .size(); 
     } 

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

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

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

     @Override 
     public View getGroupView(int groupPosition, boolean isExpanded, 
       View convertView, ViewGroup parent) { 
      ActivationType headerTitleValues = (ActivationType) getGroup(groupPosition); 
      if (convertView == null) { 
       LayoutInflater infalInflater = (LayoutInflater) this.context 
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       convertView = infalInflater.inflate(R.layout.summarylist_layout, null); 
      } 

      TextView lblListHeader = (TextView) convertView 
        .findViewById(R.id.summary_text); 
      lblListHeader.setTypeface(RobotoTypeface.sRobotoBold(context)); 
      lblListHeader.setText(headerTitleValues.getName()); 
      lblListHeader.setTextColor(Color.parseColor(headerTitleValues.getColor())); 

      ImageView lblimage=(ImageView)convertView.findViewById(R.id.summarylist_color); 
      lblimage.setBackgroundColor(Color.parseColor(headerTitleValues.getColor())); 

      ImageView arrowImg=(ImageView)convertView.findViewById(R.id.arrowImg); 
      if(isExpanded) 
      { 
       arrowImg.setBackgroundResource(R.drawable.up_arrow); 
      } 
      else 
      { 
       arrowImg.setBackgroundResource(R.drawable.down_arrow); 
      } 

      return convertView; 
     } 

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

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

     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      // TODO Auto-generated method stub 

     } 

} 

我有一个问题。当我选中一个复选框中的childList和滚动起来后滚动和向下其他子复选框被随机得到选择取消。 我无法找出问题所在的位置 请帮忙复选框被随机抽查和未选中时,我勾选复选框

+2

您将不得不大幅减少您发布的代码,只留下相关信息以帮助我们为您提供帮助。 – nKn

+1

http://www.vogella.com/tutorials/AndroidListView/article.html#listviewselection在第14点检查 –

回答

0

您应该维护collections数组中的状态(Checked/Unchecked)。由于适配器的概念是重用视图,只要您滚动列表,它将重新使用以前的视图从屏幕中移出。

所以,您应该维护一个数组以存储使用唯一变量位置的检查/取消选中状态。使用此数组来恢复getView方法中的以前的状态。