单选按钮在安卓中返回错误的值

huangapple go评论100阅读模式
英文:

Radio button returns wrong value in Android

问题

以下是翻译好的代码部分:

ViewFriend.java(显示列表视图并具有编辑选项的类)

  1. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  2. @Override
  3. public void onItemClick(AdapterView<?> parent, final View view, final int position, long id) {
  4. // ...
  5. builder.setPositiveButton("Edit", new DialogInterface.OnClickListener() {
  6. @Override
  7. public void onClick(DialogInterface dialog, int which) {
  8. // ...
  9. intent.putExtra("gender", gender);
  10. // ...
  11. }
  12. });
  13. // ...
  14. }
  15. });

editFriend.java(用传递的意图填充表单数据并显示)

  1. public class editFriend extends AppCompatActivity {
  2. // ...
  3. @Override
  4. protected void onCreate(@Nullable Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.fragment_editordeletefriend);
  7. // ...
  8. name = intent.getStringExtra("name");
  9. age = intent.getStringExtra("age");
  10. gender = intent.getStringExtra("gender");
  11. address = intent.getStringExtra("address");
  12. // ...
  13. }
  14. // ...
  15. public void displayForm() {
  16. // ...
  17. if (gender.equals("Male")) {
  18. rb = (RadioButton)findViewById(R.id.resultGenderMale_update);
  19. } else if (gender.equals("Female")) {
  20. rb = (RadioButton)findViewById(R.id.resultGenderFemale_update);
  21. }
  22. rb.setChecked(true);
  23. // ...
  24. }
  25. // ...
  26. }

如果您有任何其他问题或需要进一步的帮助,请随时问我。

英文:

I have a form where a person can enter details about a friend (name, age, gender, address). This friend is displayed in a list view and when a friend from the list is clicked on they have the choice to edit that record.

I can successfully update every detail about a friend except for the gender.

For example:

List view:

  1. 1) James Bond, 20, Male, Sydney NSW

Then I click edit and change it to

  1. James smith, 21, Female, Canberra NSW

and then back in my list view it will show:

  1. 1) James smith, 21, Male, Canberra NSW

Notice how the gender doesn't change?

I can figure out why this is happening as I use the same logic to change the name and age as i did to change the gender

Here is the relevant code:

ViewFriend.java ( this class displays the list view and has the edit option)

  1. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  2. @Override
  3. public void onItemClick(AdapterView&lt;?&gt; parent, final View view, final int position, long id) {
  4. String text = listView.getItemAtPosition(position).toString();
  5. final int ID = Integer.parseInt(String.valueOf(text.charAt(0)));
  6. AlertDialog.Builder builder = new AlertDialog.Builder(viewFriends.this);
  7. builder.setTitle(&quot;Notice&quot;);
  8. builder.setMessage(&quot;Please select to to edit, delete a friend or cancel&quot;);
  9. // add the buttons
  10. builder.setPositiveButton(&quot;Edit&quot;, new DialogInterface.OnClickListener() {
  11. @Override
  12. public void onClick(DialogInterface dialog, int which) {
  13. Intent intent = new Intent(getApplicationContext(), editOrDelete.class);
  14. ArrayList&lt;String&gt; result1 = mydb.retrieveRow(ID);
  15. name = result1.get(1);
  16. age = result1.get(2);
  17. gender = result1.get(3);
  18. address = result1.get(4);
  19. code = result1.get(0);
  20. intent.putExtra(&quot;code&quot;, code);
  21. intent.putExtra(&quot;name&quot;, name);
  22. intent.putExtra(&quot;age&quot;, age);
  23. intent.putExtra(&quot;gender&quot;, gender);
  24. intent.putExtra(&quot;address&quot;, address);
  25. startActivity(intent);
  26. }
  27. });
  28. builder.setNeutralButton(&quot; Delete&quot;, new DialogInterface.OnClickListener() {
  29. @Override
  30. public void onClick(DialogInterface dialog, int which) {
  31. mydb.deleteTitle(ID);
  32. finish();
  33. Intent intent = new Intent(getApplicationContext(),viewFriends.class);
  34. startActivity(intent);
  35. }
  36. });
  37. builder.setNegativeButton(&quot;Cancel&quot;, null);
  38. AlertDialog dialog = builder.create();
  39. dialog.show();
  40. }
  41. });

