2015-12-29 1 views
0

Je souhaite récupérer l'élément de données sélectionné dans la liste de sélection non réutilisable. J'ai d'abord obtenu tous les éléments que je les ai sélectionnés ou non ... mais je ne suis pas en mesure d'obtenir les éléments sélectionnés. et affichez-les en tant que parents avec ses enfants.Liste des éléments sélectionnés dans la liste des consommables

public class CategoryBaseAdapter extends BaseExpandableListAdapter { 
private Context _context; 
private List<ProfessionEntity> _listDataHeader; 
public static HashMap<ProfessionEntity, List<ProfessionChildEntity>> _listDataChild; 
public static HashMap<Integer, boolean[]> mChildCheckStates; 
    private Map<ProfessionEntity, List<ProfessionChildEntity>> selectedItems; 
public CategoryBaseAdapter(Context context, 
     List<ProfessionEntity> listDataHeader, 
     HashMap<ProfessionEntity, List<ProfessionChildEntity>> listChildData) { 
    this._context = context; 
    this._listDataHeader = listDataHeader; 
    this._listDataChild = listChildData; 
    selectedItems = new HashMap<ProfessionEntity,List<ProfessionChildEntity>>(); 
} 
public Map<ProfessionEntity, List<ProfessionChildEntity>> getSelectedItems() { 
    return selectedItems; 
} 
public boolean isSelected(int groupPosition, int childPosition){ 
    ProfessionEntity group = _listDataHeader.get(groupPosition); 
    ProfessionChildEntity child = getChild(groupPosition,childPosition); 
    List<ProfessionChildEntity> sel = selectedItems.get(group); 
    return sel != null && sel.contains(child); 
} 
public void toggleSelection(int groupPosition, int childPosition){ 
    ProfessionEntity group = _listDataHeader.get(groupPosition); 
    ProfessionChildEntity child = getChild(groupPosition,childPosition); 

    List<ProfessionChildEntity> sel = selectedItems.get(group); 
    if (sel == null){ 
     sel = new ArrayList<ProfessionChildEntity>(); 
     selectedItems.put(group, sel); 
    } 

    if (sel.contains(child)) 
     sel.remove(child); 
    else 
     sel.add(child); 

} 

@Override 
public ProfessionChildEntity getChild(int groupPosition, int childPosititon) { 
    return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
      .get(childPosititon); 
} 

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

@Override 
public View getChildView(final int groupPosition, int childPosition, 
     boolean isLastChild, View convertView, ViewGroup parent) { 
    final int mGroupPosition = groupPosition; 
    final int mChildPosition = childPosition; 
    final String childText = getChild(groupPosition, childPosition) 
      .getDescription(); 
    if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) this._context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.child_item, null); 

    } 
    final CheckBox childCheckBox = (CheckBox) convertView 
      .findViewById(R.id.childCheckBox); 
    TextView txtListChild = (TextView) convertView 
      .findViewById(R.id.childTextView); 
    txtListChild.setText(childText); 
    childCheckBox.setOnCheckedChangeListener(null);   
    return convertView; 
} 

@Override 
public int getChildrenCount(int groupPosition) { 
    return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
      .size(); 
} 

@Override 
public ProfessionEntity 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) { 
    String headerTitle = getGroup(groupPosition).getDecription(); 
    if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) this._context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.group_item, null); 
    } 
    TextView lblListHeader = (TextView) convertView 
      .findViewById(R.id.groupTextView); 
    CheckBox grpCheckBox = (CheckBox) convertView 
      .findViewById(R.id.groupCheckBox); 
    grpCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, 
       boolean isChecked) { 
      Log.d("grp clicked", "grpCheckBox"); 
     } 
    }); 
    lblListHeader.setText(headerTitle); 
    grpCheckBox.setTag(convertView); 
    if (isExpanded) { 
     Log.d("abc", "abc"); 
    } else { 
     Log.d("abc", "abc"); 
    } 
    return convertView; 
} 
@Override 
public boolean hasStableIds() { 
    return false; 
} 

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

Vous devez utiliser la HashMap avec consiste sa valeur mère et la valeur qui sont sélectionnés dans l'enfant –

+0

comment gérer les sélections aléatoires –

+0

En ce qui concerne le parent de l'élément sélectionné ... vous devez gérer cela ... Dans la méthode getChildView ... vous devez faire l'écouteur onclick et le gérer –

Répondre

0

Dans la méthode getGroupView() vous devriez essayer ceci:

@Override 
     public View getGroupView(int groupPosition, boolean isExpanded, 
       View convertView, ViewGroup parent) { 
     GroupHolder groupHolder; 
      String headerTitle = getGroup(groupPosition).getDecription(); 
      if (convertView == null) { 
       LayoutInflater infalInflater = (LayoutInflater) this._context 
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       convertView = infalInflater.inflate(R.layout.group_item, null); 
       groupHolder = new GroupHolder(); 
       groupHolder.lblListHeader = (TextView) convertView 
        .findViewById(R.id.groupTextView); 
       groupHolder.grpCheckBox = (CheckBox) convertView 
        .findViewById(R.id.groupCheckBox); 
     convertView.setTag(groupHolder); 
      } 
     else 
     { 
     groupHolder = (GroupHolder) convertView.getTag(); 
     } 

     groupHolder.grpCheckBox.setOnClickListener(new View.OnClickListener() 
     { 

       @Override 
       public void onClick(View v) 
       { 
        new onCheckBoxClick() 
        { 

         @Override 
         public void OnCheckClick(boolean isChecked) 
         { 
          groupHolder.grpCheckBox.setChecked(isChecked); 
         } 
        }; 
       } 
      }); 
      groupHolder.lblListHeader.setText(headerTitle); 
// you can get data from your modal class i.e.ProfessionEntity class which you have to show check box checked or not checked the following line is for your understanding.   
groupHolder.grpCheckBox.setChecked(groupHolder.grpCheckBox.getChecked());  

      return convertView; 
     } 

     private class GroupHolder 
     { 
       TextView lblListHeader 
       CheckBox grpCheckBox; 

     } 

     private interface onCheckBoxClick 
     { 
       void OnCheckClick(boolean isChecked); 
     } 

Si vous avez une question me laisse savoir

+0

J'ai des problèmes pour récupérer les éléments sélectionnés –

+0

Je rencontre des problèmes pour récupérer les éléments sélectionnés ... il y a une condition dans le code if (isexpand) que la valeur lg est imprimée dans logcat mais comment puis-je récupérer l'enfant Si vous voulez vérifier que 'grpCheckBox', vous voulez implémenter la valeur booléenne dans votre classe modale, je l'écris déjà dans le commentaire ci-dessus' groupHolder.grpCheckBox.setChecked (groupHolder.grpCheckBox.getChecked()) 'dans les valeurs sélectionnées –

+0

code. mais jusqu'à ce que vous ayez des questions, veuillez poster votre code de classe ProfessionEntity sinon je vais partager un projet d'application de démo, faites-moi savoir si vous avez des questions –