Reputation: 133
I want to show the name of those multiple checkboxes that was checked. The problem is that when I check on any of them, only the last one checked is written on the textview. How can I resolve this?
I don't really understand how I will concatenate the names of those checkboxes.
public void ChkCommand(View v)
{
txtans = (EditText)findViewById(R.id.txtcon1);
if(v.getId() == R.id.chk1 && ((CheckBox)v).isChecked()){
chkClick = (CheckBox) findViewById(R.id.chk1);
}
else if(v.getId() == R.id.chk2 && ((CheckBox)v).isChecked()){
chkClick = (CheckBox) findViewById(R.id.chk2);
}
else if(v.getId() == R.id.chk3 && ((CheckBox)v).isChecked()){
chkClick = (CheckBox) findViewById(R.id.chk3);
}
String value = chkClick.getText().toString();
txtans.setText(value);
}
public void CloseConF(View v)
{
Intent intent = new Intent(this, SampComponents.class);
startActivity(intent);
finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.con_chk_samp, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_con_chk_samp,
container, false);
return rootView;
}
}
}
Upvotes: 1
Views: 1870
Reputation: 3263
Make value
a field
private String value = "";
then change
String value = chkClick.getText().toString();
to something as:
value = value + " / " + chkClick.getText().toString();
But it's also necessary to tweak a bit the logic for reading the status from the checkboxes, otherwise they will add text also while un-checking.
Upvotes: 1
Reputation: 39386
Each time you want to display the state of all your three checkboxes, you have to check all three:
String value = "";
if (((CheckBox) findViewById(R.id.chk1)).isChecked()) {
value += (CheckBox) findViewById(R.id.chk1).getText().toString();
}
if (((CheckBox) findViewById(R.id.chk2)).isChecked()) {
value += (CheckBox) findViewById(R.id.chk2).getText().toString();
}
if (((CheckBox) findViewById(R.id.chk3)).isChecked()) {
value += (CheckBox) findViewById(R.id.chk3).getText().toString();
}
txtans = (EditText)findViewById(R.id.txtcon1);
txtans.setText(value);
Then of course there would be some refactoring to do to get something cleaner, but that should get you started.
Upvotes: 3
Reputation: 28823
Try getting the checkbox text in every if else and append to a string value like this. chkBox1, chkBox2 and chkBox3 will be CheckBoxes in your java class.
public void ChkCommand(View v)
{
String value="";
txtans = (EditText)findViewById(R.id.txtcon1);
if(chkBox1.isChecked())
{
/*((CheckBox) findViewById(R.id.chk3)).setChecked(false);
((CheckBox) findViewById(R.id.chk2)).setChecked(false);*/
chkClick = (CheckBox) findViewById(R.id.chk1);
value += chkClick.getText().toString()+", ";
}
if(chkBox2.isChecked())
{
/*((CheckBox) findViewById(R.id.chk1)).setChecked(false);
((CheckBox) findViewById(R.id.chk3)).setChecked(false);*/
chkClick = (CheckBox) findViewById(R.id.chk2);
value += chkClick.getText().toString()+", ";
}
if(chkBox3.isChecked())
{/*
((CheckBox) findViewById(R.id.chk1)).setChecked(false);
((CheckBox) findViewById(R.id.chk2)).setChecked(false);*/
chkClick = (CheckBox) findViewById(R.id.chk3);
value += chkClick.getText().toString();
}
txtans.setText(value);
}
So you will get the result with all checked checkboxes' text. Hope this helps.
P.S: I just realized from @njzk2 's comment that you will need to changed else if
to if
in order to get all checkboxes's state.
Upvotes: 2
Reputation: 5543
Declare StringBuffer or StringBuilder member variables
StringBuffer buff = new StringBuffer();
Try this
buff.append(chkClick.getText().toString()+" ");
txtans.setText(buff.toString());
in place of
String value = chkClick.getText().toString();
txtans.setText(value);
It should work if your checkbox checking action is working correctly
Upvotes: 0