【问题标题】:Get selected checkbox in listview在列表视图中获取选中的复选框
【发布时间】:2014-10-28 15:19:25
【问题描述】:

我正在尝试在列表视图中获取选中的复选框。我一直在网上搜索,但我总是找到使用“Holder”的示例,但我没有使用它。我也问过我的老师,但也不知道为什么这不起作用。

    public class MyActivity extends Activity {

        String s;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my);

            ListView list=(ListView)findViewById(R.id.listView);
            Button checkBtn=(Button)findViewById(R.id.button);
            final ArrayList<SportItem> sportArray=new ArrayList<SportItem>();

            SportItem sp;

            sp=new SportItem(getResources().getDrawable(R.drawable.baloncesto),"Basketball",false);
            sportArray.add(sp);
            sp=new SportItem(getResources().getDrawable(R.drawable.futbol),"Football",false);
            sportArray.add(sp);
            sp=new SportItem(getResources().getDrawable(R.drawable.judo),"Judo",false);
            sportArray.add(sp);
            sp=new SportItem(getResources().getDrawable(R.drawable.atletismo),"Athletism",false);
            sportArray.add(sp);
            sp=new SportItem(getResources().getDrawable(R.drawable.tenis),"Tennis",false);
            sportArray.add(sp);

            SportAdapter spadapter=new SportAdapter(this, sportArray);

            list.setAdapter(spadapter);

            checkBtn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    s="";
                    for (SportItem ar:sportArray){

                        if(ar.isCheck()){
                            s+=ar.getName()+", ";
                        }
                    }

                    Toast.makeText(getApplicationContext(), s,
                            Toast.LENGTH_SHORT).show();        
                }
            });
        }

    }

    public class SportAdapter extends BaseAdapter {

        protected Activity activity;
        protected ArrayList<SportItem> items;
        CheckBox chk;
        SportItem sp;

        public SportAdapter(Activity activity, ArrayList<SportItem> items) {
            this.activity = activity;
            this.items = items;
        }

        @Override
        public int getCount(){
            return items.size();
        }

        @Override
        public Object getItem(int i) {
            return items.get(i);
        }

        @Override
        public long getItemId(int i) {
            return items.get(i).getId();
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View v=view;

            if(view==null){
                LayoutInflater linf=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v=linf.inflate(R.layout.item_lista,null);
            }

            sp=items.get(i);
            ImageView img=(ImageView)v.findViewById(R.id.imageView);
            img.setImageDrawable(sp.getImage());

            TextView name=(TextView)v.findViewById(R.id.textView);
            name.setText(sp.getName());

            chk=(CheckBox)v.findViewById(R.id.checkBox);
            chk.setChecked(sp.isCheck());

            chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if(!chk.isChecked()) {
                        sp.setCheck(true);
                        chk.setChecked(sp.isCheck());
                    }
                    else {
                        sp.setCheck(false);
                        chk.setChecked(sp.isCheck());
                    }
                }
            });

            return v;
        }

    }


public class SportItem {

    protected Drawable image;
    protected String name;
    protected boolean check;
    protected long id;

    public SportItem(Drawable image, String name, boolean check) {
        super();
        this.image = image;
        this.name = name;
        this.check = check;
    }

    public SportItem(Drawable image, String name, boolean check, long id) {
        super();
        this.image = image;
        this.name = name;
        this.check = check;
        this.id = id;
    }

    public Drawable getImage() {
        return image;
    }

    public void setImage(Drawable image) {
        this.image = image;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isCheck() {
        return check;
    }

    public void setCheck(boolean check) {
        this.check = check;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
}

【问题讨论】:

  • 使用稀疏布尔数组。
  • 检查This链接,也许你会得到一些解决方法。

标签: java android listview


【解决方案1】:

解决方案比您提到的要简单。您不需要同时监听点击和检查更改。并且使用 getItem 或 items.get(i) 并不重要(它们做同样的事情)。

您需要做的就是在每次关联的复选框更改检查状态时更新 SportItem 的检查状态。

您的 OnCheckedChangeListener 已经在监听检查状态的变化。

将您的 OnCheckedChangeListener 修改为:

chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                sp.setCheck(b); //Updates the state with what is currently shown on screen.
            }
        });

【讨论】:

  • 首先,我忘了说我必须在 getView 上添加这一行:final int pos=i;,如果将 onCheckedChanged 函数内容更改为:SportItem spI=getItem(pos); spI.setCheck(b);跨度>
  • @Doruko,您是否尝试过返回原始答案并像我提到的那样修改已检查的更改侦听器?你甚至不需要点击监听器。
  • 是的,我已经尝试按照您说的做...但没有正常工作
【解决方案2】:

我找到了解决方案,正在改变这个

sp=items.get(i);

chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                if(!chk.isChecked()) {
                   sp.setCheck(true);
                }
                else {
                    sp.setCheck(false);
                    chk.setChecked(false);
                }
            }
        });

为此

sp=getItem(i);

chk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CheckBox cb=(CheckBox) view;
                SportItem spI=getItem(pos);
                spI.setCheck(cb.isChecked());
            }
        });

编辑: 我也必须在 getView 中添加这个

final int pos=i;

【讨论】:

    猜你喜欢
    • 2012-05-03
    • 2017-04-18
    • 2013-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-25
    • 2014-09-03
    相关资源
    最近更新 更多