简体   繁体   中英

Get selected checkbox in listview

I'm trying to get the selected checkbox in a listview. I've been searching in the web, but I always found examples using the "Holder", but I'm not using it. I asked my teacher too, but also didn't know why isn't this working.

    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;
    }
}

I found the solution, changing this

sp=items.get(i);

and

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);
                }
            }
        });

for this

sp=getItem(i);

and

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

EDIT: I had to add this too in the getView

final int pos=i;

The solution is simpler then what you mention. You don't need to listen for both clicks and check changes. And using getItem or items.get(i) does not matter (they do the same thing).

All you need to do is update the check state of a SportItem every time the associated checkbox changes check state.

Your OnCheckedChangeListener already listens for changes in check state.

Modify your OnCheckedChangeListener to:

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.
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM