I have a string array in strings.xml that looks like this:
<string-array name="helpPages">
<item>Hello, I am DOS-bot, and I will guide you through this game.</item>
<item>In this game you will learn how to use a computer terminal or console.
This is an important tool for dealing with technical problems on your computer.</item>
<item>For the common user, the terminal can be useful for figuring out problems
related to Internet connectivity, corrupted or damaged files and many more. </item>
</string-array>
And I want to access it in one of my activities.
I'm currently doing the following:
String[] pages = GetStringArray(Resource.Array.helpPages);
But it's not working.
I would do it like this :
String[] pages = getResources().getStringArray(R.array.helpPages);
The question asked about Xamarin.Android but the accepted answer syntax is for Java, so here is C# syntax (Xamarin way):
var items = Resources.GetStringArray(Resource.Array.helpPages);
You can access to each item by using for or foreach. for ex.:
foreach (var item in items)
{
// do whatever you want with item
}
Related
I made an array of questions in res->values->strings
<string-array name="Questions">
<item>Nile River is the longest river in the world.</item>
<item>Ottawa is the capital of Canada.</item>
...
</string-array>
I want to use each element in different places so I tried this syntax:
var questionList=resources.getStringArray(R.array.Questions)
..
binding.textViewQuestion.text=questionList[i]
I am not very familiar with syntaxes in android and I found this one in developer.android.com; it does not work and I could not find any other way.
There is an error says
Resource compilation failed.
and when I delete the string array the error will gone. I don't know where I'm wrong.
You should check your string array, it could possibly be an syntax error in it.
You can try to just put really simple word in your array and see if the problem is here. Otherwise, the syntax of your other line seems good to me.
http://i.stack.imgur.com/xsCSE.jpg
My game is very similar to this game named "Riddle Me That" but it's in another language. The design part is complete and I've implemented the buttons and all but how should I create the database to implement it in the app? I'm using SQLite to create the database. In a table I've created a question column and answer column. Now since the answer needs to be formed using combination of letters that is to be input by users (please see the link above), I'm really confused on how to make the database. Please help!
If you are confused about the Database there are too much tutorials about it just google it .
But i really don't recommend you to use the databases in such app you can use instead the string resources (res > values > strings.xml) and add a string array for questions and another for answers .
and be sure about the order so that the first item in answers array would be the answer of the first item in the question array and so on.
for example :
res/values/strings.xml
<string-array name="questions">
<item>who are you ?</item>
</string-array>
<string-array name="answers">
<item>Omar</item>
</string-array>
and here is the java code to fetch the arrays :
String[] questions;
String[] answers;
public void onCreate(Bundle b)
{
//here is how to get the string arrays from the resources to fetch them as you like
questions = getResources().getStringArray(R.array.questions);
answers = getResources().getStringArray(R.array.answers);
}
I wrote a big app with thousands of string in the code.... very bad idea, because now I want to translate each string.... big problem.
Copying all strings to the strings.xml takes a long time.
Eclipse has an option to take all selected strings and put them into messages.properties.
Does this work similiar like strings.xml? When, why all people use strings.xml.
Or should is use eclipse to seperate each string and than I should copy them to string.xml?
All people are using strings.xml because this is the normal way to do it on Android. You don't have to manage the load of the strings, to call any locale function in your script.
You can see the documentation here : http://developer.android.com/guide/topics/resources/index.html
BTW, you can easily transform your eclipse generated file to an strings.xml file after the extraction.
In Eclipse you can use the shortcut keys Alt + Shift A, S to extract an inline string in to the strings.xml file via a popup dialog - might be a bit easier than doing it by hand. And as the others say, yes you should ALWAYS use the strings.xml file so that you only have to look in one place when you want to change a string, instead of having to search through all your code.
How to search for a specific text inside a string-array item element? The following is an example of the xml file. The string-array name is android. I have some items inside the string-array. Now I want to do a search for the word "software". Please tell me how to do that?
<?xml version="1.0" encoding="utf-8"?><resources>
<string-array name="android">
<item>Android is a software stack for mobile devices that includes an operating system, middleware and key applications.</item>
<item>Google Inc. purchased the initial developer of the software, Android Inc., in 2005..</item>
</string-array>
This method has better performances:
String[] androidStrings = getResources().getStringArray(R.array.android);
if (Arrays.asList(androidStrings).contains("software") {
// found a match to "software"
}
Arrays.asList().contains() is faster than using a for loop.
I assume that you want to do this in code. There's nothing in the api to do text matching on an entire String array; you need to do it one element at a time:
String[] androidStrings = getResources().getStringArray(R.array.android);
for (String s : androidStrings) {
int i = s.indexOf("software");
if (i >= 0) {
// found a match to "software" at offset i
}
}
Of course, you could use a Matcher and Pattern, or you could iterate through the array with an index if you wanted to know the position in the array of a match. But this is the general approach.
I want to use the concept of array in my Android Application, I don't know how to do that actually.
So could anybody please help me how to do that on demand.
I guess you are talking about arrays in Android through the res folder.
Create an array.xml inside the /res/values folder with something like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="names_list">
<item>John</item>
<item>Peter</item>
<item>Charles</item>
</string-array>
</resources>
You can get that array on your Activity by doing:
getResources().getStringArray(R.array.names_list);
There are alot of different "array" types in java... there are actual arrays like Thorsten showed you and then there are lists, collections and hashes. Take you pick. :) A great place to start learning more about Java is the docs.
http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/
http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/
This defines an array of 5 strings:
String[] stringArray = new String[5];
However, I can not imagine that this is really what you're talking about...
CLARIFICATION
If you actually don't know what an array is, then my reply will give you a hint. In case you're talking about something else, this reply should indicate that you're not giving enough detail. You might as well be talking about this...