2012-11-02 63 views
0

我想設置一個微調項目等於到一個特定整數。我使用onItemSelected,試圖做到這一點,但問題是,我需要整數設置爲項目選擇在同一行乘以另一個的EditText整數。爲此我想mulitply由行混爲一談了一下,我所有的EditTexts的,是參考到一個列表,然後迭代,這樣我可以添加他們都放棄了。我如何獲得每個的EditText在同一行的選擇微調項目(它有一個INT值)到選擇微調項目?這是我到目前爲止...... 對不起!如何設置一個微調項目等於一個值

int count = 1; 
double gradeValue; 
List<EditText> allEd = new ArrayList<EditText>(); 
List<Spinner> allSp = new ArrayList<Spinner>(); 


@SuppressWarnings("deprecation") 
public void onClick(View v) { 
    TableLayout tableLayout1 = (TableLayout) findViewById(R.id.tableLayout1); 

    switch(v.getId()){ 
    case R.id.button1: 
     if(count != 16){ 
      count++; 

      // Create the row only when the add button is clicked 
      TableRow tempRow = new TableRow(MainActivity.this); 
      EditText tempText1 = new EditText(MainActivity.this); 
      EditText tempText2 = new EditText(MainActivity.this); 
      TextView tempTextView = new TextView(MainActivity.this); 
      Spinner spinnerTemp = new Spinner(MainActivity.this); 
      EditText editText1 = (EditText) findViewById(R.id.editText1); 
      EditText editText2 = (EditText) findViewById(R.id.editText2); 
      TextView textView3 = (TextView) findViewById(R.id.textView3); 
      Spinner s = (Spinner) findViewById(R.id.spinner1); 
      tempTextView.setText(count + "."); 

      tempRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
      tempText1.setLayoutParams(editText1.getLayoutParams()); 
      tempText2.setLayoutParams(editText2.getLayoutParams()); 
      tempTextView.setLayoutParams(textView3.getLayoutParams()); 
      tempText1.setInputType(InputType.TYPE_CLASS_TEXT); 
      tempText2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL); 
      tempText2.setId(count); 
      spinnerTemp.setLayoutParams(s.getLayoutParams()); 
      spinnerTemp.setId(count); 
      String options[] = { "A+", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "D-", "F" }; 

      ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, options); 
      spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view 
      spinnerTemp.setAdapter(spinnerArrayAdapter); 

      allEd.add(tempText2); 
      allEd.add(editText2); 
      allSp.add(spinnerTemp); 

      tempRow.addView(tempTextView); 
      tempRow.addView(tempText1); 
      tempRow.addView(tempText2); 
      tempRow.addView(spinnerTemp); 
      tableLayout1.addView(tempRow); 
     } 
     } 
     break; 
    case R.id.button2: 
     if(count != 1){ 
      count--; 
      tableLayout1.removeView(tableLayout1.getChildAt(count)); 
     } 

     break; 
    case R.id.button3: 
     int calculation = 0; 
     double spinnerChoice = 0; 
     for(int i = 0; i < allEd.size(); i++) { 
      EditText totalUnits = allEd.get(i); 
      try { 
      int units = Integer.parseInt(totalUnits.getText().toString()); 

      calculation += units; 
      }catch (Exception e) { 
       //ignore 
      } 

     for(int j = 0; j < allSp.size(); j++) { 
      Spinner gradeTotal = allSp.get(j); 
      try { 
      int grades = gradeTotal.getCount(); 

      }catch (Exception e) { 
       //ignore 
      } 

     } 
    } 
    } 

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 
    if (pos == 0){ 
     //A+ 
     gradeValue = 4.0; 
    } else if (pos == 1){ 
     //A 
     gradeValue = 4.0; 
    } else if (pos == 2){ 
     //A- 
     gradeValue = 3.7; 
    } else if (pos == 3){ 
     //B+ 
     gradeValue = 3.3; 
    } else if (pos == 4){ 
     //B 
     gradeValue = 3.0; 
    } else if (pos == 5){ 
     //B- 
     gradeValue = 2.7; 
    } else if (pos == 6){ 
     //C+ 
     gradeValue = 2.3; 
    } else if (pos == 7){ 
     //C 
     gradeValue = 2.0; 
    } else if (pos == 8){ 
     //C- 
     gradeValue = 1.7; 
    } else if (pos == 9){ 
     //D+ 
     gradeValue = 1.3; 
    } else if (pos == 10){ 
     //D 
     gradeValue = 1.0; 
    } else if (pos == 11){ 
     //D- 
     gradeValue = 0.7; 
    } else if (pos == 12){ 
     //F 
     gradeValue = 0.0; 
    } 
} 
} 
+0

您可能想嘗試減少你的問題的代碼可能的最小量發佈之前。這很可能會得到積極的迴應。 –

回答

0

這看起來比它應該是因爲你使用表格佈局更難。如果你所有的行都是相似的並且對你的設計沒有害處,那麼你可能會考慮使用一個listview。那麼你可以簡單地通過它們的位置來引用同一行上的東西 - 方便地作爲參數提供。

但是既然你有這個樣子,你可以得到選定的旋轉器項目在它在arraylist中的位置的索引。然後您可以引用EditText數組列表。關於我的頭頂,我認爲你做了一個setTag()到你選定的微調項目作爲標籤。然後,您循環查找標籤並在存在的情況下執行操作。這是我想出來的。請注意,我改變了你的onitemselected方法返回一個雙的方法,因爲我發現,對於這樣的任務更加有用:

private double calcGradeValue (int pos) { 
     if (pos == 0){ 
      //A+ 
      gradeValue = 4.0; 
     } else if (pos == 1){ 
      //A 
      gradeValue = 4.0; 
     } else if (pos == 2){ 
      //A- 
      gradeValue = 3.7; 
     } else if (pos == 3){ 
      //B+ 
      gradeValue = 3.3; 
     } else if (pos == 4){ 
      //B 
      gradeValue = 3.0; 
     } else if (pos == 5){ 
      //B- 
      gradeValue = 2.7; 
     } else if (pos == 6){ 
      //C+ 
      gradeValue = 2.3; 
     } else if (pos == 7){ 
      //C 
      gradeValue = 2.0; 
     } else if (pos == 8){ 
      //C- 
      gradeValue = 1.7; 
     } else if (pos == 9){ 
      //D+ 
      gradeValue = 1.3; 
     } else if (pos == 10){ 
      //D 
      gradeValue = 1.0; 
     } else if (pos == 11){ 
      //D- 
      gradeValue = 0.7; 
     } else if (pos == 12){ 
      //F 
      gradeValue = 0.0; 
     } 
     return gradeValue; 
    } 

    public void spinnerLoop() { 
     for(int i = 0; i < allSp.size(); i++) { 
      if (allSp.get(i).getTag().toString() == "TAGGED!") { 
       double gradeValue = calcGradeValue(allSp.get(i).getSelectedItemPosition()); 
       double calculation = (gradeValue) * (Integer.parseInt(allEd.get(i).getText().toString())); 
//    do something with your calculation double 
      } 
     } 
    } 
+0

對不起,延遲迴復,但這對我有用!謝謝! – user1460211

+0

很多if/else條件。如果您必須使用它們,請考慮使用switch語句。 – Marko

2

通過適配器循環尋找您想要匹配的值。跟蹤循環的索引。找到匹配項後,將微調器的selectedItem設置爲該索引。

相關問題