Get String from strings.xml file

Asked

Viewed 266 times

1

I’m trying to get one back String of the archive strings.xml for this variable SQL_CID.

<string name="insert_cid">"
    INTO `CID` (`COD_CID`, `NOME_CID`, `CODIGO_CID`) VALUES(1, 'CÓLERA DEVIDA A VIBRIO CHOLERAE 01, BIÓTIPO CHOLERAE', 'A000'),(2, 'CÓLERA DEVIDA A VIBRIO CHOLERAE 01, BIÓTIPO EL TOR', 'A001'),(3, 'CÓLERA NÃO ESPECIFICADA', 'A009'),(4, 'FEBRE TIFÓIDE', 'A010'),(5, 'FEBRE PARATIFÓIDE A', 'A011')
</string>

JAVA:

String SQL_CID = Resources.getSystem().getString(R.string.insert_cid);
db.execSQL(SQL_CID);

But you’re making a mistake.

1 answer

1


You must use:

  • in an Activity or Service

    String SQL_CID = getString(R.string.insert_cid);
    
  • elsewhere, you need to have a Context

    String SQL_CID = myContext.getString(R.string.insert_cid);
    

Resources.getSystem().getString() only access system resources, do not access application resources as is the case.

  • i try to use getString() but this with this error non-static method getclass() cannot be referenced from a Static context

  • The method getString() is an instance method, you must have a Context object in order to use it. If the code is not in an Activity you have to pass a Context(a Activity for example) to it and then use String SQL_CID = myContext.getString(R.string.insert_cid);

  • Thank you very much for the explanation helped a lot. I’m still in some trouble but thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.