检查复选框时Android :: Expandable检查列表重复

时间:2013-10-08 09:02:06

标签: android checkbox expandablelistview

我这里有点问题,

我做了一个可扩展的清单,但是这里的问题,就是当我检查一个盒子检查另一个盒子时,我无法找到原因,请我需要你的帮助。这是代码:

此代码在我的主要活动中:

/*
 * Preparing the list data
 */
private void prepareListData() {



    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();

    // Adding child data
    listDataHeader.add("By Body Part");
    listDataHeader.add("By Objectives");
    listDataHeader.add("By Muscle");
    listDataHeader.add("By Equipments");
    listDataHeader.add("By Sports");
    listDataHeader.add("By Level");

    // Adding child data
    List<String> bodyPart = new ArrayList<String>();
    List<String> objectives = new ArrayList<String>();
    List<String> muscleGroup = new ArrayList<String>();
    List<String> equipment = new ArrayList<String>();
    List<String> sports = new ArrayList<String>();
    List<String> level = new ArrayList<String>();

    level.add("Easy");
    level.add("Medium");
    level.add("Hard");

    if (shareAll.getFilteredItem() != null) {
        if (!shareAll.getFilteredItem().isFilteredItemEmpty()) {

        }
    }

// dataController.getinstance.get ...是一个将数据库中的列表调用到的方法 //列在下面的代码中         if(isNetworkAvailable()&amp;&amp; isBodyTableEmpty()){

        listDataChild.put(listDataHeader.get(0), DataController
                .getInstance(getApplicationContext())
                .getAllBodyPartsNames());
        listDataChild.put(listDataHeader.get(1), DataController
                .getInstance(getApplicationContext())
                .getAllObjectivesNames());
        listDataChild.put(listDataHeader.get(2), DataController
                .getInstance(getApplicationContext())
                .getAllMuscleGroupsNames());
        listDataChild.put(listDataHeader.get(3), DataController
                .getInstance(getApplicationContext())
                .getAllEquipmentsNames());
        listDataChild.put(listDataHeader.get(4), DataController
                .getInstance(getApplicationContext()).getAllSportsNames());


    listDataChild.put(listDataHeader.get(5), level);}
}

    // preparing list data
    prepareListData();

    listAdapter = new ExpandableListAdapter(this, listDataHeader,
            listDataChild, btnFilter);

    // setting list adapter
    expListView.setAdapter(listAdapter);

//这里是getgroup父视图

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

    TextView lblListHeader = (TextView) convertView
            .findViewById(R.id.lblListHeader);
    lblListHeader.setTextColor(Color.WHITE);
    Utils.TypeFace(lblListHeader, _context.getAssets());
    // lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);

    return convertView;
}

//她是孩子的视图

