How to access String Created in an inner class - android

I've been doing a lot of research about this and I can't find an answer I can implement...
So I hope one of you can help me :)
Here is my problem: I create the String "dispo" in depending on how the switch is set, but I can't use this in my URL below...
(I tried to declare the String as final but it won't work...
Here is the code:
Switch toto = (Switch) findViewById(R.id.toto);
toto.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(isChecked){
String dispo = "yes";
}else{
String dispo = "no";
}
}
});
new RequestTask().execute("http:myURL" + dispo);
}
Thank you :)

You have to move the new RequestTask().execute("http:myURL" + dispo);inside the onCheckedChanged method.

Just create the the String 'dispo' before the listener. In your solution the String 'dispo' is only known/valid in the if/else statement (within the braces).
String dispo = null;
toto.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
dispo = "yes";
} else {
dispo = "no";
}
}
});
new RequestTask().execute("http:myURL" + dispo);

Thanks to all for your help.
I figured-out a way to do what I wanted^^
Here is the code:
Switch toto = (Switch) findViewById(R.id.toto);
toto.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {}
});
//check the current state and call the URL accordingly
if (toto.isChecked()) {
new RequestTask().execute("http:myURL/script.php&dispo=yes");
} else {
new RequestTask().execute("http:myURL/script.php&dispo=no");
}
}

Related

How to set checkbox to a custom text in string and save into database?

I am working on android. I have a checkbox in my app and I want to save a custom text against my checkbox when it is checked or not.
CheckBox atbSealedCheckBox;
String selectedAtbSealedValue ="";
atbSealedCheckBox = (CheckBox) view.findViewById(R.id.atbCheckBox);
Now, If I checked the checkbox then I want to store Yes otherwise No. Also, I want to store it in my DB. For this I have already created get and set methods in my Model
private String atbSealed;
public String getAtbSealed(){return atbSealed;}
public void setAtbSealed(String atbSealed){this.atbSealed=atbSealed;}
Update 1
As per suggestion, I have tried below
atbSealedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(isatbSealedChecked)
{
selectedAtbSealedValue = "Yes";
ctQuantitySpinner.setEnabled(false);
ctQuantitySpinner.setSelection(0);
isatbSealedChecked = true;
}
else
{
selectedAtbSealedValue = "No";
ctQuantitySpinner.setEnabled(true);
ctQuantitySpinner.setSelection(0);
isatbSealedChecked = false;
}
}
});
But It doesn't help me out. Also, the ctQuantitySpinner is not disabled on atbSealedCheckbox checked. Also if checked the boolean value of the checkbox is not changed to true. Although in log I do see the selectedAtbSealedValue but it's not set in the app. In my SaveDataLocal() function I have set the value like below
survey.setAtbSealed(this.selectedAtbSealedValue);
SurveyManager dbHelper = new SurveyManager(getActivity());
dbHelper.addSurvey(survey);
I have also tried the below code
atbSealedCheckBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(((CheckBox)v).isChecked())
{
ctQuantitySpinner.setEnabled(false);
ctQuantitySpinner.setSelection(0);
selectedAtbSealedValue = "Yes";
isatbSealedChecked = true;
//atbCheckBoxEdittext.setText(selectedAtbCheckBox);
}
else {
ctQuantitySpinner.setEnabled(true);
ctQuantitySpinner.setSelection(0);
selectedAtbSealedValue = "No";
isatbSealedChecked = false;
//atbCheckBoxEdittext.setText(selectedAtbCheckBox);
}
}
});
This code shows me the selected checkbox value, disabled the spinner and set the spinner value to null. but again the checkbox value is not saved.
How can I save custom text in a string?
Try this, it work for me:
atbSealedCheckBox.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged( CompoundButton buttonView, boolean isChecked ) {
if (isChecked) {
selectedAtbSealedValue = "yes";
} else {
selectedAtbSealedValue = "no";
}
}
} );
After that, you can store selectedAtbSealedValue into DB;
Update: Your fixed code:
atbSealedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b)
{
selectedAtbSealedValue = "Yes";
ctQuantitySpinner.setEnabled(false);
ctQuantitySpinner.setSelection(0);
isatbSealedChecked = true;
}
else
{
selectedAtbSealedValue = "No";
ctQuantitySpinner.setEnabled(true);
ctQuantitySpinner.setSelection(0);
isatbSealedChecked = false;
}
}
});
As long as I understand your question, you want to change this variable String selectedAtbSealedValue =""; when user checked your checkbox.
To achieve this statement, you can use onCheckedListener that is triggered as soon as users checked or removed the check from your checkbox.
atbSealedCheckBox.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged( CompoundButton buttonView, boolean isChecked ) {
// Todo something.
}
} );
This will help you.
At submit button get the current value of checkbox.
submit_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
control_of_vectors_value = control_of_vectors_checkbox.isChecked() == true ? "yes" : "no"; }}

Overriding setOnCheckedChangeListener in Android

