Reputation: 115
I have 16 checkboxes and i am dynamically trying to get the once that are checked i have numbered them from checkBox1-checkbox16 in my layout.
The code i am using is as below
package com.example.test;
import android.R.string;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Compare1 extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.commac1);
// setdesign();
checkbox();
}
private void checkbox() {
// TODO Auto-generated method stub
for(int a=1;a<17;a++){
String cb="checkBox"+a;
int id=getResources().getIdentifier(cb, "id", getPackageName());
CheckBox cb1=(CheckBox) findViewById(id);
if(cb1.isChecked()){
Toast.makeText(this, "you selected" +cb, Toast.LENGTH_SHORT).show();
}}}}
This does not seem to be working.Can someone help me with this. Thanks
Upvotes: 0
Views: 76
Reputation: 927
Using IsabelHM suggestion, you would end up with you would end up with something looking like this:
package com.example.test;
import android.R.string;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Compare1 extends Activity implements CompoundButton.OnCheckedChangeListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.commac1);
// setdesign();
checkbox();
}
private void checkbox() {
// TODO Auto-generated method stub
for(int a=1;a<17;a++){
String cb="checkBox"+a;
int id=getResources().getIdentifier(cb, "id", getPackageName());
CheckBox cb1=(CheckBox) findViewById(id);
cb1.setOnCheckedChangeListener(this);
}
}
/// Will be called by all the checkboxes
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.checkbox1:
//your toast
break;
case R.id.checkbox2:
//your toast
break;
case R.id.checkbox3:
//your toast
break;
...
case R.id.checkbox16:
//your toast
break;
}
}
}
Upvotes: 0
Reputation: 4185
Add this to your class (based on @FMontano's suggestion)
public class Compare1 extends Activity implements CompoundButton.OnCheckedChangeListener {
...
and then try this method 1:
CheckBox cb1 = findViewById(id);
check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//Toast.makeText ...
}
});
Or this method 2:
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.checkbox1:
//your toast
break;
case R.id.checkbox2:
//your toast
break;
case R.id.checkbox3:
//your toast
break;
}
}
and call it to use like this
CheckBox cb1 = findViewById(R.id.checkbox1);
cb1.setOnCheckedChangeListener(this);
Upvotes: 1