The code above retrieves the details from the database and passes it to the intent. I have printed the contents of each variable (name, age, gender, address) and they print out correctly.

editFriend.java ( this class pre fills the form with the data passed through the intent that displays correctly)

  1. public class editFriend extends AppCompatActivity {
  2. private Intent intent;
  3. private RadioGroup rg;
  4. private Button update;
  5. private RadioButton rb;
  6. private String newName,newAddress,newGender;
  7. private int newAge;
  8. EditText ed, ed1;
  9. public String name,address,gender,age,code;
  10. private int selectedID,ages,codes;
  11. NumberPicker numberPicker;
  12. private databaseManager4 myDataBase;
  13. @Override
  14. protected void onCreate(@Nullable Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.fragment_editordeletefriend);
  17. intent= getIntent();
  18. myDataBase = new databaseManager4(this);
  19. rg = (RadioGroup)findViewById(R.id.radioGroup_update);
  20. selectedID = rg.getCheckedRadioButtonId();
  21. rb = (RadioButton) findViewById(selectedID);
  22. ed = (EditText)findViewById(R.id.fullName_update);
  23. numberPicker = (NumberPicker)findViewById(R.id.resultAge_update);
  24. numberPicker.setMinValue(6);
  25. numberPicker.setMaxValue(110);
  26. numberPicker.setWrapSelectorWheel(false);
  27. update = (Button)findViewById(R.id.update_button);
  28. ed1 = (EditText)findViewById(R.id.address_update);
  29. name = intent.getStringExtra(&quot;name&quot;);
  30. age = intent.getStringExtra(&quot;age&quot;);
  31. gender = intent.getStringExtra(&quot;gender&quot;);
  32. address = intent.getStringExtra(&quot;address&quot;);
  33. code = intent.getStringExtra(&quot;code&quot;);
  34. codes = Integer.parseInt(code);
  35. displayForm();
  36. newName = ed.getText().toString();
  37. newAge = numberPicker.getValue();
  38. newGender = rb.getText().toString();
  39. update.setOnClickListener(new View.OnClickListener() {
  40. @Override
  41. public void onClick(View v) {
  42. int id = rg.getCheckedRadioButtonId();
  43. rb = (RadioButton) findViewById(id);
  44. if (myDataBase.updateRow(codes,newName,newAge,newGender,ed1.getText().toString())){
  45. Toast.makeText(getApplicationContext(),&quot;successfully updated the friend &quot;
  46. +ed.getText().toString(),Toast.LENGTH_SHORT).show();
  47. }
  48. else{
  49. Toast.makeText(getApplicationContext(),&quot;Could not update the friend &quot;
  50. +ed.getText().toString(),Toast.LENGTH_SHORT).show();
  51. }
  52. intent = new Intent(getApplicationContext(),viewFriends.class);
  53. startActivity(intent);
  54. }
  55. });
  56. }
  57. public void displayForm(){
  58. ed.setText(name);
  59. ed1.setText(address);
  60. if (gender.equals(&quot;Male&quot;)){
  61. rb = (RadioButton)findViewById(R.id.resultGenderMale_update);
  62. }
  63. else if (gender.equals(&quot;Female&quot;))
  64. {
  65. rb = (RadioButton)findViewById(R.id.resultGenderFemale_update);
  66. }
  67. rb.setChecked(true);
  68. ages= Integer.parseInt(age);
  69. numberPicker.setValue(ages);
  70. }
  71. public void clear(){
  72. ed.setText(&quot;&quot;);
  73. ed1.setText(&quot;&quot;);
  74. }
  75. }