I have a set of switch components, around 30. I want to override the setOnCheckedChangeListener so that I can avoid unnecessary code that does nearly the same thing.
I know, it is possible for onClick function, so using multiple buttons and changing the logic in the overridden function is way better.
What I have so far is:
final Switch question1 = (Switch) findViewById(R.id.question1);
question1.setText(R.string.no);
question1.setChecked(false);
question1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
question1.setText(R.string.yes);
} else {
question1.setText(R.string.no);
}
UpdateScores();
}
});
final Switch question2 = (Switch) findViewById(R.id.question2);
question2.setText(R.string.no);
question2.setChecked(false);
question2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
question2.setText(R.string.yes);
} else {
question2.setText(R.string.no);
}
UpdateScores();
}
});
As can be seen much of the code is replicated and I don't want that. Is there any way to achive this?
You can create one listener for all your switchers.
CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
buttonView.setText(R.string.yes);
} else {
buttonView.setText(R.string.no);
}
UpdateScores();
}
And then add this listener to your Switchers:
question1.setOnCheckedChangeListener(listener);
question2.setOnCheckedChangeListener(listener);
....
question30.setOnCheckedChangeListener(listener);
OnCheckedChangeListner onchange = new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
buttonView.setText(R.string.yes);
} else {
buttonView.setText(R.string.no);
}
UpdateScores();
}
}
question2.setOnCheckedChangeListener(onchange);
you want it?

Check if Any Checkbox is Checked

I have checkboxes for week days in my android application , and I want to put a listener to check if any one of these checkboxes is checked but my way seems hard , isn't there a way to gather all these listeners into one
sun.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
mon.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
tue.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
wed.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
thu.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
fri.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
sat.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{ if ( isChecked )
{
count=count+1;
// perform logic
}
else
{
count=count-1;
}
}});
You can check the id of the buttonView that is being triggered and do the proper validation, all you have to do is assign the same onCheckListener to the checkboxes and do something as show below:
private OnCheckedChangeListener checkedListener = new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.checkId1:
//TODO: Code for checked one...
break;
case R.id.checkId2:
//TODO: Code for checked two...
break;
}
}
};
Hope it Helps!
Regards!
If you have similar logic that you haven't provided here, you can create a list of checkboxes and create listeners for them in a cycle, for example.
Let your activity implement the OnCheckedChangeListener and then you have the onCheckChanged...
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.sun:
break;
case R.id.mon:
break;
default:
break;
}
}
there is; implements View.OnClickListener with his method in your class
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.checbox1:
if(checkbo1.isCheck){
//do logic then dont forget to set to the opposite state
checkbox.setChecked(false);
}
else{
//do logic
checkbox.setChecked(true);
}
break;
//
case R.id.checbox2:
//do logic etc...
break;
}
}
then use a switch case deal between different click event from user
hope it help
Of course there is a simpler way. Just make your Activity where you are doing this implement OnCheckedChangeListener
public Class MyActivity extends Activity implements OnCheckedChangeListener{
//your activity logic in here
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//your Activity will now receive onCheckedChangeEvents here from
//the checkboxes to which you assign it as a listener
}
}
, in onCreate, after you get references to all of your day checkboxes, set the Activity as the listener like this
#Override
public void onCreate(Bundle savedInstanceState){
//first get references to all your checkboxes and assign them to mon, tue, wed etc.
//then set the listeners
mon.setOnCheckedChangeListener(this);
tue.setOnCheckedChangeListener(this);
wed.setOnCheckedChangeListener(this);
thu.setOnCheckedChangeListener(this);
fri.setOnCheckedChangeListener(this);
sat.setOnCheckedChangeListener(this);
sun.setOnCheckedChangeListener(this);
}
, make sure all of your checkboxes have IDs assigned to them in the layout xml, like this
<CheckBox
android:id="#+id/checkMonday"
....
, then you will have one onCheckedChange method where you can handle the different days like this
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int id = buttonView.getId();
if(isChecked){
counter++; //a day has been checked
}else{
counter--; //a day has been unchecked
}
switch(id){
case R.id.checkMonday:
//logic for Monday
break;
case R.id.checkTuesday:
//logic for Tuesday
break;
...
}
}
That should do the trick!

How to uncheck checkbox using tag value of another checkbox in android

I have a number of check boxes creating dynamically.
Now i just want to select any of them not more than one. When i select the second one the first one should unselect.
I can't use radio group. Any help
Here is my code .. i am creating check boxes dynamically
private void createRadioButton() {
cb = new CheckBox(fContext);
cb.setTag(id++);
cb.setOnCheckedChangeListener( new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
Log.i("comVisa","getPos =="+getPosition);
}
});
}
You can save last check box checked and unchecked it when checked other one like this
In public area define this
HashMap<String, CompoundButton> hash = new HashMap<String, CompoundButton>();
then edit your code :
cb.setTag(id++);
cb.setOnCheckedChangeListener( new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//int getPosition = (Integer) buttonView.getTag();
if(isChecked){
if(hash.size()>0){
hash.get("1").setChecked(false);
}
hash.put("1", buttonView);
}else{
hash.clear();
}
Log.i("comVisa","getPos =="+getPosition);
}
});
A really simple example of using single selection checkbox. try this link
CheckBox myCheckBox[] = new CheckBox[noofCheckBox];
for (int i=0; i<noofCheckBox; i++) {
myCheckBox[i] = new myCheckBox(this);
myCheckBox[i].setId(i+1);
myCheckBox[i].setOnCheckedChangeListener( new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
if(myCheckBox[i].getId() == getPosition){
myCheckBox[i].setChecked(true);
}else{
myCheckBox[i].setChecked(false)
}
}
});
}

