How can I auto increment variable names in for loop?

williamsmagic

New Member
API Level: 12 (Android 3.1)App Background: Through my Android app I am sending a request to our web servers MySQL database to retrieve companies. All of that works fine. Originally I had the returned data populate a \[code\]ListView\[/code\], but to get the visual my boss prefers I have 6 \[code\]TextView\[/code\]'s evenly distributed. (This app is designed to only work on ONE device, the Samsung Galaxy Tab 10.1)Question: So, in parsing the returned data I have a \[code\]for\[/code\] loop. What I would like to do, is auto increment a variable i.e. \[code\]company_ + i\[/code\] then \[code\]i++\[/code\]. Is this possible in Java or is there a better way?Here is an example of how I would think it would work:\[code\]int length = 5;//parse JSON datatry{ JSONArray jArray = new JSONArray(result); for(int i=0; i < length; i++){ TextView company = (TextView)findViewById(R.id.company_ + i); JSONObject jObject = jArray.getJSONObject(i); String businessName = jObject.getString("businessName"); double distance = jObject.getDouble("distance"); double distRound = 1e5; double roDistance = Math.round(distance * distRound) / distRound; company.setText(businessName); company.append(Html.fromHtml("<br>")); company.append("Approximate distance: " + roDistance + " feet.");\[/code\]Error Thrown: \[code\]The operator + is undefined for the argument type(s) TextView, int\[/code\]. Which makes sense, however I am used to working in PHP where I do this often.Here is my layout:\[code\]<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/location_select" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/row_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:weightSum="2" > <TextView android:id="@+id/company_1" android:layout_width="0dp" android:layout_height="200dp" android:layout_gravity="top|left" android:layout_weight="1" android:textIsSelectable="true" android:textSize="25sp" /> <TextView android:id="@+id/company_2" android:layout_width="0dp" android:layout_height="200dp" android:layout_gravity="top|right" android:layout_marginTop="0dp" android:layout_weight="1" android:textIsSelectable="true" android:textSize="25sp" /> </LinearLayout> <LinearLayout android:id="@+id/row_3" android:layout_width="match_parent" android:layout_height="wrap_content" android:weightSum="2" android:layout_below="@id/row_2" > <TextView android:id="@+id/company_3" android:layout_width="0dp" android:layout_height="200dp" android:layout_gravity="bottom|left" android:layout_marginLeft="0dp" android:layout_weight="1" android:textIsSelectable="true" android:textSize="25sp" /> <TextView android:id="@+id/company_4" android:layout_width="0dp" android:layout_height="200dp" android:layout_gravity="bottom|right" android:layout_marginRight="0dp" android:layout_weight="1" android:textIsSelectable="true" android:textSize="25sp" /> </LinearLayout> <LinearLayout android:id="@+id/row_4" android:layout_width="match_parent" android:layout_height="wrap_content" android:weightSum="2" android:layout_below="@id/row_3" > <TextView android:id="@+id/company_5" android:layout_width="0dp" android:layout_height="200dp" android:layout_gravity="bottom|left" android:layout_marginLeft="0dp" android:layout_weight="1" android:textIsSelectable="true" android:textSize="25sp" /> <TextView android:id="@+id/next_5" android:layout_width="0dp" android:layout_height="200dp" android:layout_gravity="bottom|right" android:layout_marginRight="0dp" android:layout_weight="1" android:textIsSelectable="true" android:textSize="25sp" /> </LinearLayout></RelativeLayout>\[/code\]
 
Back
Top