Increment hex value of R.id file element - android

i have some consecutive elements id declared inside R.java files. Now i need to fill each one by using a for cycle, so i need to increment for each iteration value of id. I've write this:
int current_id = R.id.button00;
for (int i = 0; i < values.length; i++) {
TextView to_fill = (TextView) (getActivity().findViewById(current_id));
to_fill.setText(String.valueOf(values[i]));
current_id++;
}
but in this way current_id doesn't increment correctly.
How can i do?

There is no guarantee that the generated IDs in R will be sequential, or that they will be generated in any particular order.
I recommend putting your IDs in an array resource like so:
<array name="button_id_array">
<item>#id/button00</item>
<item>#id/button01</item>
<item>#id/button02</item>
</array>
Then you can access it in code like so:
int[] ids = getResources().getIntArray(R.array.test);

Related

How to get Values in String Array from XML in android

I have a String value in a variable for eg ID
XML Like
<DocumentElement><Contact ID="1" Name="Test1" 1/><Contact ID="2" Name="TEST" /></DocumentElement>
i get my id in _s2
i want to add all id in a String Array like EmailArr
i have Done
Count=0;
EmailArr=new String[Count];
String _s2=event.getAttribute("ID").getValue();
if(_s2=="" || _s2==null){
_s2="N/A";
}
if(_s2!=null){
EmailArr[Count]=_s2;
Count=Count++;
}
I get Exception java.lang.ArrayIndexOutOfBoundsException
You need to create an Array with Xml Values:-
int count=XMlData.getPropertyCount();// Get the XML Data count/ some thing related to get Count of XMl Details
EmailArr=new String[Count]; // create Array EmailArr with that Count
You should give some initial size to your array :
EmailArr=new String[Count];//where count is number of records

get list of the strings present in strings.xml file in Android

I want the list of strings present in the strings.xml file.
Does anyone knows how to get it??? One thing I found is it assigns the ids in sequential order inside R.java but how to get the starting id is not clear.
For Example I have 100 Strings in my strings.xml like below and I want to read in at a time not like giving getResources().getString(int id) for individual.
<string name="app_label">Calendar</string>
<string name="what_label">What</string>
<string name="where_label">Where</string>
<string name="when_label">When</string>
<string name="timezone_label">Time zone</string>
<string name="attendees_label">Guests</string>
<string name="today">Today</string>
<string name="tomorrow">Tomorrow</string>
You can declare your strings in res\values\strings.xml file like this.
<string-array name="vehiclescategory_array">
<item>Cars</item>
<item>Bikes</item>
<item>RVs</item>
<item>Trucks</item>
<item>Other Vehicles</item>
</string-array>
In your activity class, you can access them like the following.
String[] categories;
categories=getResources().getStringArray(R.array.vehiclescategory_array);
In the above list, whatever sequence you declare, the same way it is assigned to the array in your activity. Suppose Cars will be assigned to categories[0]. Hope this helps.
Field[] fields = R.string.class.getDeclaredFields(); // or Field[] fields = R.string.class.getFields();
String str = "";
for (int i =0; i < fields.length; i++) {
int resId = getResources().getIdentifier(fields[i].getName(), "string", getPackageName());
str += fields[i].getName() + " = ";
if (resId != 0) {
str += getResources().getString(resId);
}
str += "\n";
}
You will get all codes of strings with its values in "str" variable.
If you want to access all the Strings from the strings.xml file you could use reflection on the R.string class. An example can be found in this answer, you'll just need to replace drawables with strings.
You could declare an integer array with an entry for each string. I did this for an array of colors once, so I imagine it works for strings as well.
res/values/arrays.xml
<integer-array name="app_strings">
<item>#string/app_label</item>
<item>#string/what_label</item>
<item>#string/where_label</item>
<item>#string/when_label</item>
<item>#string/timezone_label</item>
<item>#string/attendees_label</item>
<item>#string/today</item>
<item>#string/tomorrow</item>
</integer-array>
Then in your code, you would loop over the array and use each value as the argument for getString().
int[] stringIds = getResources().getIntArray(R.array.app_strings);
String[] strings = new String[stringIds.length];
for (int i = 0; i < stringIds.length; i++) {
strings[i] = getString(stringIds[i]);
}
The problem is you have to manually update your arrays.xml whenever you modify your string resources, so it's certainly not ideal.
String[] categories = getResources().getStringArray(R.array.stars_array);
List<String> stringList = new ArrayList<>(Arrays.asList(categories));
use this simple one

