API 级别:12 (Android 3.1)
应用程序背景:通过我的 Android 应用程序,我向我们的 Web 服务器 MySQL 数据库发送请求以检索公司。所有这些都很好。最初我将返回的数据填充到一个 ListView
中,但为了获得老板喜欢的视觉效果,我有 6 个均匀分布的 TextView
。 (此应用仅适用于一台设备,即 Samsung Galaxy Tab 10.1)
问题: 因此,在解析返回的数据时,我有一个 for
循环。我想做的是自动递增一个变量,即 company_ + i
然后是 i++
。这在 Java 中可行还是有更好的方法?
这是我认为它会如何工作的示例:
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.");
抛出错误: 未定义参数类型 TextView、int 的运算符 +
。这是有道理的,但我习惯于在 PHP 中工作,我经常这样做。
这是我的布局:
<?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>
最佳答案
如果 Textview 保持不变,那么你可以这样做..
int[] companyIds = { R.id.company_id_1,
R.id.company_id_2, R.id.company_id_3,...};
在代码中这样做
TextView company_select = (TextView)findViewById(companyIds[i]);
关于java - 如何在 for 循环中自动递增变量名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11315630/