列表视图中的选中复选框

时间:2014-10-28 15:19:25

标签: java android listview

我正在尝试在列表视图中获取所选复选框。我一直在网上搜索,但我总是找到使用“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;
    }
}

2 个答案:

答案 0 :(得分:0)

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

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;

答案 1 :(得分:0)

解决方案比你提到的更简单。您不需要同时收听点击次数和检查更改。使用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.
            }
        });