Trying to Reference a Button With a String - java

I have a situation in an app I'm making where I have an activity that has 15 buttons and a string[] that contains 15 strings. I'm looking for an easy way to assign each string in the string[] to its corresponding button. I was hoping I could somehow do something like:
for(int i; i<myStringArray.length; i++){
String ref = "btn" + (i + 1);
ref.setText(resultString[i]);
}
where all the buttons are labeled "btn1", "btn2", etc so that they could all be accessed with the string "ref". Obviously this doesn't work so I was wondering if there was another way of doing something similar to this instead of doing:
btn1.setText(resultString[0]);
btn2.setText(resultString[1]);
btn3.setText(resultString[2]);
...
thanks for the help!

I think you can solve this by having a Button[] table where you store all your Button objects and then you can access them by index instead of the actual object name:
Button[] btns = new Button[15]
for(int i = 0; i < btns.length; i++) {
btns[i].setText(resultString[i]);
}
Hope that helps.
EDIT: Ofcourse you have to fill your Button[] btns with your objects first.

You can do what you tried using reflection. It would be way better in your case to store your buttons in an Array or a Map instead though.

Related

How can I iterate thought JLabel objects to add a icon to a specific one

so I'm bit new to this. So I have a bunch of JLabels (javax.swing.JLabel) and want to be able to loop through them adding a icon to a specific one that could change each time.
private void myMethod(JLabel l){
int x = 6;
for(int i = 0; i <10; i++){
if(x == 6){
l.setIcon(set icon to something nice);
break;
}else{
l++;
}
}
}
I basically want to do this, how would I go about doing this? Thanks!
Okay yeah that was a very easy solution. Thanks GhostCat and maloomesiter! For some reason I was way overthinking that. Just putting the objects in an arraylist and doing it that way was very straightforward once you guys suggested it. Thanks!

How to assign dynamic names in Java Android?

How do you initialise the name of a TextView or any other Android component with a name only known at runtime?
For example:
LinearLayout linLayout = (LinearLayout) findViewById(R.id.dynamicLayout);
linLayout.setOrientation(LinearLayout.VERTICAL);
for(int i = 1; i < 5; i++){
String dynamic_name = "myTextView"+i;
//Create new textView named dynamically
TextView dynamic_name = new TextView(this);
//style textView here etc
linLayout.addView(dynamic_name);
}
How do you initialise the name of a textView or any other Android component with a name only known at runtime?
You can't.
TextView this_variable_changes is defined at compile time while String this_variable_changes changes at runtime.
Then how to create a variable amount of textViews based on a number that can only be known at run-time?
Once you've got the amount of TextViews at runtime, programmatically create that much TextViews and add those to a layout.
You don't need dynamic names by the reason mentioned above. What you can do is to map the received amount to the corresponding number of TextViews, i.e. use Map with the keys indexed up to the received amount and values of TextViews.
In Java, but you can create Maps or Arrays to deal with your particular issue. When you encounter an issue that makes you think "I need my variables to change names dynamically" you should try and think "associative array". In Java, you get associative arrays using Maps.
That is, you can keep a List of your arrays, something like:
List<String[]> kList = new ArrayList<String[]>();
for(int k = 0; k < 5; k++){
kList.add(new String[3]);
}
Or perhaps a little closer to what you're after, you can use a Map:
Map<Integer,String[]> kMap = new HashMap<Integer,String[]>();
for(int k = 0; k < 5; k++){
kMap.put(k, new String[3]);
}
// access using kMap.get(0) etc..
Simple solution for me was going to your cmd type ipconfig Copy your IP v4 address and paste in android emulator host
http://your-ip-v4-address:3000/

Use JTextField to List items

I'm not too familiar with Java GUI programming, and I wanted to do something where I have a loop that spits out a list of stuff and have the JTextField render it in the order it comes out.
I just do not know how the second parameter of the JTextField insert() function works. Right now when I do something like:
for(int i = 0; i < list.size(); i++){
textArea.insert(list.get(i), 0);
}
It does what I want, except it lists everything in backwards order that I put it in. I want it to display everything the other way around.
Thank you for any advice.
All you need to define a temporary string, result and for every item in the list add the string representation to that variable. When you have looped through everything, all you need to do is textArea.setText(result).
String result = "";
for(int i = 0; i < list.size(); i++)
{
result += list.get(i).toString();
}
textArea.setText(result);

set array values in to a text area in java

i want to set an array list value in to a text area.
this is my code,
ArrayList<String> phone_numberArray = db.getNumberSms();
for(int i=0; i<phone_numberArray.size(); i++){
jtext_area.addElement(phone_numberArray.get(i));
}
this code getting errors.
some one help me please....
I assume jtext_area is an instance of JTextArea , I don't see any addElement() in that class, You might be looking for append()
Try:
for(String number : db.getNumberSms()) {
jtext_area.append(number);
}

Java Can Variables Be Used in Variable Names

I have this little script repeated below in my code a few times. I know that I could run a function to do this easily, but can I use a variable as a variable name like in PHP.
if (4val != null && 4val.length() > 0){
Button 4 = new Button(this);
4.setText(4val);
4.setTextSize(20);
}
I want to be able to do something like
i=1;
while{i > 10}{
$$i = value;
//do stuff with $$i
i++;
}
Is this possible in Java?
No. But you can stick the buttons in an array, and then iterate through.
Button[] buttons = new Button[10];
// instantiate all the buttons
for (int i = 0; i < buttons.length; i++) {
// update the button
}
use Map instead
map.put("key","val");
map.get("key");
Well you can also use array, List , but if you use HashMap you retrieval process would be almost o(1)

Categories