Android : Get selected checkbox in listview

on Tuesday, October 28, 2014


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

0 comments:

Post a Comment