id的ExpandableListView复选框选择已点击复选框

id的ExpandableListView复选框选择已点击复选框

本文介绍了Android的ExpandableListView复选框选择已点击复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ExpandableListViews 一个快速的问题,并使用复选框。我下方会显示出当前code。我现在有一个 ExpandableListView 在每个子行的复选框。

这些复选框可以选择等,但问题是我有很多孩子在我的 ExpandableListView 。所以,问题是,当我检查中,它表明一个以上的复选框被选中。所以,如果我检查的第一个孩子复选框,这也表明了第六或第七的孩子也被选中。

 包com.WebGems.MW3CustomClasses;

进口的java.util.ArrayList;
进口的java.util.HashMap;
进口的java.util.Map;

进口android.app.Activity;
进口android.content.Context;
进口android.graphics.drawable.Drawable;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.CheckBox;
进口android.widget.ExpandableListView;
进口android.widget.ExpandableListView.OnChildClickListener;
进口android.widget.ImageView;
进口android.widget.SimpleExpandableListAdapter;
进口android.widget.TextView;

公共类PrimaryWeapon延伸活动{
    ExpandableListView listPrimaryWeapon;


    //攻击包的ArrayList

    ArrayList的assaultPackage =新的ArrayList();




    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.primary_weapon);
        listPrimaryWeapon =(ExpandableListView)findViewById(R.id.listView1);

        捆绑额外= getIntent()getExtras()。
        字符串类型= extras.getString(类);


        如果(type.equals(攻击)){
            showAssaultPackageList();
        }


    }
    私人无效showAssaultPackageList(){
        // TODO自动生成方法存根

        assaultPackage.add(无人机);
        assaultPackage.add(爱心包裹);
        assaultPackage.add(I.M.S。);
        assaultPackage.add(preDATOR导弹);
        assaultPackage.add(步哨枪);
        assaultPackage.add(preCISION空袭);
        assaultPackage.add(攻击直升机);
        assaultPackage.add(扫射RUN);
        assaultPackage.add(AH-6监工);
        assaultPackage.add(收割);
        assaultPackage.add(突击无人机);
        assaultPackage.add(AC-130);
        assaultPackage.add(PAVE LOW);
        assaultPackage.add(重量级);
        assaultPackage.add(OS preY炮手);



        //构造扩展列表
        最终的字符串名称=名称;
        最终的字符串图像=形象;
        最终的字符串数据=数据;
        最终的串号=号;

        最后LayoutInflater layoutInflater =(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        最后的ArrayList< HashMap的<字符串,字符串>> headerData =新的ArrayList< HashMap的<字符串,字符串>>();

        最后的HashMap<字符串,字符串>组别1 =新的HashMap<字符串,字符串>();
        group1.put(名字,突击包);
        headerData.add(1组);

        最后的ArrayList< ArrayList的< HashMap的<字符串,对象>>> childData =新的ArrayList< ArrayList的< HashMap的<字符串,对象>>>();

        最后的ArrayList< HashMap的<字符串,对象>> group1data =新的ArrayList< HashMap的<字符串,对象>>();
        childData.add(group1data);

        INT assaultPackageCounter = 0;
        对于(INT臂= 0;臂LT; assaultPackage.size(); ++ ARM){
            最后的HashMap<字符串,对象> assaultPackageMap =新的HashMap<字符串,对象>();
            assaultPackageMap.put(姓名,assaultPackage.get(ARM));
            如果(assaultPackageCounter == 0){
                assaultPackageMap.put(IMAGE,getResources()getDrawable(R.drawable.uav));
                assaultPackageMap.put(DATA无人机说明);
                assaultPackageMap.put(NUMBER,getResources()getDrawable(R.drawable.three));
            }
            如果(assaultPackageCounter == 1){
                assaultPackageMap.put(IMAGE,getResources()getDrawable(R.drawable.carepackage));
                assaultPackageMap.put(DATA,爱心包裹说明);
                assaultPackageMap.put(NUMBER,getResources()getDrawable(R.drawable.four));
            }
            如果(assaultPackageCounter == 2){
                assaultPackageMap.put(IMAGE,getResources()getDrawable(R.drawable.ims));
                assaultPackageMap.put(DATA,IMS描述);
                assaultPackageMap.put(NUMBER,getResources()getDrawable(R.drawable.five));
            }
            如果(assaultPackageCounter == 3){
                assaultPackageMap.put(IMAGE,getResources()getDrawable(R.drawable predatormissile));
                assaultPackageMap.put(DATA,preDATOR导弹说明);
                assaultPackageMap.put(NUMBER,getResources()getDrawable(R.drawable.five));
            }
            如果(assaultPackageCounter == 4){
                assaultPackageMap.put(IMAGE,getResources()getDrawable(R.drawable.sentrygun));
                assaultPackageMap.put(DATA,步哨枪说明);
                assaultPackageMap.put(NUMBER,getResources()getDrawable(R.drawable.five));
            }
            如果(assaultPackageCounter == 5){
                assaultPackageMap.put(IMAGE,getResources()getDrawable(R.drawable precisionairstrike));
                assaultPackageMap.put(DATA,preCISION空袭说明);
                assaultPackageMap.put(NUMBER,getResources()getDrawable(R.drawable.six));
            }
            如果(assaultPackageCounter == 6){
                assaultPackageMap.put(IMAGE,getResources()getDrawable(R.drawable.attackhelicopter));
                assaultPackageMap.put(DATA,攻击直升机说明);
                assaultPackageMap.put(NUMBER,getResources()getDrawable(R.drawable.seven));
            }
            如果(assaultPackageCounter == 7){
                assaultPackageMap.put(IMAGE,getResources()getDrawable(R.drawable.straferun));
                assaultPackageMap.put(DATA扫射RUN说明);
                assaultPackageMap.put(NUMBER,getResources()getDrawable(R.drawable.nine));
            }
            如果(assaultPackageCounter == 8){
                assaultPackageMap.put(IMAGE,getResources()getDrawable(R.drawable.ahsixoverwatch));
                assaultPackageMap.put(DATA,AH-6监工说明);
                assaultPackageMap.put(NUMBER,getResources()getDrawable(R.drawable.nine));
            }
            如果(assaultPackageCounter == 9){
                assaultPackageMap.put(IMAGE,getResources()getDrawable(R.drawable.reaper));
                assaultPackageMap.put(DATA,REAPER说明);
                assaultPackageMap.put(NUMBER,getResources()getDrawable(R.drawable.nine));
            }
            如果(assaultPackageCounter == 10){
                assaultPackageMap.put(IMAGE,getResources()getDrawable(R.drawable.assaultdrone));
                assaultPackageMap.put(DATA,突击无人机说明);
                assaultPackageMap.put(NUMBER,getResources()getDrawable(R.drawable.ten));
            }
            如果(assaultPackageCounter == 11){
                assaultPackageMap.put(IMAGE,getResources()getDrawable(R.drawable.aconethreezero));
                assaultPackageMap.put(DATA,AC-130说明);
                assaultPackageMap.put(NUMBER,getResources()getDrawable(R.drawable.twelve));
            }
            如果(assaultPackageCounter == 12){
                assaultPackageMap.put(IMAGE,getResources()getDrawable(R.drawable.pavelow));
                assaultPackageMap.put(DATA,PAVE不足说明);
                assaultPackageMap.put(NUMBER,getResources()getDrawable(R.drawable.twelve));
            }
            如果(assaultPackageCounter == 13){
                assaultPackageMap.put(IMAGE,getResources()getDrawable(R.drawable.juggernaut));
                assaultPackageMap.put(DATA重量级说明);
                assaultPackageMap.put(NUMBER,getResources()getDrawable(R.drawable.fifteen));
            }
            如果(assaultPackageCounter == 14){
                assaultPackageMap.put(IMAGE,getResources()getDrawable(R.drawable.os preygunner));
                assaultPackageMap.put(DATA,OS preY炮手说明);
                assaultPackageMap.put(NUMBER,getResources()getDrawable(R.drawable.seventeen));
            }
            assaultPackageCounter ++;
            group1data.add(assaultPackageMap);
        }
        assaultPackageCounter = 0;

        //两组设置一些示例数据
        / *的for(int i = 0;我小于10 ++我){
            最后的HashMap<字符串,对象>图=新的HashMap<字符串,对象>();
            map.put(NAME,儿童+ I);
            //map.put(DATA,数据+ I);
            map.put(IMAGE,getResources()getDrawable((我%3 == 0 R.drawable.arrowright:R.drawable.arrowdown)));
            (?I%2 == 0 group1data:group2data)。新增(图)
        } * /

        listPrimaryWeapon.setAdapter(新SimpleExpandableListAdapter(
                本,
                headerData,
                R.layout.group_row,
                新的String [] {名,数据} //数据的名称
                新的INT [] {} R.id.groupname,//文本字段来填充字段数据
                childData,
                0,
                空值,
                新的INT [] {}
            ){
                @覆盖
                公共查看getChildView(INT groupPosition,诠释childPosition,布尔isLastChild,查看convertView,ViewGroup中父){
                    最终的视图V = super.getChildView(groupPosition,childPosition,isLastChild,convertView,父母);

                    //在这里填写您的自定义视图
                    ((TextView中)v.findViewById(R.id.name))的setText((字符串)((地图<字符串,对象>)getChild(groupPosition,childPosition))得到(名));
                    ((TextView中)v.findViewById(R.id.textView1))的setText((字符串)((地图<字符串,对象>)getChild(groupPosition,childPosition))得到(DATA));
                    ((ImageView的)v.findViewById(R.id.image))setImageDrawable。((可绘制)((地图<字符串,对象>)getChild(groupPosition,childPosition))得到(图片));
                    ((ImageView的)v.findViewById(R.id.imageView1))setImageDrawable。((可绘制)((地图<字符串,对象>)getChild(groupPosition,childPosition))得到(NUMBER));
                    复选框CB =(复选框)v.findViewById(R.id.checkBox1);
                    cb.setClickable(假);
                    返回伏;
                }

                @覆盖
                公共查看newChildView(布尔isLastChild,ViewGroup中父){
                     返回layoutInflater.inflate(R.layout.strikepackagelayout,空,假);
                }
            }
        );
        ExpandableListView名单=(ExpandableListView)findViewById(R.id.listView1);
        list.setChoiceMode(ExpandableListView.CHOICE_MODE_MULTIPLE);
        list.setOnChildClickListener(新OnChildClickListener(){
            公共布尔onChildClick(ExpandableListView父,视图V,
                    INT groupPosition,诠释childPosition,长ID){
                复选框CB =(复选框)v.findViewById(R.id.checkBox1);
                如果(CB!= NULL){
                    cb.toggle();
                }
                Log.d(类,组:+ groupPosition +,儿童:+ childPosition);
                的System.out.println(本集团+ groupPosition +,儿童:+ childPosition);
                返回true;
            }
        });
        listPrimaryWeapon.expandGroup(0);




    }
}
 

