2013-02-28 75 views
0

該程序爲i的每個值給出一個Htm值。我想通過重複按計算按鈕輪流顯示每個值。我希望它顯示i = 0的值,然後當我按下按鈕時,它會變爲i = 1並顯示計算出的值。 我知道我的問題是有點模糊,但你的幫助將是非常讚賞.. 問候如何從for循環中依次顯示每個值

我的main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/main" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:text="Optimum Tilt Angle Calculator" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:text="Calculate" /> 

<TextView 
    android:id="@+id/beta" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:text="Beta = " 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<TextView 
    android:id="@+id/rad" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:text="H = " 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 


我傾斜。 Java的:

package com.ned.tilt; 

    import android.app.Activity; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.EditText; 
    import android.widget.TextView; 

    public class Tilt extends Activity implements android.view.View.OnClickListener { 
private static final double PI = 3.142; 
private static final double PHI = 24.8508 * (PI/180); 
private static final double Gsc = 1367; 
private static final double RO = 0.2; 
int i, beta, ang; 

double delta, ws, wss, Rb, Rb_num, Rb_den, Ra, 
     Htm, Ht; 
    double Hd[] = new double[12]; 
double Kt[] = new double[12]; 
double Gon[] = new double[12]; 
double Hom[] = new double[12]; 
int nbar[] = { 17, 47, 75, 105, 135, 162, 198, 228, 258, 288, 318, 344 }; 
int N[] = { 31, 28, 31, 30, 31, 30, 31, 30, 31, 30, 31, 30 }; 
double Hm[] = { 4.38, 5.18, 5.93, 6.65, 6.67, 6.40, 5.44, 5.27, 5.62, 5.24, 
     4.5, 4.11 }; 
Button cal; 
TextView radiation, angle; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    initialize(); 
    cal.setOnClickListener(this); 

} 

private void initialize() { 
    // TODO Auto-generated method stub 
    cal = (Button) findViewById(R.id.button1); 
    radiation = (TextView) findViewById(R.id.rad); 
    angle = (TextView) findViewById(R.id.beta); 
} 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    for (i = 0; i <= 11; i++) { 
     Htm = 0; 
     delta = 23.45 * (Math.sin((0.986301369) * (PI/180) 
       * (284 + (nbar[i])))); 

     ws = Math.acos(-(Math.tan(PHI)) * (Math.tan(delta * (PI/180)))); 
     ws = ws * (180/PI); 

     Gon[i] = Gsc 
       * (1 + (0.033 * Math.cos(0.986301369 * (nbar[i]) 
         * (PI/180)))); 

     Hom[i] = (86400/PI) 
       * Gon[i] 
       * (((Math.cos(PHI)) * (Math.cos(delta * (PI/180))) * (Math 
         .sin(ws * (PI/180)))) + ((ws * (PI/180)) * (Math 
         .sin(PHI) * (Math.sin(delta * (PI/180)))))); 
     Hom[i] = Hom[i]/3600000; 

     Kt[i] = Hm[i]/Hom[i]; 

     Hd[i] = (0.96268) - ((1.45200) * (Kt[i])) 
       + ((0.27365) * (Kt[i]) * (Kt[i])) 
       + ((0.04279) * (Kt[i]) * (Kt[i]) * (Kt[i])) 
       + ((0.000246) * (ws)) 
       + ((0.001189) * (90 - (PHI * (180/PI)) + delta)); 
     Hd[i] = Hd[i] * Hm[i]; 
     for (beta = 0; beta <= 90; beta++) { 
      wss = Math.acos(-(Math.tan((PHI) - (beta * (PI/180))) * (Math 
        .tan(delta * (PI/180))))); 
      wss = wss * (180/PI); 
      if (wss > ws) { 
       wss = ws; 
      } 

      Rb_num = ((Math.cos((PHI) - (beta * (PI/180)))) 
        * (Math.cos(delta * (PI/180))) * (Math.sin(wss 
        * (PI/180)))) 
        + (((PI/180) * wss) 
          * ((Math.sin((PHI) - (beta * (PI/180))))) * ((Math 
           .sin(delta * (PI/180))))); 
      Rb_den = ((Math.cos(PHI)) * (Math.cos(delta * (PI/180))) * (Math 
        .sin(ws * (PI/180)))) 
        + (((PI/180) * (ws) * (Math.sin(PHI)) * (Math 
          .sin(delta * (PI/180))))); 
      Rb = Rb_num/Rb_den; 
      Ra = ((1 - (Hd[i]/Hm[i])) * (Rb)) 
        + ((Hd[i]/(2 * Hm[i])) * (1 + (Math.cos(beta 
          * (PI/180))))) 
        + ((RO/2) * (1 - (Math.cos(beta * (PI/180))))); 

      Ht = Ra * Hm[i]; 
      if (Ht > Htm) { 
       Htm = Ht; 
       ang = beta; 
      } 
     } 

     break; 
    } 
    radiation.setText("H = " + Htm); 
    angle.setText("Beta =" + ang); 

} 
} 

