R.string.(variable) geht das

Joob

Top Contributor
Hallo,

ich benutze php-Scripte und gebe Erfolgsmeldungen zurück.
Diese zeige ich im Toast an.
Nun möchte ich die string.xml verwenden um Sprachenunabhängig zu sein.
Also gebe ich vom PHP die Stringbezeichnung zurück die in der strings.xml steht.
Leider weiß ich nicht wie ich da einen String verarbeiten soll.
Beispiel:
php sendet "phpnewMemOK"
in string.xml steht <string name="phpnewMemOK">Neues Mitglied angelegt</string>
Im Toast getString(R.string.phpnewMemOk)
den Teil phpnewMemOK bekomme ich als Variable aus

also ist die Frage : Wie geht getString(R.string.[stringvariableausphp])
 

Robat

Top Contributor
Das kannst du in der Tat recht einfach umsetzen:

Java:
// holen der ID über die getIdentifier() Methode
int id = getResources().getIdentifier(stringvariableausphp, "string", getPackageName());
String value = getString(id);

// hier muss natürlich entsprechend dem Wert geschaut werden ob in id überhaupt was drinne steht :)

Erklärung:
getIdentifier(String name, String defType, String package);
name: Name der Resource
defType: typ.. also ob R.string, R.layout, R.color etc..
package: das Package eben.

Gruß
Robert
 

Joob

Top Contributor
Erst mal schönen Dank, das haut hin.
Allerdings wo finde ich die ID, in der String.xml steht sie nicht und die Zahl sagt mir auch nichts.
Was ist die ID ?
 

Robat

Top Contributor
Die ID ist zum Beispiel: R.string.meineStringVariable.

Weil du ja den genauen Pfad nicht kennst sondern nur weißt wie die Variable heißt musst die dir die ID "zusammenbauen".
Das kannst du mit der o.g. Methode.

Mittels der getString() Methode kannst du dir dann aus der Strings.xml über die id den Wert holen.

Gruß
Robert
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
W UTF-8 String Android & Cross-Platform Mobile Apps 66
W Base64 konvertierter URI String Android & Cross-Platform Mobile Apps 32
W String Array Pfad in Int setzen Android & Cross-Platform Mobile Apps 54
W Volley String Response gibt falchen if aus Android & Cross-Platform Mobile Apps 35
H Anfänger String types not allowed (at 'textColor' with value 'black' Android & Cross-Platform Mobile Apps 13
W Firestore String in Apps Laden Android & Cross-Platform Mobile Apps 10
T Android R.string.test+i Problem Android & Cross-Platform Mobile Apps 2
A Mit Java neues item in ein string-array einer Strings.xml schreiben Android & Cross-Platform Mobile Apps 4
C Zugriff auf die Position eines String- bzw Spinner-Arrays Android & Cross-Platform Mobile Apps 1
J Android String in andere Java-Dateien überführen Android & Cross-Platform Mobile Apps 1
R Android incomingNumber bein Eingehenden Anruf immer leerer String Android & Cross-Platform Mobile Apps 4
S SPLIT funktion bei STRING funktioniert nicht! Android & Cross-Platform Mobile Apps 4
G String an einen php Script senden Android & Cross-Platform Mobile Apps 8
J Plötzlich "java.lang.String cannot be converted to JSONObject" Android & Cross-Platform Mobile Apps 9
T int to string ... Android & Cross-Platform Mobile Apps 8
A String[] für Lisadapter Android & Cross-Platform Mobile Apps 4
M jsonobject cannot be cast to java.lang.string Android & Cross-Platform Mobile Apps 4
N Android Hilfe string to float geht nicht... Android & Cross-Platform Mobile Apps 4
J Einen String bewegen wie? Android & Cross-Platform Mobile Apps 3
R String wie WAV Datei nutzen Android & Cross-Platform Mobile Apps 4
C 2 kleine Probleme (Datei lesen, String durchsuchen) Android & Cross-Platform Mobile Apps 16
L String dem Display anpassen Android & Cross-Platform Mobile Apps 12
G Text parsen String to Double Android & Cross-Platform Mobile Apps 2
S ein String nach vorgegebenen Zeichen teilen Android & Cross-Platform Mobile Apps 3
N Zeichen im String löschen? Android & Cross-Platform Mobile Apps 18
M MIDlet + Datum in String Android & Cross-Platform Mobile Apps 5
W Variable überschreiben (Android Studio) Android & Cross-Platform Mobile Apps 2
ruutaiokwu Android Studio (SDK) ANDROID_SDK_ROOT-Variable? Android & Cross-Platform Mobile Apps 5
J Variable in strings.xml (Android Studio) Android & Cross-Platform Mobile Apps 0
T variable public machen... Android & Cross-Platform Mobile Apps 5
G Android Finale Variable ändert sich NIE? Android & Cross-Platform Mobile Apps 2
G Connector.open(): cannot find symbol - variable Connector Android & Cross-Platform Mobile Apps 11
S Variable Anzahl von Bytes aus einer Datei lesen Android & Cross-Platform Mobile Apps 2
W Android Wieso geht getApplicationContext() bei Toast, aber nicht bei AlertDialog.Builder? Android & Cross-Platform Mobile Apps 36
K Log.v geht nicht Android & Cross-Platform Mobile Apps 4
B Timer geht nicht Android & Cross-Platform Mobile Apps 2
L Android Apache POI: Datei speichern geht nicht Android & Cross-Platform Mobile Apps 1
T Button geht net... Android & Cross-Platform Mobile Apps 2
D Hilfe! es geht um Buttons und Sounds Android & Cross-Platform Mobile Apps 14
S Android LogCat Ausgaben in Schleife geht nicht? Android & Cross-Platform Mobile Apps 2
K Android Alarm Manager (Wakelock? ) geht nicht Android & Cross-Platform Mobile Apps 4
P Android Programm stürzt ab - Es geht um Netzwerk Android & Cross-Platform Mobile Apps 5
D wie geht Klassenvererbung von Canvas Klassen Android & Cross-Platform Mobile Apps 2
R Meine arctan-Methode geht nicht Android & Cross-Platform Mobile Apps 6
X Anfängerfrage : Form und CommandListener - Warum geht das nicht? Android & Cross-Platform Mobile Apps 5

Ähnliche Java Themen

Neue Themen


Oben