How do I iterate through the id properties of R.java class?
Something like that?
import java.lang.reflect.Field;
/* ... */
for (int i = 1; i < 16; i++) {
int id = R.id.class.getField("s" + i).getInt(0);
tv[i] = (TextView)findViewById(id);
tv[i].setTypeface(face);
tv[i].setClickable(true);
tv[i].setOnClickListener(clickListener);
}
Enable iterate through R file in android
getFields()
return all the public fields inside the R.id
class, which is a static inner class inside the R
class, which is a Java auto generated class for Resources. That method, getFields()
, doesn't take any argument
You are looking for getField()
, not getFields()
public void initializeChipsViewholdersArray (){
for (int i = 1; i<10; i ++){
String viewholderName = "chip_00"+i;
int id_2 = R.id.class.getField(viewholderName).getInt(0);
ImageView chipViewholder= (ImageView)findViewById(id);
chipViewholder.setVisibility(View.INVISIBLE);
}
How to reference R.id s with indices?
You can achieve this by using getIdentifier()
for(int i=0; i<8; i++) {
for(int j=0; j<8; j++) {
String editTextID = "btn" + i + "-" + j;
int resID = getResources().getIdentifier(editTextID
,"id", "com.example.yourpackagename");
buttons[i][j] = ((EditTextID) findViewById(resID));
}
}
Android: What's the class of R.id.class.getFields()?
R.java
is a class. Auto generated class from the Resources
. R.id
is accessing to a inner class. The public static final class id
. R.id.class
will give you a Class object of R.id and getFields
method will return all the public
fields inside the class R.id
. Take a look to the reflection
mechanism.
EDIT: reflection.
Iteration Through R class
You can do this using java reflection:
Class raw = R.raw.class;
Field[] fields = raw.getFields();
for (Field field : fields) {
try {
Log.i("REFLECTION",
String.format("%s is %d", field.getName(), field.getInt(null)));
} catch(IllegalAccessException e) {
Log.e("REFLECTION", String.format("%s threw IllegalAccessException.",
field.getName()));
}
}
iterate through images
At OP's request, answering with a copypasta from a comment:
Have a look at How do I iterate through the id properties of R.java class? and Android: Programatically iterate through Resource ids.
...though this question should really just be closed as a dup.
Looping through json and picking random objects by id - Android/JAVA
value is not a JSONObject, but a JSONArray, this should give you two random jokes
String finalJson = buffer.toString();
JSONObject parentObject = new JSONObject((finalJson));
//JSONObject finalObj = parentObject.getJSONObject("value");
JSONArray jokeArray = parentObject.getJSONArray("value");
Random r = new Random();
String joke1 = jokeArray.getJSONObject(r.nextInt(jokeArray.length())).getString("joke");
String joke2 = jokeArray.getJSONObject(r.nextInt(jokeArray.length())).getString("joke");
This also depends on the fact the ids will always start at 1 and increment 1 at a time
EDIT
To display the ID too, break it down further...
String finalJson = buffer.toString();
JSONObject parentObject = new JSONObject((finalJson));
//JSONObject finalObj = parentObject.getJSONObject("value");
JSONArray jokeArray = parentObject.getJSONArray("value");
Random r = new Random();
int id1 = r.nextInt(jokeArray.length())
String joke1 = jokeArray.getJSONObject(id1).getString("joke");
int id2 = r.nextInt(jokeArray.length())
String joke2 = jokeArray.getJSONObject(id1).getString("joke");
String joke1WithID = id1 + " " + joke1
String joke2WithID = id2 + " " + joke2
Related Topics
PDF Library to Rendering the PDF Files in Android
How to Check If Class Exists Somewhere in Package
Android Runtime Permissions- How to Implement
Listview Viewholder Checkbox State
Why Can't I Use Resources.Getsystem() Without a Runtime Error
Double Tap: Zoom on Android Mapview
How to Get Selected Xls File Path from Uri for Sdk 17 or Below for Android
Firebase Database Points to Wrong Database Url
Nullpointerexception on Button.Findviewbyid()
Rotate Marker and Move Animation on Map Like Uber Android
What Is the Equivalent to a JavaScript Setinterval/Settimeout in Android/Java
Open Source Face Recognition for Android
Adding Header to All Request with Retrofit 2
What's the Best Practice to Round a Float to 2 Decimals