解决方案

可扩展列表视图每次移动时回收的观点。所以你需要保存每个视图的状态。

1>设置不同的ID为每个复选框。类似的东西      checkBox.setId(groupPosition + 1);

2>添加选中的复选框ID到哈希地图对象像

  checkBox.setOnCheckedChangeListener(
新CompoundButton.OnCheckedChangeListener(){

                                    @覆盖
                                    公共无效onCheckedChanged(如果(checkBox.isChecked()){
                                            mapViewState.put(checkBox.getId(),
                                                    checkBoxSelected);
                                        } 其他 {
                                            mapViewState.remove(复选框
                                                    .getId());
                                        }

                                    }
                                });
 

,然后加载从HashMap中的复选框的状态,或者重复它来获得所选择的值。

I have a quick question on ExpandableListViews and using CheckBoxes. I will show the current code below. I currently have a ExpandableListView with a checkbox in each child row.

These checkboxes can be selected and such but the problem is I have many children in my ExpandableListView. So the problem is that when I check on box, it shows that more than one checkbox is selected. So if I check the first child checkbox it also shows that the 6th or 7th child is also selected.

package com.WebGems.MW3CustomClasses;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;

public class PrimaryWeapon extends Activity {
    ExpandableListView listPrimaryWeapon;


    //Strike Package ArrayLists

