繁体   English   中英

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

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

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.");

引发错误: The operator + is undefined for the argument type(s) 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]);

您真的确定这条线吗?

TextView company_select = company_ + i; 

?? 该行将引发错误(异常)。

那条线是什么意思,你想实现什么?

只需将您的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...};

并在您的for循环中使用此数组,

TextView company_select = (TextView)findViewById(textviewIds[i]);

您所做的方式是不可能的。

您可以使用Reflection,但这太过分了。 尝试以下

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

并获得它:

companies.get(i%5);

您可以使用Integer.toString()
然后,调用将为TextView company_select = company_ + Integer.toString(i);

暂无
暂无

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

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