Store multiple dynamic EditText value

I have a code adding multiple EditText. How can i store them into a array. This number 10 is just example, the number may bigger than that. How can i store it after click a button
for(int i=0; i<10; i++) {
String store[] = new String[10];
EditText addAnsMCQ = new EditText(this);
AnswerRG.addView(addAnsMCQ, 1);
addAnsMCQ.setWidth(200);
addAnsMCQ.setId(1000);
}
In your example the store variable isn't actually being used, did you intend do use it for storing the EditTexts?
Instead of using an array of String, just use an array of EditText and store a reference to them:
EditText store[] = new EditText[10];
for(int i=0; i<10; i++) {
EditText addAnsMCQ = new EditText(this);
AnswerRG.addView(addAnsMCQ, 1);
addAnsMCQ.setWidth(200);
addAnsMCQ.setId(1000);
store[i] = addAnsMCQ; //store a reference in the array to the EditText created
}
Then outside of the for loop, you can access the reference to each EditText, e.g.
store[0].setWidth(300);
You need to keep/get a reference to each of your EditText's then you can look up its value with .getText().toString() which you can store in whatever manner you like.
However if as you say
This number 10 is just example, the number may bigger than that.
If the number is going to be larger you should be using an Adapter and a ListView or something to hold your View objects. That will make it easier to get everything on to the screen. And will give you the benefit of view recycling.

How to Use String as a ResourceID in Android

I've several TextViews or another component, doesn't matter. And the views have iteration ids like: textView1, textView2, textView3 etc.
Simply I want to iterate ids by using pre-string values.
Psuedo example:
String pre_value = "textView";
for(int i = 0; i < size; i++) {
String usable_resource_id = pre_value + Integer.toString(pre_value);
// So how to use these id like R.id.textView1
// Cast or something similar
}
Any suggestions?
You can use Resources#getIdentifier() to get the identifier from a string.
But if you are going to iterate over them, wouldn't it be easier to keep the ids in an array or a list?

submitting data to an array to be pulled later

So I've got the following:
NodeList nodeList = element.getElementsByTagName("rowset");
if (nodeList.getLength() > 0) {
for (int i = 0; i < nodeList.getLength(); i++) {
Element entry = (Element) nodeList.item(i);
Element _titleE = (Element) entry.getElementsByTagName("row").item(0);
Node _title = _titleE.getAttributes().getNamedItem("name");
t1.setText(_title.getNodeValue());
}
}
I've got the following XML layout:
<row name="" characterID="" corporationName="" corporationID="" />
(couple of lines of these)
the ideal way would be to create an array right? and then call the data from the array?
EDIT:
What I'm trying to do is read an XML File and store the values so that they can be accessed later so I'm assuming the ideal way would be to use an array?
(as my girlfriends name is jenny, too, I will be guessing what you want)
If you just want to store one value, an array or a ArrayList is good for that. If you need to store all 4 given attributes of your row, you should think about creating a class (lets call it MyRow) that contains those values. Than you can put all your rows into one ArrayList with the type of your class.
Pseudocode:
ArrayList<MyRow> myRowList = new ArrayList<MyRow>();
while reading each row
MyRow row = new MyRow();
row.mName = getAttributes().getNamesItem("name");
row.mCharacterId = getAttributes().getNamesItem("characterID");
// more setting...
}
A last tip for the next time: take some time to explain and specify your next question. That will improve the answers you get as well.

Categories

Resources