0

我正在尝试设置一个微调器项目Equal to a Specific Integer。我正在使用onItemSelected来尝试完成此操作,但问题是,我需要为所选项目设置的整数乘以Integer同一行中的另一个EditText。稍微混淆一下,我试图乘以的行中的所有EditTexts都被引用到一个列表,然后进行迭代,以便我可以将它们全部加起来。如何将每个EditText与Selected Spinner Item放在同一行中(它有一个Int Value乘以Selected Spinner Item?到目前为止,这是我所拥有的……对不起,一团糟!

    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;
    }
}
}
4

2 回答 2

2

遍历适配器以查找要匹配的值。跟踪循环的索引。找到匹配项后,将微调器的 selectedItem 设置为该索引。

于 2012-11-02T00:35:15.623 回答
0

这看起来比应该的要难,因为您使用的是表格布局。如果您的所有行都相似并且对您的设计无害,那么您可能会考虑使用列表视图。那么您可以简单地通过它们的位置来引用同一行上的事物 - 作为参数方便地提供。

但是既然你有这样的,你可以在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
            }
        }
    }
于 2012-11-02T03:31:16.667 回答