i have a problem in my application, i have a formulaire whish a user should fill information and save it to database,i have both Edit Text and Radio Button :
rm_1 = (EditText) findViewById(R.id.rm_1);
rm_2 = (EditText) findViewById(R.id.rm_2);
rm_3 = (EditText) findViewById(R.id.rm_3);
rm_13_1 = (RadioButton) findViewById(R.id.rm_13_1);
rm_13_2 = (RadioButton) findViewById(R.id.rm_13_2);
rm_14_1 = (RadioButton) findViewById(R.id.rm_14_1);
rm_14_2 = (RadioButton) findViewById(R.id.rm_14_2);
rm_14_3 = (RadioButton) findViewById(R.id.rm_14_3);
i have a method Onclick whish associate each radio buton selected with a value :
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((Checkable) view).isChecked();
switch (view.getId()) {
case R.id.rm_13_1:
if (checked)
a = 0;
break;
case R.id.rm_13_2:
if (checked)
a = 1;
break;
case R.id.rm_14_1:
if (checked)
b = 0;
break;
case R.id.rm_14_2:
if (checked)
b = 1;
break;
case R.id.rm_14_3:
if (checked)
b = 2;
break;
case R.id.rm_14_4:
if (checked)
b = 3;
break;
}
until now everything works fine, the user writes in the edit text and select the radio button , and in database i find the same information.
in order to save the data entered by the user i did used shared preferences, so the text writing by the user and radio button selected appear again when the user returns to the activity.
That's when the issue occurs when a user change the activity , so if he returns to the activity he finds the radio button already selected but when he click on save button the value he gets in database is zero, it is like the methode :
public void onRadioButtonClicked(View view)
is not working, i don't know why ?? the user need to click again on radio button to have the values assigned in the method onRadioButtonClicked(View view), so how to solve that ?
this is where i save data :
Button bton = (Button) findViewById(R.id.ajoutUn);
bton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ajouter(v);
}
public void ajouter(View v) {
db.open();
db.insertMENAGE(rm_1ts, rm_2ts, rm_3ts, rm_4ts, rm_5ts,
rm_6ts, rm_7ts, rm_8ts, rm_9ts, rm_10ts, rm_11ts,
a, b, rm_14_4_autrets, rm_15ts);}}
And the method in database is :
public long insertMENAGE(String Region, String Provence_prefecture , String Commune_Arrondissement ,String N_district, String N_M_district , String N_menage_logement, String Adresse_menage , String Nom_Enqueteur, String code_enquêteur , String Date_realisation_enquête, String Nom_controleur , String Date_controle, int echantillon_principal, int Statut_enquêté , String autre, String Observations ) {
ContentValues initialValues = new ContentValues();
initialValues.put(col_Commune_Arrondissement,Commune_Arrondissement);
initialValues.put(col_N_district,N_district);
initialValues.put(col_N_M_district,N_M_district);
initialValues.put(col_N_menage_logement,N_menage_logement);
initialValues.put(col_Adresse_menage,Adresse_menage);
initialValues.put(col_Nom_Enqueteur,Nom_Enqueteur);
initialValues.put(col_code_enquêteur ,code_enquêteur);
initialValues.put(col_Date_realisation_enquête,Date_realisation_enquête);
initialValues.put(col_Nom_controleur,Nom_controleur);
initialValues.put(col_Date_controle,Date_controle);
initialValues.put(col_echantillon_principal,echantillon_principal);
initialValues.put(col_Statut_enquêté,Statut_enquêté);
initialValues.put(col_Observations,Observations);
return db.insertOrThrow(MENAGE,null, initialValues);
}
Check all your radio button values at your onResume() method like following, and assigning values to your variable.
if(rm_13_1.isChecked())
{
}
else if(rm_13_2.isChecked())
{
}
else if(rm_14_1.isChecked())
{
}
else if(rm_14_2.isChecked())
{
}
else if(rm_14_3.isChecked())
{
}
Hope it will work
First of all I'm posting this an answer because the text is too large.
Either there's some meaningful code that we're not seeing, or there's a problem understanding a few things.
The method onRadioButtonClicked(View view) is only called when the user clicks the radioButton. When the user comes back from another Activity the button is already selected. This is correct, and this is how it should behave. As far as I can see, this code only affects variables 'a' and 'b'.
On the other hand you say that you have a separate 'save' button. When the user comes back from another Acivity, variables 'a' and 'b' should preserve their values, and the results of your 'save' code should work.
I know this is not an answer, just a few insights that hopefully help you in some way.
Related
So I'm trying to learn Kotlin and have been using Android Studios to practice and learn. Currently I'm trying to make a simple activity with RadioGroup (with Radio Buttons), save the selected value, and then display how much of each value (radiobutton) was selected.
My question is, how do I print which button was selected, and how many of this type of button was selected?
I tried the following:
//in MainActivity.kt in my MainActivity class
s1RadioGroup.setOnCheckedChangeListener { _, checkedId ->
//if catButton was selected add 1 to variable cat
if(checkedId == R.id.catRadio) {
catSum += 1
print(catSum)
}
//if dogButton was selected add 1 to variable dog
if(checkedID == R.id.dogRadio) {
dogSum += 1
print(dogSum)
}
Not sure if I'm going about it the right way, but the desired output is:
I have layout, ID's, clear button, and everything else working. But I'm not sure how to use onClickListener event on 'SaveButton' to save selected radio button and then displaying results (Ex: Cat = 1, Dog =2). I would appreciate any suggestions, or if you can point me in the right direction.
You can maybe try something like this:
RadioButton rb = (RadioButton) findViewById(R.id.radio_button);
// restore previous state
rb.setChecked(lastButtonState);
// set a listener
rb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// call this to enable editing of the shared preferences file
// in the event of a change
SharedPreferences.Editor editor = sharedpreferences.edit();
Boolean isChecked = rb.isChecked();
// use this to add the new state
editor.putBoolean(BUTTON_STATE, isChecked);
// save
editor.apply();
}
});
I realize that this is in Java, and you're asking for kotlin, but a SharedPreference, is what you would need to save the radio button's state.
if you want to save all datam you can use database or sharedprefrence.
and if you only want just display value is clicked, you can make like this in button save.
String result1 = ""
String result2 = ""
String result3 = ""
RadioGroup radioGroup = findViewById('yourRGidFromXml')
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
int selectedId = radioGroup.getCheckedRadioButtonId();
RadioButton rb = findViewById(selecetedId)
result1= rb.getText.toString()
Log.i("ID", String.valueOf(selectedId));
}
});
//this just for see result
btnSave.OnclikListener(view -> {
Log.i("Result1",result1)
})
you can copy code and android will convert that code to kotlin.
I want the user to be able to rank a list of fruits in the order that they like to most.
Say for example, consider the list as "Mango, Apple, Banana, Pineapple".
If they like Apple, Mango, Pineapple, Banana in this order I want to use checkboxes that show the rank when the user clicks on them.
Is this possible?
Thanks
Why don't you try with a counter. It start's at 0, when user press a checkbox you just add 1 to it and you have an array for each checkbox where you will save the number pressed.
You have final int[] fruits = new int[4]; and final int counter = 0;
public void onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.checkbox_mango:
if (checked) {
counter++;
fruits[0] = counter;
} else {
counter--;
fruits[0] = 0;
}
break;
case R.id.checkbox_apple:
if (checked) {
counter++;
fruits[1] = counter;
} else {
counter--;
fruits[1] = 0;
}
break;
//OTHER CASES
}
}
Yes this is possible with Checkboxes, take a look here:
http://stackoverflow.com/questions/3149414/how-to-receive-a-event-on-android-checkbox-check-change
If I understood you right, you just have to set a variable when the user clicks on one of those checkboxes like:
var number1 = 'Mango'
You should achive this with the example you can find in the link above.
I have two activities (TextActivity.java and GrammarActivity.java), on which one fragment (ChoixLangue.java) is a radio group with four buttons to select a language of translation.
The parent of GrammarActivity is TextActivity.
I can select the language I want either on TextActivity or GrammarActivity. And I can change it on whichever ativity I am as well.
But if I select a language on TextActivity, then go to GrammarActivity and change the language, and then kill GrammarActivity, which takes me back to TextActivity, it displays the wrong radio button checked - presumably because the display of TextActivity is not refreshed.
How to fix this bug? Here is a part of the code in TextActivity `
public class TextActivity extends AppCompatActivity {
public ImageButton monImageBouton;
public Button monBouton;
public TextView monTextView;
public static RadioButton monBoutonRadio;
public static String langueChoisie = "francais";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text);
// une des solutions pour rendre la zone trado scrollable
TextView myXmlContent = (TextView)findViewById(R.id.zone_trado_scrollable);
myXmlContent.setMovementMethod(new ScrollingMovementMethod());
verifLangue();
playText();
test("button8"); // indique la langue choisie
}
public void verifLangue(){
if(langueChoisie == "english"){
monBoutonRadio = (RadioButton)findViewById(R.id.english);
monBoutonRadio.setChecked(true);
}
else if(langueChoisie == "espanol"){
monBoutonRadio = (RadioButton)findViewById(R.id.espanol);
monBoutonRadio.setChecked(true);
}
else if(langueChoisie == "chinois"){
monBoutonRadio = (RadioButton)findViewById(R.id.chinois);
monBoutonRadio.setChecked(true);
}
else{
monBoutonRadio = (RadioButton)findViewById(R.id.francais);
monBoutonRadio.setChecked(true);
}
}
public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.english:
if (checked)
langueChoisie = "english";
break;
case R.id.francais:
if (checked)
langueChoisie = "francais"; // checked et définie a priori
break;
case R.id.espanol:
if (checked)
langueChoisie = "espanol";
break;
case R.id.chinois:
if (checked)
langueChoisie = "chinois";
break;
}
}`
I'm not sure I'm fully understood your question but the best choice would be to use eventhandlers for radio buttons which will get the selected value and store it for both classes (Grammer and Text).
(I'm assuming you want the choice you made for Grammer activity should be kept for Text activity as well.)
I have a bunch of buttons to listen to.
Each button increments or decrements a value in an EditText.
Since every Button changes a different EditText I'm left with just switching.
There are quite a lot of buttons. Some buttons do the same thing except they are hidden according to user preference.
For every button in XML I did android:onClick="plusEditTexts", or android:onClick="minEditTexts" of course.
public void plusEditTexts(View v) {
switch(v.getId()) {
case R.id.firstButton1Inc:
case R.id.secondButton1Inc:
// EditText1++
break;
case R.id.firstButton2Dec:
case R.id.secondButton2Dec:
// EditText2++
break;
...
}
}
public void minEditTexts(View v) {
switch(v.getId()) {
case R.id.firstButton1Inc:
case R.id.secondButton1Inc:
// EditText1--
break;
case R.id.firstButton2Dec:
case R.id.secondButton2Dec:
// EditText2--
break;
...
}
}
I was wondering if this is the best way?
There are quite a lot of buttons. So the switch statement gets huge.
Cheers,
Daan
You could set attribute tagof your buttons with the id of the TextViewto be modified. Then, you can retrieve the int value of TextView resource id like this.-
int textResId = getResources().getIdentifier(v.getTag().toString(), "id", getPackageName());
Getting rid of the whole case sentences, so your code looked like.-
public void plusEditTexts(View v) {
int textResId = getResources().getIdentifier(v.getTag().toString(), "id", getPackageName());
EditText editText = (EditText) findViewById(textResId);
// Do your EditText stuff
}
public void minEditTexts(View v) {
int textResId = getResources().getIdentifier(v.getTag().toString(), "id", getPackageName());
EditText editText = (EditText) findViewById(textResId);
// Do your EditText stuff
}
How do I set up a OnClickListener to simply tell me which index button was pressed from an array of buttons. I can change text and color of these buttons using the array. I set them up like this.
TButton[1] = (Button)findViewById(R.id.Button01);
TButton[2] = (Button)findViewById(R.id.Button02);
TButton[3] = (Button)findViewById(R.id.Button03);
up to 36.
The OnClickListener is going to receive the button itself, such as R.id.Button01. It's not going to give you back your array index, as it knows nothing about how you have references to all the buttons stored in an array.
You could just use the button that is passed into your onClickListener directly, with no extra lookups in your array needed. Such as:
void onClick(View v)
{
Button clickedButton = (Button) v;
// do what I need to do when a button is clicked here...
switch (clickedButton.getId())
{
case R.id.Button01:
// do something
break;
case R.id.Button01:
// do something
break;
}
}
If you are really set on finding the array index of the button that was clicked, then you could do something like:
void onClick(View v)
{
int index = 0;
for (int i = 0; i < buttonArray.length; i++)
{
if (buttonArray[i].getId() == v.getId())
{
index = i;
break;
}
}
// index is now the array index of the button that was clicked
}
But that really seems like the most inefficient way of going about this. Perhaps if you gave more information about what you are trying to accomplish in your OnClickListener I could give you more help.
You can set Tag value and get the Tag on Click:
TButton[1] = (Button)findViewById(R.id.Button01);
TButton[1].setTag(1);
onClick(View v)
{
if(((Integer)v.getTag())==1)
{
//do something
}
}