API Level: 12 (Android 3.1)
API级别: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)
应用背景:通过我的Android应用程序,我向我们的web服务器MySQL数据库发送请求,以检索公司。所有这些都很有效。最初,我有返回的数据填充ListView,但是为了得到我的老板更喜欢的视觉,我有6个TextView的均匀分布。(这款应用只适用于三星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?
问题:因此,在解析返回的数据时,我有一个for循环。我想做的是,自动递增一个变量,例如company_ + I然后I ++。这在Java中是可能的还是有更好的方法?
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.
抛出错误:操作符+未定义为参数类型TextView (s) int.这是有意义的,但是我习惯在PHP中工作,我经常这样做。
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>
4 个解决方案
#1
3
If Textview remains constant, then you can do this..
如果Textview保持不变,那么可以这样做。
int[] companyIds = { R.id.company_id_1,
R.id.company_id_2, R.id.company_id_3,...};
and in code do this
在代码中做这个
TextView company_select = (TextView)findViewById(companyIds[i]);
#2
1
The way you are doing is not possible.
你这样做是不可能的。
You could use Reflection but that would be overkill. Try the following
你可以使用反射,但那是多余的。试试以下
List<Integer> companies= Arrays.asList(R.id.company_1,R.id.company_2,R.id.company_3,R.id.company_4,R.id.company_5);
And get it with:
并得到:
companies.get(i%5);
#3
1
Are you really sure about this line,
你真的确定这条线,
TextView company_select = company_ + i;
?? That line throws the error (Exception).
? ?这行抛出错误(异常)。
What's the meaning of that line, What you want to achieve?
这句话的意思是什么,你想要实现什么?
Just store your textview's id in int array..
只需将textview的id存储在int数组中。
int[] textviewIds = { R.id.company_id_1,
R.id.company_id_2, R.id.company_id_3,R.id.company_id_4,R.id.company_id_5...};
and use this array in your for loop,
在for循环中使用这个数组,
TextView company_select = (TextView)findViewById(textviewIds[i]);
#4
0
You can use Integer.toString()
Then the call will be TextView company_select = company_ + Integer.toString(i);
可以使用Integer.toString()然后调用TextView company_select = company_ + Integer.toString(i);
#1
3
If Textview remains constant, then you can do this..
如果Textview保持不变,那么可以这样做。
int[] companyIds = { R.id.company_id_1,
R.id.company_id_2, R.id.company_id_3,...};
and in code do this
在代码中做这个
TextView company_select = (TextView)findViewById(companyIds[i]);
#2
1
The way you are doing is not possible.
你这样做是不可能的。
You could use Reflection but that would be overkill. Try the following
你可以使用反射,但那是多余的。试试以下
List<Integer> companies= Arrays.asList(R.id.company_1,R.id.company_2,R.id.company_3,R.id.company_4,R.id.company_5);
And get it with:
并得到:
companies.get(i%5);
#3
1
Are you really sure about this line,
你真的确定这条线,
TextView company_select = company_ + i;
?? That line throws the error (Exception).
? ?这行抛出错误(异常)。
What's the meaning of that line, What you want to achieve?
这句话的意思是什么,你想要实现什么?
Just store your textview's id in int array..
只需将textview的id存储在int数组中。
int[] textviewIds = { R.id.company_id_1,
R.id.company_id_2, R.id.company_id_3,R.id.company_id_4,R.id.company_id_5...};
and use this array in your for loop,
在for循环中使用这个数组,
TextView company_select = (TextView)findViewById(textviewIds[i]);
#4
0
You can use Integer.toString()
Then the call will be TextView company_select = company_ + Integer.toString(i);
可以使用Integer.toString()然后调用TextView company_select = company_ + Integer.toString(i);