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 ListView, but to get the visual my boss prefers I have 6 TextView‘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 for loop. What I would like to do, is auto increment a variable i.e. company_ + i then i++. Is this possible in Java or is there a better way?
Here is an example of how I would think it would work:
int length = 5;
//parse JSON data
try{
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.");
Error Thrown: The operator + is undefined for the argument type(s) TextView, int. Which makes sense, however I am used to working in PHP where I do this often.
Here is my layout:
<?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>
If Textview remains constant, then you can do this..
and in code do this