Insert CheckBox value into SQLite Android

I have this database-query:
public void checkBox(String fasil1,String fasil2,String fasil3,String fasil4,String fasil5,String fasil6,String fasil7,
String fasil8,String fasil9,String fasil10,String fasil11,String fasil12,String fasil13,String fasil14){
ContentValues cv = new ContentValues();
cv.put("fasil1", fasil1);
cv.put("fasil2", fasil2);
cv.put("fasil3", fasil3);
cv.put("fasil4", fasil4);
cv.put("fasil5", fasil5);
cv.put("fasil6", fasil6);
cv.put("fasil7", fasil7);
cv.put("fasil8", fasil8);
cv.put("fasil9", fasil9);
cv.put("fasil10", fasil10);
cv.put("fasil11", fasil11);
cv.put("fasil12", fasil12);
cv.put("fasil13", fasil13);
cv.put("fasil14", fasil14);
getWritableDatabase().insert("fasil", "name", cv);
}
and this is my CheckBox code:
fasil1 = (CheckBox)findViewById(R.id.rs);
fasil1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
checked = "Rumah Sakit";
Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
}else{
checked = "-";
}
}
});
fasil2 = (CheckBox)findViewById(R.id.skul);
fasil2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
checked = "Sekolah";
Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
}else{
checked = "-";
}
}
});
fasil3 = (CheckBox)findViewById(R.id.kampus);
fasil3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
checked = "Kampus";
Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
}else{
checked = "-";
}
}
});
fasil4 = (CheckBox)findViewById(R.id.officeBuild);
fasil4.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
checked = "Office Building";
Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
}else{
checked = "-";
}
}
});
fasil5 = (CheckBox)findViewById(R.id.apartemen);
fasil5.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
checked = "Apartemen";
Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
}else{
checked = "-";
}
}
});
fasil6 = (CheckBox)findViewById(R.id.hotel);
fasil6.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
checked = "Hotel";
Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
}else{
checked = "-";
}
}
});
fasil7 = (CheckBox)findViewById(R.id.bank);
fasil7.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
checked = "Bank";
Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
}else{
checked = "-";
}
}
});
fasil8 = (CheckBox)findViewById(R.id.spbu);
fasil8.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
checked = "SPBU";
Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
}else{
checked = "-";
}
}
});
fasil9 = (CheckBox)findViewById(R.id.pasar);
fasil9.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
checked = "Pasar";
Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
}else{
checked = "-";
}
}
});
fasil10 = (CheckBox)findViewById(R.id.wisata);
fasil10.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
checked = "Jalur Wisata / Tempat Wisata";
Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
}else{
checked = "-";
}
}
});
fasil11 = (CheckBox)findViewById(R.id.terminal);
fasil11.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
checked = "Stasiun / Terminal";
Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
}else{
checked = "-";
}
}
});
fasil12 = (CheckBox)findViewById(R.id.bandara);
fasil12.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
checked = "Bandara / Airport";
Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
}else{
checked = "-";
}
}
});
fasil13 = (CheckBox)findViewById(R.id.mol);
fasil13.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
checked = "Mal / Shoping Center";
Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
}else{
checked = "-";
}
}
});
fasil14 = (CheckBox)findViewById(R.id.hiburan);
fasil14.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
checked = "Entertainment";
Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
}else{
checked = "-";
}
}
});
Then I have code to save values when button is clicked:
helper.checkBox(fasil1.getText().toString(), fasil2.getText().toString(), fasil3.getText().toString(),
fasil4.getText().toString(), fasil5.getText().toString(), fasil6.getText().toString(),
fasil7.getText().toString(), fasil8.getText().toString(), fasil9.getText().toString(),
fasil10.getText().toString(), fasil11.getText().toString(), fasil12.getText().toString(),
fasil13.getText().toString(), fasil14.getText().toString());
But it's not work...
I want to store values checked into the database when checked or unchecked. How I can do that?
Thank you.
1st issue:
You are assigning value to checked string variable on every checkbox checked, so at the end you will be having only 1 string value whether you have selected 2 checkbox or a single, Instead you should create String[] array for checked checkbox.
2nd issue:
On the button click, you are trying to call checkBox method of your database, here again you are just fetching a value of CheckBox caption, so it doesn't matter here whether checkbox is checked or unchecked.
if a checkbox is checked you can get the bool value (true or false) with value = checkbox.isChecked();
if it is true, save as a 1 in your db, if not save as a 0

Categories

Resources