This is where the issue lies, even if the user clicks on Male it registers as female and i am unsure why.

Any ideas how i can fix this?

答案1

得分: 0

你的问题在于你将 rb 变量的值设置为一个单选按钮,根据先前的性别值预定义,代码如下:

  1. if (gender.equals("Male")){
  2. rb = (RadioButton)findViewById(R.id.resultGenderMale_update);
  3. }
  4. else if (gender.equals("Female"))
  5. {
  6. rb = (RadioButton)findViewById(R.id.resultGenderFemale_update);
  7. }

然后你在同一个按钮上读取值。为了解决这个问题,在更新按钮的点击监听器中,你需要使用 getCheckedRadioButtonId 方法来读取选中的单选按钮 id,使用你的 rg 变量来表示单选按钮组。

以下是你的 "Edit Friend" 活动代码可能的外观:

  1. public class EditFriendActivity extends AppCompatActivity {
  2. // ... 其他成员变量的初始化
  3. @Override
  4. protected void onCreate(@Nullable Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.fragment_editordeletefriend);
  7. initUI();
  8. mDatabaseManager = new databaseManager4(this);
  9. }
  10. private void initUI() {
  11. // ... 初始化 UI 元素的代码
  12. mUpdateButton = (Button) findViewById(R.id.update_button);
  13. mUpdateButton.setOnClickListener(updateClickListener);
  14. mGenderRadioGroup = (RadioGroup) findViewById(R.id.radioGroup_update);
  15. RadioButton targetRadioButton = null;
  16. switch (Gender.fromString(intent.getStringExtra("gender"))) {
  17. case MALE:
  18. targetRadioButton = (RadioButton) findViewById(R.id.resultGenderMale_update);
  19. break;
  20. case FEMALE:
  21. targetRadioButton = (RadioButton) findViewById(R.id.resultGenderFemale_update);
  22. break;
  23. }
  24. if (targetRadioButton != null) {
  25. targetRadioButton.setChecked(true);
  26. }
  27. }
  28. // ... 其他方法的定义
  29. private View.OnClickListener updateClickListener =
  30. new View.OnClickListener() {
  31. @Override
  32. public void onClick(View v) {
  33. Gender newGender = getSelectedGender();
  34. int code = Integer.parseInt(getIntent().getStringExtra("code"));
  35. // ... 更新数据库并显示 Toast 通知
  36. Intent viewFriendsIntent = new Intent(getApplicationContext(), viewFriends.class);
  37. startActivity(viewFriendsIntent);
  38. finish();
  39. }
  40. };
  41. // ... 其他内部类和枚举的定义
  42. }

注意:上面的代码片段仅包含翻译后的部分,用于描述问题和解决方案。实际使用时,你需要将这些代码片段与你的项目整合,并根据需要进行调整。

英文:

Your problem is that you set value of rb variable to a single radio button, predefined with previous gender value here

  1. if (gender.equals(&quot;Male&quot;)){
  2. rb = (RadioButton)findViewById(R.id.resultGenderMale_update);
  3. }
  4. else if (gender.equals(&quot;Female&quot;))
  5. {
  6. rb = (RadioButton)findViewById(R.id.resultGenderFemale_update);
  7. }

And you read value of the same button then. To fix that in you click listener for update button you need to read checked radio button id using getCheckedRadioButtonId on radio group (your rg variable).

