简体   繁体   English

如何在for循环中自动递增变量名?

[英]How can I auto increment variable names in for loop?

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. 应用背景:通过我的Android应用,我正在向Web服务器MySQL数据库发送请求以检索公司。 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. 最初,我将返回的数据填充到ListView ,但是要得到我的老板更喜欢的视觉效果,我平均分配了6个TextView (This app is designed to only work on ONE device, the Samsung Galaxy Tab 10.1) (此应用旨在仅在一种设备上运行,即Samsung Galaxy Tab 10.1)

Question: So, in parsing the returned data I have a for loop. 问题:因此,在解析返回的数据时,我有一个for循环。 What I would like to do, is auto increment a variable ie company_ + i then i++ . 我想做的是自动递增一个变量,即company_ + i然后i++ Is this possible in Java or is there a better way? 这在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 . 引发错误: 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. 这是有道理的,但是我习惯于在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>

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]);

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]);

The way you are doing is not possible. 您所做的方式是不可能的。

You could use Reflection but that would be overkill. 您可以使用Reflection,但这太过分了。 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);

You can use Integer.toString() 您可以使用Integer.toString()
Then the call will be TextView company_select = company_ + Integer.toString(i); 然后,调用将为TextView company_select = company_ + Integer.toString(i);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM