How to Use String as a ResourceID in Android - 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?

Related

How to read Text on any view of any application using AccessibilityInfo?

I am trying to read every item inside a ListView of any application by using AccessibilityInfo class. The problem is that i am getting the total number of items inside the ListView by using the following piece of code:
if(event.getClassName().equals("android.widget.ListView")) {
String className= TextView.class.getName();
AccessibilityNodeInfo nodeInfo= event.getSource();
int i= nodeInfo.getChildCount(); }
But i am not getting the names of items inside the ListView , for example let there are 5 textviews inside one ListView then i want to get the text from that TextViews. I tried to further break the nodeInfo object by using:
for(int j=0; j<nodeInfo.getChildCount() ;j++){
AccessibilityNodeInfo subChild = nodeInfo.getChild(j);
CharSequence subText = subChild.getText();
Log.e("MyService: ","ListView child name: "+subText);
}
So what i am doing wrong in getting the values of TextView's inside ListView ,
or
it is not possible to do so?
I have found the solution by myself and the solution is:
event.getSource() method returns instance of AccessibilityNodeInfo and by converting that instance value to String format we can easily get the results. Just we need is to break the string and get the required result out, in my case the way i did is:
String get_cord= String.valueOf(event.getSource());
String[] first_split= get_cord.split("[;]");
for(int i=0; i<first_split.length; i++){
if(first_split[i].contains("text")){
String[] second_split= first_split[i].split(":");
user= second_split[1].trim();
System.out.println(user);
Log.e("MyService: ","username is: "+user);
break;
}
}`

Wrong arrangement of data from 'for loop'

I have the following code. I added my text fields dynamically. My desired result shown in Genymotion 5.0 (Google Nexus 5) but when I run my app in other devices/actual device the textfields get shuffled. Please help, Thanks in advance.
JSONObject jsonObject = new JSONObject(question.getSublabels());
final EditText[] editTextSublabels = new EditText[jsonObject.length()];
for (int i = 0; i < jsonObject.length(); i++) {
String names = jsonObject.names().get(i).toString();
editTextSublabels[i] = (EditText) LayoutInflater.from(activity).inflate(R.layout.sublabels, null);
editTextSublabels[i].setId(i);
editTextSublabels[i].setHint(jsonObject.getString(names));
sublabelsContainers.addView(editTextSublabels[i], params);
}
You cannot and should not rely on the ordering of elements within a JSON object.
In JSON, an object is defined thus:
An object is an unordered set of name/value pairs.
If you want order to be preserved, you need to redefine your data structure or put it inside a jsonarray
see http://www.json.org.
A JSONObject is a type of map. It does not preserve ordering. If you want to preserve ordering using JSON, you will need to use an array (and matching JSONArray in Java).

Increment hex value of R.id file element

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);

String usage as TextView ID

I would like to generate a random string(with some rules), than use it as a textview id. For example I would like to use settext with this string.
Purpose: I should select a textview randomly, than set its text to another.
Actually, there are different kinds of way to achieve this purpose. For instance you could have an array of texts that can be selected randomly.
String[] strArr = { "text1", "text2", "text3" };
Random rand = new Random();
int selected = rand.nextInt(3);
textView.setText(strArr[selected]);
If you MUST get the string from other textviews then you can create an array of IDs instead of an array of text. Then use the Random object to get an ID and then something like:
TextView textToGetString = (TextView) findViewById(idArray[selected]);
String newText = textToGetString.getText();
Your thought process seems a little complicated, but there could be a simpler solution. Ids are really only used by Android as placeholders for an integer. Instead of randomly generating an id's placeholder, you could populate an integer array with all the ids you want to use and then randomly select one from that array. Implementation could be as follows in your activity:
Random rand = new Random();
int[] myTextViews = new int[]{R.id.textView1, R.id.textView2, R.id.textView3}
int length = myTextViews.length;
TextView tV = (TextView)findViewById(myTextViews[rand.nextInt() % length]);
tV.setText("Whatever Text You Want");
I hope this helps! Good luck

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.

Categories

Resources