RadioGroup mit EditTextfield

  • Antworten:2
Bernd Roth
  • Forum-Beiträge: 98

15.01.2016, 21:47:30 via Website

Hallo Forum,
ich habe folgendes Problem bei meiner App:

Ich hatte eine RadioGroup mit statischem Text, jetzt wollte ich die App erweitern, sodass man auch einen RadioButton mit einem Edittextfield hat.

Dabei musste ich mein XML File ein wenig anpassen.
Zuvor sah es so aus:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<RadioGroup
    android:id="@+id/radioButtonGroup"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
<RadioButton
    android:id="@+id/radioButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Wien" />
<RadioButton
    android:id="@+id/radioButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="A Bregenz" />
 </RadioGroup>
 </ScrollView>

Ich habe es ein wenig verkürzt, die 50 anderen RadioButtons tragen nur einen anderen Namen aufsteigend nach ihrer Anzahl!

Jetzt habe ich das Layout folgendermaßen verändert:

<?xml version="1.0" encoding="utf-8"?>

android:layout_width="match_parent"
android:layout_height="match_parent">

android:layout_width="match_parent"
android:layout_height="1322dp" >

<RadioGroup
    android:id="@+id/radioButtonGroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
        <RelativeLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <RadioButton
                android:id="@+id/radioButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:text="Wien" />
            <RadioButton
                android:id="@+id/radioButton56"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/radioButton55"/>

            <EditText
                android:id="@+id/editText1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignRight="@+id/radioButton45"
                android:layout_toRightOf="@+id/radioButton56"
                android:editable="true"
                android:singleLine="true"
                android:text="Test Text" />
        </RelativeLayout>
           </RadioGroup>
     </RelativeLayout>
     </ScrollView>

Mein Java Code, welcher den zuvor angeklickten RadioButton aus den SharedPreferences sich holt:

    RadioGroup radioGroup;
RadioButton radioButton;
final String KEY_SAVED_RADIO_BUTTON_INDEX = "SAVED_RADIO_BUTTON_INDEX";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_stadt);
    radioGroup = (RadioGroup)findViewById(R.id.radioButtonGroup);
    radioGroup.setOnCheckedChangeListener(radioGroupOnCheckedChangeListener);
    LoadPreferences();
}

OnCheckedChangeListener radioGroupOnCheckedChangeListener = new OnCheckedChangeListener(){
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        RadioButton checkedRadioButton = (RadioButton)radioGroup.findViewById(checkedId);
        int checkedIndex = radioGroup.indexOfChild(checkedRadioButton);
        int selectedId = radioGroup.getCheckedRadioButtonId();
        radioButton = (RadioButton) findViewById(selectedId);
        if(isNetworkAvailable() == true)
            displayTextMessage(checkedIndex);
        else
            Toast.makeText(getBaseContext(), "Bitte schalten Sie die Internet. -oder WiFi - Verbindung für eine Übersicht über die Parkzeiten ein!", Toast.LENGTH_LONG).show();
        SavePreferences(KEY_SAVED_RADIO_BUTTON_INDEX, checkedIndex);
    }
};
private void SavePreferences(String key, int value){
    SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt(key, value);
    editor.commit();
    SharedPreferences sharedPreferences1 = getSharedPreferences("stadt", MODE_PRIVATE);
    SharedPreferences.Editor editor1 = sharedPreferences1.edit();
    editor1.putString("stadt", radioButton.getText().toString());
    editor1.commit();
}

private void LoadPreferences(){
    SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
    int savedRadioIndex = sharedPreferences.getInt(KEY_SAVED_RADIO_BUTTON_INDEX, 0);
    RadioButton savedCheckedRadioButton = (RadioButton)radioGroup.getChildAt(savedRadioIndex);
    savedCheckedRadioButton.setChecked(true);
}

Nun bekomme, wenn ich die App starte, die Fehlermeldung: ClassCastException - RelativLayout cannot be cast to android.widget.RadioButton, Fehler bei der Methode LoadPreferences()

Ich vermute, dass sich der Fehler durch die eine RelativLayout - Erweiterung gegeben hat, leider jedoch weiss ich nicht, wie ich den beheben kann.
Wenn mir jemand vll weiterhelfen könnte.

Danke schon vielmals im vorhinein!

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

16.01.2016, 09:08:34 via App

Da musst du eine Ebene Tiefer gehen.
Also RelativeLayout ist child 0. Das castest du tu einem Relayout Objekt.
Dann kannst du ganz normal über den Index weiter den RadioButton suchen. Ich würde aber zusätzlich das ganze cast safe bauen indem ich erstmal eine normale View suche und dann schaue on die View instanceof RadioButton ist. Dann sollte es gehen.

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Bernd Roth
  • Forum-Beiträge: 98

16.01.2016, 19:04:17 via Website

Danke Dir vielmals!

So etwas habe ich mir gedacht, war mir nur nicht ganz sicher!
Werde es dann mal auf diese Art versuchen.

LG

Antworten