    ArrayList assaultPackage = new ArrayList();




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.primary_weapon);
        listPrimaryWeapon = (ExpandableListView)findViewById(R.id.listView1);

        Bundle extras = getIntent().getExtras();
        String type = extras.getString("type");


        if(type.equals("assault")) {
            showAssaultPackageList();
        }


    }
    private void showAssaultPackageList() {
        // TODO Auto-generated method stub

        assaultPackage.add("UAV");
        assaultPackage.add("CARE PACKAGE");
        assaultPackage.add("I.M.S.");
        assaultPackage.add("PREDATOR MISSILE");
        assaultPackage.add("SENTRY GUN");
        assaultPackage.add("PRECISION AIRSTRIKE");
        assaultPackage.add("ATTACK HELICOPTER");
        assaultPackage.add("STRAFE RUN");
        assaultPackage.add("AH-6 OVERWATCH");
        assaultPackage.add("REAPER");
        assaultPackage.add("ASSAULT DRONE");
        assaultPackage.add("AC-130");
        assaultPackage.add("PAVE LOW");
        assaultPackage.add("JUGGERNAUT");
        assaultPackage.add("OSPREY GUNNER");



        // Construct Expandable List
        final String NAME = "name";
        final String IMAGE = "image";
        final String DATA = "data";
        final String NUMBER = "number";

        final LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final ArrayList<HashMap<String, String>> headerData = new ArrayList<HashMap<String, String>>();

        final HashMap<String, String> group1 = new HashMap<String, String>();
        group1.put(NAME, "Assault Package");
        headerData.add( group1 );

        final ArrayList<ArrayList<HashMap<String, Object>>> childData = new ArrayList<ArrayList<HashMap<String, Object>>>();

        final ArrayList<HashMap<String, Object>> group1data = new ArrayList<HashMap<String, Object>>();
        childData.add(group1data);

        int assaultPackageCounter = 0;
        for(int arm = 0; arm < assaultPackage.size(); ++arm) {
            final HashMap<String, Object> assaultPackageMap = new HashMap<String, Object>();
            assaultPackageMap.put(NAME, assaultPackage.get(arm));
            if(assaultPackageCounter == 0) {
                assaultPackageMap.put(IMAGE, getResources().getDrawable(R.drawable.uav));
                assaultPackageMap.put(DATA, "UAV DESCRIPTION");
                assaultPackageMap.put(NUMBER, getResources().getDrawable(R.drawable.three));
            }
            if(assaultPackageCounter == 1) {
                assaultPackageMap.put(IMAGE, getResources().getDrawable(R.drawable.carepackage));
                assaultPackageMap.put(DATA, "CARE PACKAGE DESCRIPTION");
                assaultPackageMap.put(NUMBER, getResources().getDrawable(R.drawable.four));
            }
            if(assaultPackageCounter == 2) {
                assaultPackageMap.put(IMAGE, getResources().getDrawable(R.drawable.ims));
                assaultPackageMap.put(DATA, "IMS DESCRIPTION");
                assaultPackageMap.put(NUMBER, getResources().getDrawable(R.drawable.five));
            }
            if(assaultPackageCounter == 3) {
                assaultPackageMap.put(IMAGE, getResources().getDrawable(R.drawable.predatormissile));
                assaultPackageMap.put(DATA, "PREDATOR MISSILE DESCRIPTION");
                assaultPackageMap.put(NUMBER, getResources().getDrawable(R.drawable.five));
            }
            if(assaultPackageCounter == 4) {
                assaultPackageMap.put(IMAGE, getResources().getDrawable(R.drawable.sentrygun));
                assaultPackageMap.put(DATA, "SENTRY GUN DESCRIPTION");
                assaultPackageMap.put(NUMBER, getResources().getDrawable(R.drawable.five));
            }
            if(assaultPackageCounter == 5) {
                assaultPackageMap.put(IMAGE, getResources().getDrawable(R.drawable.precisionairstrike));
                assaultPackageMap.put(DATA, "PRECISION AIRSTRIKE DESCRIPTION");
                assaultPackageMap.put(NUMBER, getResources().getDrawable(R.drawable.six));
            }
            if(assaultPackageCounter == 6) {
                assaultPackageMap.put(IMAGE, getResources().getDrawable(R.drawable.attackhelicopter));
                assaultPackageMap.put(DATA, "ATTACK HELICOPTER DESCRIPTION");
                assaultPackageMap.put(NUMBER, getResources().getDrawable(R.drawable.seven));
            }
            if(assaultPackageCounter == 7) {
                assaultPackageMap.put(IMAGE, getResources().getDrawable(R.drawable.straferun));
                assaultPackageMap.put(DATA, "STRAFE RUN DESCRIPTION");
                assaultPackageMap.put(NUMBER, getResources().getDrawable(R.drawable.nine));
            }
            if(assaultPackageCounter == 8) {
                assaultPackageMap.put(IMAGE, getResources().getDrawable(R.drawable.ahsixoverwatch));
                assaultPackageMap.put(DATA, "AH-6 OVERWATCH DESCRIPTION");
                assaultPackageMap.put(NUMBER, getResources().getDrawable(R.drawable.nine));
            }
            if(assaultPackageCounter == 9) {
                assaultPackageMap.put(IMAGE, getResources().getDrawable(R.drawable.reaper));
                assaultPackageMap.put(DATA, "REAPER DESCRIPTION");
                assaultPackageMap.put(NUMBER, getResources().getDrawable(R.drawable.nine));
            }
            if(assaultPackageCounter == 10) {
                assaultPackageMap.put(IMAGE, getResources().getDrawable(R.drawable.assaultdrone));
                assaultPackageMap.put(DATA, "ASSAULT DRONE DESCRIPTION");
                assaultPackageMap.put(NUMBER, getResources().getDrawable(R.drawable.ten));
            }
            if(assaultPackageCounter == 11) {
                assaultPackageMap.put(IMAGE, getResources().getDrawable(R.drawable.aconethreezero));
                assaultPackageMap.put(DATA, "AC-130 DESCRIPTION");
                assaultPackageMap.put(NUMBER, getResources().getDrawable(R.drawable.twelve));
            }
            if(assaultPackageCounter == 12) {
                assaultPackageMap.put(IMAGE, getResources().getDrawable(R.drawable.pavelow));
                assaultPackageMap.put(DATA, "PAVE LOW DESCRIPTION");
                assaultPackageMap.put(NUMBER, getResources().getDrawable(R.drawable.twelve));
            }
            if(assaultPackageCounter == 13) {
                assaultPackageMap.put(IMAGE, getResources().getDrawable(R.drawable.juggernaut));
                assaultPackageMap.put(DATA, "JUGGERNAUT DESCRIPTION");
                assaultPackageMap.put(NUMBER, getResources().getDrawable(R.drawable.fifteen));
            }
            if(assaultPackageCounter == 14) {
                assaultPackageMap.put(IMAGE, getResources().getDrawable(R.drawable.ospreygunner));
                assaultPackageMap.put(DATA, "OSPREY GUNNER DESCRIPTION");
                assaultPackageMap.put(NUMBER, getResources().getDrawable(R.drawable.seventeen));
            }
            assaultPackageCounter++;
            group1data.add(assaultPackageMap);
        }
        assaultPackageCounter = 0;

        // Set up some sample data in both groups
        /*for( int i=0; i<10; ++i) {
            final HashMap<String, Object> map = new HashMap<String,Object>();
            map.put(NAME, "Child " + i );
            //map.put(DATA, "Data " + i);
            map.put(IMAGE, getResources().getDrawable((i%3==0? R.drawable.arrowright : R.drawable.arrowdown)));
            ( i%2==0 ? group1data : group2data ).add(map);
        }*/

        listPrimaryWeapon.setAdapter( new SimpleExpandableListAdapter(
                this,
                headerData,
                R.layout.group_row,
                new String[] { NAME, DATA },    // the names of the data
                new int[] { R.id.groupname },   // the text field to populate with the field data
                childData,
                0,
                null,
                new int[] {}
            ) {
                @Override
                public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
                    final View v = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);

                    // Populate your custom view here
                    ((TextView)v.findViewById(R.id.name)).setText( (String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get(NAME) );
                    ((TextView)v.findViewById(R.id.textView1)).setText( (String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get(DATA) );
                    ((ImageView)v.findViewById(R.id.image)).setImageDrawable( (Drawable) ((Map<String,Object>)getChild(groupPosition, childPosition)).get(IMAGE) );
                    ((ImageView)v.findViewById(R.id.imageView1)).setImageDrawable( (Drawable) ((Map<String,Object>)getChild(groupPosition, childPosition)).get(NUMBER) );
                    CheckBox cb = (CheckBox)v.findViewById( R.id.checkBox1 );
                    cb.setClickable(false);
                    return v;
                }

                @Override
                public View newChildView(boolean isLastChild, ViewGroup parent) {
                     return layoutInflater.inflate(R.layout.strikepackagelayout, null, false);
                }
            }
        );
        ExpandableListView list = (ExpandableListView) findViewById(R.id.listView1);
        list.setChoiceMode(ExpandableListView.CHOICE_MODE_MULTIPLE);
        list.setOnChildClickListener(new OnChildClickListener(){
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                CheckBox cb = (CheckBox)v.findViewById( R.id.checkBox1 );
                if(cb != null){
                    cb.toggle();
                }
                Log.d("type", "Group: " + groupPosition + ", Child: " + childPosition);
                System.out.println("Group:"+groupPosition+", Child: "+childPosition);
                return true;
            }
        });
        listPrimaryWeapon.expandGroup(0);




    }
}
解决方案

As Expandable list view Recycle the view each time you move. so you need to save the state of each views.

1> set different id for each checkbox. something like that checkBox.setId(groupPosition + 1);

2> add the selected checkbox id to a hash map object like

checkBox.setOnCheckedChangeListener(
new CompoundButton.OnCheckedChangeListener() {

                                    @Override
                                    public void onCheckedChanged(if (checkBox.isChecked()) {
                                            mapViewState.put(checkBox.getId(),
                                                    checkBoxSelected);
                                        } else {
                                            mapViewState.remove(checkBox
                                                    .getId());
                                        }

                                    }
                                });

and then load the status of the check box from HashMap, or iterate it to get the selected values.

这篇关于Android的ExpandableListView复选框选择已点击复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:35