2012-12-27 32 views
1

我有一個12值的數組,我想要一個接一個地顯示它。最初在應用程序運行時,我顯示了6個值。在按鈕上單擊我想要逐個顯示其他6個值。向按鈕顯示值的數組

prevbutton-> value1 value2 value3 value4 value5 value6 -> nextbutton 

所以在下一個按鈕點擊它應該是這樣的。

prevbutton-> value2 value3 value4 value5 value6 value7 -> nextbutton 

這應該持續到12個值,以及反向應prevbutton的情況下發生的。

我已經使用這個代碼,但它不工作:

public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.radionextstn: 
     forwardtune(); 
     break; 
    } 
} 

public void forwardtune(){ 
    Button[] buttons = new Button[5]; 
    buttons[0] = (Button)findViewById(R.id.radiostation1); 
    buttons[1] = (Button)findViewById(R.id.radiostation2); 
    buttons[2] = (Button)findViewById(R.id.radiostation3); 
    buttons[3] = (Button)findViewById(R.id.radiostation4); 
    buttons[4] = (Button)findViewById(R.id.radiostation5); 
    buttons[5] = (Button)findViewById(R.id.radiostation6); 
    if(currentlyDisplayingFromPosition + 6 >= arraySize) 
     return; 
    for(int i=0; i<arraySize; i++) { 
     buttons[i].setText(""); 
    } 
    for(int i=currentlyDisplayingFromPosition; i<=currentlyDisplayingFromPosition+6; i++){ 
     if (frequency[i] == 0.0) 
      buttons[i].setText(""); 
     else 
      buttons[i].setText("" + frequency[0]); 
    } 
} 

我顯示的前6個值是這樣的:

public void autoTune() { 
    Log.i("autotune", " called"); 

    if (frequency[0] == 0.0) 
     station1.setText(""); // station1 is a button 
    else 
     station1.setText("" + frequency[0]); // station1 is a button 
    if (frequency[1] == 0.0) 
     station2.setText(""); // station2 is a button 
    else 
     station2.setText("" + frequency[1]); // station2 is a button 
    if (frequency[2] == 0.0) 
     station3.setText(""); // station3 is a button 
    else 
     station3.setText("" + frequency[2]); // station3 is a button 
    if (frequency[3] == 0.0) 
     station4.setText(""); // station4 is a button 
    else 
     station4.setText("" + frequency[3]); // station4 is a button 
    if (frequency[4] == 0.0) 
     station5.setText(""); // station5 is a button 
    else 
     station5.setText("" + frequency[4]); // station5 is a button 
    if (frequency[5] == 0.0) 
     station6.setText(""); // station6 is a button 
    else 
     station6.setText("" + frequency[5]); // station6 is a button 
} 

@Override 
protected Boolean doInBackground(Context... params) { 
    // TODO Auto-generated method stub 
    Log.i("in autotune", " before freq length"); 
    int[] freq = { 911, 943, 947, 932, 901, 964, 843, 835, 946,904,908,873 }; 
    freqCounter = 0; 
    Log.i("in autotune", "after freq length : " + freq.length); 
    frequency = new double[freq.length]; 
    Log.i("in autotune", "after frequency length : " + frequency.length); 
    for (int k = 0; k < freq.length; k++) { 
     Log.i("In Radio.java", "Freq : " + freq[k]); 
     frequency[k] = freq[k]; 
     frequency[k] = frequency[k]/10; 
     if (frequency[k] == 0.0) 
      break; 
     freqCounter++; 
     Log.i("In Radio.java", "Frequency : " + frequency[k]); 
    } 
} 
+0

創建多個問題,不會增加你的答案的機會http://stackoverflow.com/questions/14055253/android-display-the-array-of-values-one-after-the-other-according按鈕 – Budius

回答

0

我看到的第一個錯誤,你聲明的5陣列按鈕,並嘗試把6個按鈕:

Button[] buttons = new Button[5]; 

你應該這樣做:

Button[] buttons = new Button[6]; 
+0

非常感謝,哦,你的權利。我改變了我的代碼,因爲你說,但沒有運氣 – Randroid

+0

你的數組大小不是12但是13 :-) – gezdy

+0

不要爲eack onClick事件調用findViewById(),而是爲所有6個按鈕創建一個實例變量。 – gezdy

0

解決方案:

if(currentlyDisplayingFromPosition + 6 >= arraySize) 
    return; 

for(int i=0; i<6; i++){ 
    buttons[i].setText("" + frequency[currentlyDisplayingFromPosition+i]); 
} 
+0

感謝您的代碼 – Randroid

0

修改它根據自己的需要。

Button[] btns = new Button[8]; 
List<String> pages = new ArrayList<String>(); 
int mStart = 1; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_dialog_activiy); 
    btns[0] = (Button) findViewById(R.id.pr); 
    btns[1] = (Button) findViewById(R.id.b1); 
    btns[2] = (Button) findViewById(R.id.b2); 
    btns[3] = (Button) findViewById(R.id.b3); 
    btns[4] = (Button) findViewById(R.id.b4); 
    btns[5] = (Button) findViewById(R.id.b5); 
    btns[6] = (Button) findViewById(R.id.b6); 
    btns[7] = (Button) findViewById(R.id.nt); 

    btns[0].setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      rearrangeBy(-1); 
     } 
    }); 
    btns[btns.length - 1].setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      rearrangeBy(1); 
     } 
    }); 
    // setup listeners 
    for (int i = 1; i < btns.length - 1; i++) { 
     btns[i].setOnClickListener(mClickListener); 
    } 
    for (int i = 0; i < 12; i++) { 
     pages.add((i + 1) + ""); 
    } 
    setUpButtons(); 
} 

final View.OnClickListener mClickListener = new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     Toast.makeText(DialogActiviy.this, v.getTag() + "", 
       Toast.LENGTH_SHORT).show(); 

    } 
}; 

private void rearrangeBy(int by) { 
    mStart += by; 
    setUpButtons(); 
} 

private void setUpButtons() { 
    // setup previous button 
    btns[0].setEnabled(!(mStart == 1)); 
    // setup next button 
    int end = pages.size() - btns.length + 2 + 1; 
    btns[btns.length - 1].setEnabled(!(mStart == end)); 
    // setup intermediate buttons 
    for (int i = 1; i < btns.length - 1; i++) { 
     String text = (mStart + i - 1) + ""; 
     btns[i].setText(text); 
     btns[i].setTag(text); 
    } 
} 
+0

感謝您的代碼 – Randroid