Reputation: 3139
I add my checkboxes dynamically like this:
public void addFiles()
{
LinearLayout layout = (LinearLayout) findViewById(R.id.filesList);
if(!FileManagerActivity.finalAttachFiles.isEmpty())
{
for (final File file:FileManagerActivity.finalAttachFiles)
{
Log.i("what I've got", file.toString());
View line = new View(this);
line.setLayoutParams(new LayoutParams(1, LayoutParams.MATCH_PARENT));
line.setBackgroundColor(0xAA345556);
informationView= new CheckBox(this);
informationView.setTextColor(Color.BLACK);
informationView.setTextSize(16);
informationView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
informationView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.file_icon, 0, 0, 0);
informationView.setText(file.getName().toString());
layout.addView(informationView, 0);
layout.addView(line, 1);
layout.postInvalidate();
}
}
}
And I have a button, which onClick event should get the checkable state of those checkboxes and delete which were checked...How can I implement this?
Upvotes: 0
Views: 657
Reputation: 19250
your can add your checkboxes like:
for(int i=0;i<totalCB;i++){
CheckBox chB=new CheckBox(context);
...
chB.setId(i);
layout.add(...);// add checkbox to view
}
now,on click of button,
for(int i=0;i<totalCB;i++){
CheckBox cb=(CheckBox)findViewById(i);
boolean checked=cd.isChecked();// status of checkbox
if(checked){
// perform action
}
}
Upvotes: 2
Reputation: 3846
You can set id for the checkbox using informationView.setId(someUniqueInt)
. and then find a text box whereever you want in the activity by
CheckBox checkBox = (CheckBox) findViewById(someUniqueInt)
And then use
if (checkBox.isChecked()) {
//do whatever you want here
}
Upvotes: 0
Reputation: 3727
for(int i=0;i<layout.getChildCount;i++){
if ( ((CheckBox) layout.getChildAt(i)).isChecked() )
//remove the Box
}
This should do the trick. You may have to alter the code if you have other Views in your layout or create a new layout which only hosts the checkboxes. The Code goes into the OnClick Method of you Buttons onclicklistener of course.
Upvotes: 1