如何在expandablelistview中执行复选框(在组视图和子视图中)

时间:2013-01-29 04:26:57

标签: android android-layout android-intent android-emulator android-contentprovider

我正在尝试在expansionablelistview中执行复选框(在groupview和子视图中)。但我不理解这个概念。 所以,如果你有任何想法,请与我分享。 这是我第一次使用这个概念(expandablelistview)。

复选框 groupview (CheckBox checkBoxGroup)

  1. 如果我在groupview(groupposition 0)中选中复选框,则必须检查child(childposition 0)中的所有项目。
  2. 就像我取消选中groupview(groupposition 0)一样,child(childposition 0)中的所有项必须取消选中。
  3. 复选框子视图(CheckBox checkBoxChild;)

    1. 只是复选框中的多项选择。
    2. 谢谢你的帮助。

      这是我的代码

      public class DetailAdapter extends BaseExpandableListAdapter {
          private LayoutInflater mInflater;
          private ArrayList<PatientSampleDetails> sampleArray;    
          private ArrayList<lstServiceOrderDetails> serviceLst;
          private Activity activity;
          ArrayAdapter<String> adapter;
          View row;
          public LayoutInflater minflater;
          Context mContext;
          int chldPosition;
         //   ServiceOrderList elementChild
          PatientSampleDetails elementChild;
          public static HashMap<Integer,Boolean> selectedGroupMembers = new HashMap<Integer, Boolean>();
          private static View container ;
          ActionItem nextItem;
          int posi;
          lstServiceOrderDetails child;
          View rowView;
      
          static class ViewHolder {
      
              private CheckBox checkBoxChild;
      
      
              public CheckBox checkBoxGroup;
              public Button button;
              public TextView service;
              }
      
          public WorkDetailAdapter(Activity activity, ArrayList<PatientSampleDetails> sampleArray) {
              this.sampleArray = sampleArray;
              this.activity = activity;
              mInflater = LayoutInflater.from(activity);      
          }
      
          public void setInflater(LayoutInflater mInflater) {
                this.minflater = mInflater;
               }
      
      
          public Object getChild(int groupPosition, int childPosition) {
              return sampleArray.get(groupPosition).lstServiceOrderDetails.get(childPosition);
          }
      
          public long getChildId(int groupPosition, int childPosition) {
              return childPosition;
          }
      
          public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
              Log.i("getChildView-getChild", groupPosition + "/" + childPosition);
              child = (lstServiceOrderDetails) getChild(groupPosition, childPosition);
              Log.i("child", "child data"+ child.getServiceName());
              rowView = convertView;
              final ViewHolder holder = new ViewHolder();
              if (convertView == null) {
                  convertView = mInflater.inflate(R.layout.detaillistrowview, null);
                  holder.serviceText = (TextView) convertView.findViewById(R.id.sevice);  
                  holder.checkBoxChild = (CheckBox)convertView.findViewById(R.id.checkBox);   
      
      
                  holder.checkBoxChild.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                          @Override
                          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                            
                               if (isChecked) {
                              elementChild = (PatientSampleDetails) holder.checkBoxChild.getTag();    
                              elementChild.setSelected(buttonView.isChecked());
                              Log.i("buttonView-checkBox", "getChildId  :" + getChildId(groupPosition, childPosition));
                              Log.i("status", ""+ elementChild.isSelected());
                              }   else {                                                    
                                      elementChild.setSelected(false);                        
                                  }
                               }
                      });
      
                  if (selectedGroupMembers.containsKey(groupPosition) == true){
                      holder.checkBoxChild.setActivated(true);
                      Log.i("true", "true");
                      holder.checkBoxChild.setSelectAllOnFocus(true);
                  }  else {
                      holder.checkBoxChild.setActivated(false);
                      Log.i("false", "false");
                      holder.checkBoxChild.setSelectAllOnFocus(true);
                  }
      
                      convertView.setTag(holder);
      
      
                  holder.checkBoxChild.setTag(sampleArray.get(groupPosition));
                  holder.serviceText.setText(Util.formatN2H(child.getServiceName())); 
                  Log.i("child", "child data ---> "+ child.getServiceName() +"/"+child.getServiceName());
      
      
              //Now write here code for set colors for rows
            /*  if(childPosition % 2 == 0) {
                 convertView.setBackgroundColor(Color.parseColor("#E7E8E8")); 
              } else {
                 convertView.setBackgroundColor(Color.parseColor("#C9CACC"));
              }*/
      
              // To change the color of the Listview row.         
          //      /   convertView.setBackgroundColor((childPosition & 1) == 1 ? Color.parseColor("#E7E8E8") : Color.parseColor("#C9CACC"));
      
                  return convertView;
          }
      
          public int getChildrenCount(int groupPosition) {
              serviceLst = sampleArray.get(groupPosition).getServiceOrderList();
              return serviceLst.size();       
          }
      
          public Object getGroup(int groupPosition) {
              return sampleArray.get(groupPosition);
          }
      
          public int getGroupCount() {
              return sampleArray.size();
          }
      
          public long getGroupId(int groupPosition) {
              return groupPosition;
          }
      
          public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
              final ViewHolder holder = new ViewHolder();
              PatientSampleDetails group = (PatientSampleDetails) getGroup(groupPosition);
              if (convertView == null) {
                  convertView = mInflater.inflate(R.layout.getgroupviewdetail, null);
                  convertView.setId(groupPosition);
      
              holder.checkBoxGroup = (CheckBox) convertView.findViewById(R.id.checkBoxGroup);
              holder.checkBoxGroup.setFocusable(false);
      
              holder.checkBoxGroup.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                  PatientSampleDetails elementGroup ;
                  @Override
                  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                            
      
                      if (isChecked == true) {
                          selectedGroupMembers.put(groupPosition, Boolean.valueOf(true)); //new Boolean(true));
                          elementGroup = (PatientSampleDetails) holder.checkBoxGroup.getTag();
                          elementGroup.setSelected(buttonView.isChecked());
                          Log.i("Clicked : ", "group" + groupPosition);
                          Log.i("buttonView", "getItemId  :" + getGroupId(groupPosition));
                          Log.i("status", ""+ elementGroup.isSelected());
                          Log.i("buttonView", "chldPosition  :" + getChildId(groupPosition, chldPosition));
                          notifyDataSetChanged();
                      } else if (selectedGroupMembers.containsKey(groupPosition)) {
                          selectedGroupMembers.remove(groupPosition);
                          elementGroup.setSelected(false);
                          Log.i(" Removed : ", "group" + groupPosition);
                          notifyDataSetChanged();
                      }
                      }
              });
      
              convertView.setTag(holder); 
      
              holder.checkBoxGroup.setTag(sampleArray.get(groupPosition));
      
              } else {
                  ((ViewHolder) convertView.getTag()).checkBoxGroup.setTag(sampleArray.get(groupPosition));
      
              }
              return convertView;
          }
      
          public boolean hasStableIds() {
              return false;
          }
      
          public boolean isChildSelectable(int groupPosition, int childPosition) {
              return true;
          }
      
        }
      

2 个答案:

答案 0 :(得分:0)

试试这个,当您滚动List时,每分钟都会刷新Adapter。您必须为每个项目设置标记,否则您将获得标记值

if (convertView == null) {
 //Add ur code 
 convertView.setTag(holder);
 }else{
holder = (ViewHolder) convertView.getTag();
}

它会识别您ListView中的唯一值。

答案 1 :(得分:0)

  1. 在应用程序上下文中使用arraylist来保存将要存在的状态 用于保存列表的当前状态。第一次从中获取数据 webservice或任何地方并填充它。

  2. 将此arraylist传递给 适配器填充列表。

  3. 在groupview复选框上的
  4. 更改了该列表的更新。并打电话 notifyDatasetChanged。