回答

1

據我瞭解,你應該不要用你的for循環

for (i = 0; i <= 11; i++) 

for循環中刪除此,聲明一個變量,而退出的onClick增加你的變量時,將「我」來了。

private int counter=0; 

public void onClick(View v) { 
    int i=counter; 
    //everything continues, just remove the for loop 
    angle.setText("Beta =" + ang); 
    counter++; 
    if(counter==12) counter=0;//your arrays are fixed, so we are turning back 
} 
+1

再次感謝Serkan ...我是一個粉絲,真的...... :) – renupok92 2013-02-28 18:23:52

1

您希望如何顯示呢?只需將它們添加到TextView的字符串?

textView.setText(someString); 

TextView中的每一個代表一個新的計算的一個ListView?

private List<String> mCalculations = new LinkedList<String>(); 

private Adapter mAdapter; 

@Override 
public onCreate(Bundle instance) { 
    mAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, mCalculations); 
    ((ListView) findViewById(R.id.my_list_view)).setAdapter(mAdapter); 
} 

@Override 
public void onClick(View v) { 
    new AsyncTask<Void, Void, Double>() { 
     public Double doInBackground(Void... values) { 
      /* perform calculation */ 
      return value; 
     } 

     public void onPostExecute(Double... values) { 
      if(values.length <= 0) return; 
      String text = String.valueOf(values[0]); 
      mCalculations.add(String.valueOf(value)); 
      mAdapter.notifyDataSetChanged(); 
     } 
    }.execute(); 
} 
+1

runOnUiThread是什麼? – 2013-02-28 18:19:26

+0

適配器不喜歡更新它的視圖不在UI線程上:) – 2013-02-28 18:21:27

+1

onClick在UI線程上運行 – 2013-02-28 18:22:34

1

只需刪除你的電流回路,因爲你叫破一次迭代反正以後:

for (i = 0; i <= 11; i++) { 

然後,讓i一個字段變量,各按一次加一巴頓:

private int i = 0; 

@Override 
public void onClick(View v) { 
    // Do all your calculations and call setText() like above... 

    i++; 
    if(i == Hm.length) // Start over 
     i = 0; 
} 

(這假定每個數組都等於或大於Hm ...這是危險的。您應該執行更徹底的檢查。)

0

不要使用一個for循環,而不是創建一個全局變量i每次增加它,你從你的問題點擊按鈕

0

在你的onClick你開始一個循環

for (i = 0; i <= 11; i++) { 

這是設置我開始在任意時間均爲零單擊該按鈕。然後在循環結束時使用break來突破循環。這使得這個循環無用,你不需要這個部分的循環。

在你的初始化函數

你應該設置我等於零。然後在你的onClick函數中取出第一個for循環和你有中斷的地方,把它取出並用i ++代替。