@Override
public View getChildView(final int groupPosition, final int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    final String childText = (String) getChild(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(
                android.R.layout.simple_list_item_multiple_choice, null);
    }
    final CheckedTextView txtListChild = (CheckedTextView) convertView
            .findViewById(android.R.id.text1);
    // ////////

    FilteredItem SharedFilteredItem = shareAll.getFilteredItem();
    if (SharedFilteredItem != null) {
        ArrayList<String> bodyPartList = SharedFilteredItem
                .getByBodyPartItem();
        for (int k = 0; k < bodyPartList.size(); k++) {
            if (childText.equals(bodyPartList.get(k))) {
                txtListChild.setChecked(true);
            }
        }

        ArrayList<String> equipmentList = SharedFilteredItem
                .getByEquipmentItem();
        for (int k = 0; k < equipmentList.size(); k++) {
            if (childText.equals(equipmentList.get(k))) {
                txtListChild.setChecked(true);
            }
        }
        ArrayList<String> levelList = SharedFilteredItem.getByLevelItem();
        for (int k = 0; k < levelList.size(); k++) {
            if (childText.equals(levelList.get(k))) {
                txtListChild.setChecked(true);
            }
        }
        ArrayList<String> muscleGroupList = SharedFilteredItem
                .getByMuscleGroupItem();
        for (int k = 0; k < muscleGroupList.size(); k++) {
            if (childText.equals(muscleGroupList.get(k))) {
                txtListChild.setChecked(true);
            }
        }
        ArrayList<String> objectivesList = SharedFilteredItem
                .getByObjectivesItem();
        for (int k = 0; k < objectivesList.size(); k++) {
            if (childText.equals(objectivesList.get(k))) {
                txtListChild.setChecked(true);
            }
        }
        ArrayList<String> sportList = SharedFilteredItem.getBySportItem();
        for (int k = 0; k < sportList.size(); k++) {
            if (childText.equals(sportList.get(k))) {
                txtListChild.setChecked(true);
            }
        }
    }
    // ////////
    txtListChild.setTextColor(Color.WHITE);
    Utils.TypeFace(txtListChild, _context.getAssets());
    filteredItem = shareAll.getFilteredItem();
    if (filteredItem == null) {
        filteredItem = new FilteredItem();
    }
    btnFilter.setOnClickListener(new OnClickListener() {

        @SuppressLint("NewApi")
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            // TODO put the id for each
            String filteredStringUrl = "";

            if (filteredItem.isFilteredItemEmpty()) {
                filteredStringUrl = "-1/-1/-1/-1/-1/-1/";
            } else {
                ArrayList<String> byBodyPartItem = filteredItem
                        .getByBodyPartItem();
                ArrayList<String> byEquipmentItem = filteredItem
                        .getByEquipmentItem();
                ArrayList<String> byLevelItem = filteredItem
                        .getByLevelItem();
                ArrayList<String> byMuscleGroupItem = filteredItem
                        .getByMuscleGroupItem();
                ArrayList<String> byObjectivesItem = filteredItem
                        .getByObjectivesItem();
                ArrayList<String> bySportItem = filteredItem
                        .getBySportItem();

                if (!byBodyPartItem.isEmpty()) {
                    for (int i = 0; i < byBodyPartItem.size(); i++) {

                        if (i > 0 && !filteredStringUrl.isEmpty()) {
                            filteredStringUrl = filteredStringUrl + ",";
                        }
                        List<exercise_BodyParts> allExerciseBodyPart = DataController
                                .getInstance(_context)
                                .getAllExerciseBodyPart();
                        if (!allExerciseBodyPart.isEmpty()) {
                            for (int j = 0; j < allExerciseBodyPart.size(); j++) {

                                if (allExerciseBodyPart.get(j)
                                        .getBodyPartName()
                                        .contains(byBodyPartItem.get(i))) {

                                    filteredStringUrl = filteredStringUrl
                                            + allExerciseBodyPart.get(j)
                                                    .getExerciseID()
                                                    .toString();
                                }
                            }
                        }

                    }
                } else {
                    filteredStringUrl = filteredStringUrl + "-1/";
                }
                if (!byEquipmentItem.isEmpty()) {
                    for (int i = 0; i < byEquipmentItem.size(); i++) {

                        if (i > 0) {
                            filteredStringUrl = filteredStringUrl + ",";
                        }
                        List<exercise_Equipments> allExerciseEquipment = DataController
                                .getInstance(_context)
                                .getAllExerciseEquipment();
                        if (!allExerciseEquipment.isEmpty()) {
                            for (int j = 0; j < allExerciseEquipment.size(); j++) {

                                if (allExerciseEquipment.get(j)
                                        .getEquipmentName()
                                        .contains(byEquipmentItem.get(i))) {

                                    filteredStringUrl = filteredStringUrl
                                            + allExerciseEquipment.get(j)
                                                    .getExerciseID()
                                                    .toString();
                                }
                            }
                        }

                    }
                } else {
                    filteredStringUrl = filteredStringUrl + "-1/";
                }
                if (!byLevelItem.isEmpty()) {
                    for (int i = 0; i < byLevelItem.size(); i++) {
                        if (i > 0) {
                            filteredStringUrl = filteredStringUrl + ",";
                        }

                        List<exercise> allExerciseLevel = DataController
                                .getInstance(_context).getAllexercises();
                        if (!allExerciseLevel.isEmpty()) {
                            for (int j = 0; j < allExerciseLevel.size(); j++) {

                                if (allExerciseLevel.get(j)
                                        .getExerciseLevel()
                                        .equals(byLevelItem.get(i))) {

                                    filteredStringUrl = filteredStringUrl
                                            + allExerciseLevel.get(j)
                                                    .getId().toString();
                                }
                            }
                        }
                    }
                } else {
                    filteredStringUrl = filteredStringUrl + "-1/";
                }
                if (!byMuscleGroupItem.isEmpty()) {
                    for (int i = 0; i < byMuscleGroupItem.size(); i++) {
                        if (i > 0) {
                            filteredStringUrl = filteredStringUrl + ",";
                        }

                        List<exercise_MuscleGroup> allExerciseMuscleGroupItem = DataController
                                .getInstance(_context)
                                .getallExerciseMuscleGroupItem();
                        if (!allExerciseMuscleGroupItem.isEmpty()) {
                            for (int j = 0; j < allExerciseMuscleGroupItem
                                    .size(); j++) {

                                if (allExerciseMuscleGroupItem.get(j)
                                        .getMusclegroupName()
                                        .contains(byMuscleGroupItem.get(i))) {

                                    filteredStringUrl = filteredStringUrl
                                            + allExerciseMuscleGroupItem
                                                    .get(j).getExerciseID()
                                                    .toString();
                                }
                            }
                        }
                    }
                } else {
                    filteredStringUrl = filteredStringUrl + "-1/";
                }
                if (!byObjectivesItem.isEmpty()) {
                    for (int i = 0; i < byObjectivesItem.size(); i++) {
                        if (i > 0) {
                            filteredStringUrl = filteredStringUrl + ",";
                        }
                        List<exercise_Objectives> allExerciseObjectivesItem = DataController
                                .getInstance(_context)
                                .getallExerciseObjectivesItem();
                        if (!allExerciseObjectivesItem.isEmpty()) {
                            for (int j = 0; j < allExerciseObjectivesItem
                                    .size(); j++) {

                                if (allExerciseObjectivesItem.get(j)
                                        .getObjectiveName()
                                        .contains(byObjectivesItem.get(i))) {

                                    filteredStringUrl = filteredStringUrl
                                            + allExerciseObjectivesItem
                                                    .get(j).getExerciseID()
                                                    .toString();
                                }
                            }
                        }
                    }
                } else {
                    filteredStringUrl = filteredStringUrl + "-1/";
                }
                if (!bySportItem.isEmpty()) {
                    for (int i = 0; i < bySportItem.size(); i++) {
                        if (i > 0) {
                            filteredStringUrl = filteredStringUrl + ",";
                        }
                        List<exercise_Sports> allExerciseSportItem = DataController
                                .getInstance(_context)
                                .getallExerciseSportItem();
                        if (!allExerciseSportItem.isEmpty()) {
                            for (int j = 0; j < allExerciseSportItem.size(); j++) {

                                if (allExerciseSportItem.get(j)
                                        .getSportName()
                                        .contains(bySportItem.get(i))) {

                                    filteredStringUrl = filteredStringUrl
                                            + allExerciseSportItem.get(j)
                                                    .getExerciseID()
                                                    .toString();
                                }
                            }
                        }
                    }
                } else {
                    filteredStringUrl = filteredStringUrl + "-1/";
                }
            }
            filteredStringUrl.trim();
            Intent intent = new Intent(_context, ExerciseActivity.class);
            intent.putExtra("filteredUrl", url + filteredStringUrl);
            _context.startActivity(intent);

        }
    });

    txtListChild.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            ((CheckedTextView) v).toggle();
            //
            String groupParetItem = _listDataHeader.get(groupPosition);
            String childItem = _listDataChild.get(groupParetItem).get(
                    childPosition);
            Toast.makeText(
                    _context,
                    groupParetItem
                            + " : "
                            + _listDataChild.get(groupParetItem).get(
                                    childPosition), Toast.LENGTH_SHORT)
                    .show();

            if (groupParetItem.equals(BY_BODY_PART)) {
                filteredItem.setByBodyPartItem(childItem);
            }
            if (groupParetItem.equals(BY_OBJECTIVES)) {
                filteredItem.setByObjectivesItem(childItem);
            }
            if (groupParetItem.equals(BY_MUSCLE_GROUP)) {
                filteredItem.setByMuscleGroupItem(childItem);
            }
            if (groupParetItem.equals(BY_EQUIPMENT)) {
                filteredItem.setByEquipmentItem(childItem);
            }
            if (groupParetItem.equals(BY_SPORTS)) {
                filteredItem.setBySportItem(childItem);
            }
            if (groupParetItem.equals(BY_LEVEL)) {
                filteredItem.setByLevelItem(childItem);
            }
            shareAll.setFilteredItem(filteredItem);
        }
    });
    txtListChild.setText(childText);
    return convertView;
}

1 个答案:

答案 0 :(得分:1)

这是因为listview重用它是节省内存的视图。

建议的解决方案: 您必须在数组中保存所选复选框项的位置,然后根据数组检查或取消选中getChildView中的视图