upd:
this is how your Edit Friend activity code might look like

  1. public class EditFriendActivity extends AppCompatActivity {
  2. private RadioGroup mGenderRadioGroup;
  3. private Button mUpdateButton;
  4. private EditText mNameField;
  5. private EditText mAddressField;
  6. private NumberPicker mAgePicker;
  7. private databaseManager4 mDatabaseManager;
  8. @Override
  9. protected void onCreate(@Nullable Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.fragment_editordeletefriend);
  12. initUI();
  13. mDatabaseManager = new databaseManager4(this);
  14. }
  15. private void initUI() {
  16. Intent intent = getIntent();
  17. mNameField = (EditText) findViewById(R.id.fullName_update);
  18. mNameField.setText(intent.getStringExtra(&quot;name&quot;));
  19. mAddressField = (EditText) findViewById(R.id.address_update);
  20. mAddressField.setText(intent.getStringExtra(&quot;address&quot;));
  21. mAgePicker = (NumberPicker) findViewById(R.id.resultAge_update);
  22. mAgePicker.setMinValue(6);
  23. mAgePicker.setMaxValue(110);
  24. mAgePicker.setWrapSelectorWheel(false);
  25. mAgePicker.setValue(Integer.parseInt(intent.getStringExtra(&quot;age&quot;)));
  26. mUpdateButton = (Button) findViewById(R.id.update_button);
  27. mUpdateButton.setOnClickListener(updateClickListener);
  28. mGenderRadioGroup = (RadioGroup) findViewById(R.id.radioGroup_update);
  29. RadioButton targetRadioButton = null;
  30. switch (Gender.fromString(intent.getStringExtra(&quot;gender&quot;))) {
  31. case MALE:
  32. targetRadioButton = (RadioButton) findViewById(R.id.resultGenderMale_update);
  33. break;
  34. case FEMALE:
  35. targetRadioButton = (RadioButton) findViewById(R.id.resultGenderFemale_update);
  36. break;
  37. }
  38. if (targetRadioButton != null) {
  39. targetRadioButton.setChecked(true);
  40. }
  41. }
  42. public void clear() {
  43. mNameField.setText(&quot;&quot;);
  44. mAddressField.setText(&quot;&quot;);
  45. }
  46. private Gender getSelectedGender() {
  47. int checkedButtonId = mGenderRadioGroup.getCheckedRadioButtonId();
  48. switch (checkedButtonId) {
  49. case R.id.resultGenderMale_update:
  50. return Gender.MALE;
  51. case R.id.resultGenderFemale_update:
  52. return Gender.FEMALE;
  53. }
  54. return Gender.UNDEFINED;
  55. }
  56. private View.OnClickListener updateClickListener =
  57. new View.OnClickListener() {
  58. @Override
  59. public void onClick(View v) {
  60. Gender newGender = getSelectedGender();
  61. int code = Integer.parseInt(getIntent().getStringExtra(&quot;code&quot;));
  62. System.out.println(&quot;gender is&quot; + newGender.stringValue);
  63. if (mDatabaseManager.updateRow(
  64. code,
  65. mNameField.getText().toString(),
  66. mAgePicker.getValue(),
  67. newGender.stringValue,
  68. mAddressField.getText().toString())) {
  69. Toast.makeText(
  70. getApplicationContext(),
  71. &quot;successfully updated the friend &quot; + ed.getText().toString(),
  72. Toast.LENGTH_SHORT)
  73. .show();
  74. } else {
  75. Toast.makeText(
  76. getApplicationContext(),
  77. &quot;Could not update the friend &quot; + ed.getText().toString(),
  78. Toast.LENGTH_SHORT)
  79. .show();
  80. }
  81. Intent viewFriendsIntent = new Intent(getApplicationContext(), viewFriends.class);
  82. startActivity(viewFriendsIntent);
  83. finish();
  84. }
  85. };
  86. private enum Gender {
  87. UNDEFINED(&quot;undefined&quot;),
  88. MALE(&quot;Male&quot;),
  89. FEMALE(&quot;Female&quot;);
  90. private @Nullable String stringValue;
  91. Gender(@Nullable String stringValue) {
  92. this.stringValue = stringValue;
  93. }
  94. public static Gender fromString(@Nullable String value) {
  95. if (value != null) {
  96. for (Gender gender : Gender.values()) {
  97. if (value.equals(gender.stringValue)) {
  98. return gender;
  99. }
  100. }
  101. }
  102. return UNDEFINED;
  103. }
  104. }
  105. }

huangapple
  • 本文由 发表于 2020年9月11日 13:23:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63841205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定