在计算时收到无效

时间:2014-01-25 18:01:25

标签: android

我在Android上进行计算,它将在文本视图中显示。

current odometer(edit text) - previous odometer(edit text) / number of fuel pump.

我在我的代码中做过类似的事情,但它似乎没有用。 每当我想将之前的里程表(编辑文本)更改为其他值时, 文本视图将显示-infinity。

我想知道为什么会这样。 我希望我已经足够清楚了。

有人能帮助我吗?

这是我的代码:

 odometerEdit.addTextChangedListener(new TextWatcher() {
                   @Override
                   public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                   }

                   @Override
                   public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                   }



                   @Override
                   public void afterTextChanged(Editable editable) {
                      //here, after we introduced something in the EditText we get the string from it



                       if(!odometerEdit.getText().toString().trim().equalsIgnoreCase(""))
                           odmEditStr = odometerEdit.getText().toString().trim();
                       if(!odometerPreEdit.getText().toString().trim().equalsIgnoreCase(""))
                           preOdm = odometerPreEdit.getText().toString().trim();


                      if(!odometerEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase("") && !odometerPreEdit.getText().toString().trim().equalsIgnoreCase(""))
                         {

                           resultCon = Double.parseDouble(odmEditStr) - Double.parseDouble(preOdm) / Double.parseDouble(pumpEditStr);              
                           fconView.setText(" "+resultCon);
                         }
                   }


               });

               odometerPreEdit.addTextChangedListener(new TextWatcher() {
                   @Override
                   public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                   }

                   @Override
                   public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                   }



                   @Override
                   public void afterTextChanged(Editable editable) {
                      //here, after we introduced something in the EditText we get the string from it



                       if(!odometerEdit.getText().toString().trim().equalsIgnoreCase(""))
                           odmEditStr = odometerEdit.getText().toString().trim();
                       if(!odometerPreEdit.getText().toString().trim().equalsIgnoreCase(""))
                           preOdm = odometerPreEdit.getText().toString().trim();


                      if(!odometerEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase("") && !odometerPreEdit.getText().toString().trim().equalsIgnoreCase(""))
                         {

                           resultCon = Double.parseDouble(odmEditStr) - Double.parseDouble(preOdm) / Double.parseDouble(pumpEditStr);              
                           fconView.setText(" "+resultCon);
                         }
                   }


               });

这是我的完整源代码:

public class updateAnddelete extends Activity implements OnClickListener {


        // TableLayout tablelayout_Log = null;
       private Button deleteButton;
       private Button updateButton;
       private Button cancelButton;
       // Button searchButton = null;
       private    static EditText dateEdit; 
       private    EditText priceEdit;
       private    EditText pumpEdit;
       private  TextView costView;
       private   EditText odometerEdit;
       private   EditText odometerPreEdit;
       private   TextView fconView;
       private     TextWatcher textWatcher;
     private     String priceEditStr ="0.0",pumpEditStr="0.0";
      private     String  odmEditStr = "";
       private     String preOdm = "";
       private String bundleDate;
        private String bundlePrice;
        private String bundlePump;
        private String bundleCost;
        private String bundleOdometer;
        private String bundleFcon;


        private String dateEditTextValue;
        private String priceEditTextValue;
        private String pumpEditTextValue;
        private String costTextViewValue;
        private String odometerEditTextValue;
        private String fconTextViewValue;
    double result;
    double resultCon;
    private int mYear;
    private int mMonth;
    private int mDay;
    static final int DATE_DIALOG_ID = 0;




        public boolean isNumeric(String str)
        {
            return str.matches("-?\\d+(\\.\\d+)?"); 
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.update);
            costView = (TextView)findViewById(R.id.tcost);
            dateEdit = (EditText)findViewById(R.id.date);
            priceEdit = (EditText)findViewById(R.id.fuelprice);
            pumpEdit = (EditText)findViewById(R.id.fuelpump);
            odometerEdit = (EditText)findViewById(R.id.odometer);
            odometerPreEdit = (EditText)findViewById(R.id.odometerPre);
            fconView = (TextView)findViewById(R.id.fcon);

            cancelButton = (Button) findViewById(R.id.cancelBTN);
            cancelButton.setOnClickListener(this);
            updateButton = (Button) findViewById(R.id.updateBTN);
            updateButton.setOnClickListener(this);
            deleteButton = (Button) findViewById(R.id.deleteBTN);
            deleteButton.setOnClickListener(this);
            Bundle takeBundledData = getIntent().getExtras();

            // First we need to get the bundle data that pass from the UndergraduateListActivity
            bundleDate = takeBundledData.getString("clickedDate");
            bundlePrice = takeBundledData.getString("clickedPrice");
            bundlePump = takeBundledData.getString("clickedPump");
            bundleCost = takeBundledData.getString("clickedCost");
            bundleOdometer = takeBundledData.getString("clickedOdometer");
            bundleFcon = takeBundledData.getString("clickedFCon");

            // Set the values that we extracted from the Bundle in the EditText fields
            dateEdit.setText(bundleDate);
            priceEdit.setText(bundlePrice);
            pumpEdit.setText(bundlePump);
            costView.setText(bundleCost);
            odometerEdit.setText(bundleOdometer);
            fconView.setText(bundleFcon);






            dateEdit.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                   // showDialog(DATE_DIALOG_ID);
                    DialogFragment newFragment = new DatePickerFragment();
                    newFragment.show(getFragmentManager(), "datePicker");
                }
            });


               priceEdit.addTextChangedListener(new TextWatcher() {

                   @Override
                   public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                   }

                   @Override
                   public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                   }

                   @Override
                   public void afterTextChanged(Editable editable) {
                        if (! android.text.TextUtils.isEmpty(priceEdit.getText())) {
                            priceEditStr = priceEdit.getText().toString().trim();
                        }

                        if (! android.text.TextUtils.isEmpty(pumpEdit.getText())) {
                            pumpEditStr = pumpEdit.getText().toString().trim();
                        }

                        result = Double.parseDouble(priceEditStr) * Double.parseDouble(pumpEditStr);              
                        costView.setText(" "+result);


                   }
               });

               pumpEdit.addTextChangedListener(new TextWatcher() {

                   @Override
                   public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                   }

                   @Override
                   public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                   }

                   @Override
                   public void afterTextChanged(Editable editable) {
                      //here, after we introduced something in the EditText we get the string from it
                       if(!priceEdit.getText().toString().trim().equalsIgnoreCase(""))
                            priceEditStr = priceEdit.getText().toString().trim();
                       if(!pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
                            pumpEditStr = pumpEdit.getText().toString().trim();


                       if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
                          {
                            result = Double.parseDouble(priceEditStr) * Double.parseDouble(pumpEditStr);              
                            costView.setText(" "+result);
                          }

                   }
               });







               odometerEdit.addTextChangedListener(new TextWatcher() {
                   @Override
                   public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                   }

                   @Override
                   public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                   }



                   @Override
                   public void afterTextChanged(Editable editable) {
                      //here, after we introduced something in the EditText we get the string from it



                       if(!odometerEdit.getText().toString().trim().equalsIgnoreCase(""))
                           odmEditStr = odometerEdit.getText().toString().trim();
                       if(!odometerPreEdit.getText().toString().trim().equalsIgnoreCase(""))
                           preOdm = odometerPreEdit.getText().toString().trim();


                      if(!odometerEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase("") && !odometerPreEdit.getText().toString().trim().equalsIgnoreCase(""))
                         {

                           resultCon = Double.parseDouble(odmEditStr) - Double.parseDouble(preOdm) / Double.parseDouble(pumpEditStr);              
                           fconView.setText(" "+resultCon);
                         }
                   }


               });

               odometerPreEdit.addTextChangedListener(new TextWatcher() {
                   @Override
                   public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                   }

                   @Override
                   public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                   }



                   @Override
                   public void afterTextChanged(Editable editable) {
                      //here, after we introduced something in the EditText we get the string from it



                       if(!odometerEdit.getText().toString().trim().equalsIgnoreCase(""))
                           odmEditStr = odometerEdit.getText().toString().trim();
                       if(!odometerPreEdit.getText().toString().trim().equalsIgnoreCase(""))
                           preOdm = odometerPreEdit.getText().toString().trim();


                      if(!odometerEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase("") && !odometerPreEdit.getText().toString().trim().equalsIgnoreCase(""))
                         {

                           resultCon = Double.parseDouble(odmEditStr) - Double.parseDouble(preOdm) / Double.parseDouble(pumpEditStr);              
                           fconView.setText(" "+resultCon);
                         }
                   }


               });

        }



        @Override
        public void onClick(View v) {

            // We need to update or delete details which is in the EditText fields after user edit the values
            // These values are the ContentValues that we are going to use in future
            dateEditTextValue = dateEdit.getText().toString();
            priceEditTextValue = priceEdit.getText().toString();
            pumpEditTextValue = pumpEdit.getText().toString();
            costTextViewValue = costView.getText().toString();
            odometerEditTextValue = odometerEdit.getText().toString();
            fconTextViewValue = fconView.getText().toString();



            // It is easy to set values to the POJO class and pass the class instance to the updateUgraduateDetails() method
            fuelLogPojo fuelLogPojo = new fuelLogPojo();

            fuelLogPojo.setdate(bundleDate);
            fuelLogPojo.setprice(bundlePrice);
            fuelLogPojo.setpump(bundlePump);
            fuelLogPojo.setcost(bundleCost);
            fuelLogPojo.setodometer(bundleOdometer);
            fuelLogPojo.setfcon(bundleFcon);




            if(v.getId() == R.id.cancelBTN){
                finish();
            }else if(v.getId() == R.id.updateBTN){
                updateLog(fuelLogPojo);
            }else if(v.getId() == R.id.deleteBTN){
                deleteLog(fuelLogPojo);
            }
        }



        private void deleteLog(fuelLogPojo deletefuelLogPojo) {
            // TODO Auto-generated method stub

            AndroidOpenDbHelper androidOpenDbHelper = new AndroidOpenDbHelper(this);
            SQLiteDatabase sqliteDatabase = androidOpenDbHelper.getWritableDatabase();

            String[] whereClauseArgument = new String[1];
            whereClauseArgument[0] = deletefuelLogPojo.getdate();

            // Only difference between UPDATE and DELETE is
            //DELETE does not have ContentValues part
            sqliteDatabase.delete(AndroidOpenDbHelper.TABLE_NAME_LOG, AndroidOpenDbHelper.KEY_DATE+"=?", whereClauseArgument);

            sqliteDatabase.close();
            finish();           


















        }




        private void updateLog(fuelLogPojo updatefuelLogPojo) {
            // TODO Auto-generated method stub
            AndroidOpenDbHelper androidOpenDbHelper = new AndroidOpenDbHelper(this);
            SQLiteDatabase sqliteDatabase = androidOpenDbHelper.getWritableDatabase();


            // ContentValues class is used to store a set of values
            //It is like name-value pairs
            // "value" part contains the values that we are going to UPDATE
            ContentValues contentValues = new ContentValues();
            contentValues.put(AndroidOpenDbHelper.KEY_DATE, dateEditTextValue);
            contentValues.put(AndroidOpenDbHelper.KEY_PRICE, priceEditTextValue);
            contentValues.put(AndroidOpenDbHelper.KEY_FUEL, pumpEditTextValue);
            contentValues.put(AndroidOpenDbHelper.KEY_COST, costTextViewValue);
            contentValues.put(AndroidOpenDbHelper.KEY_ODM, odometerEditTextValue);
            contentValues.put(AndroidOpenDbHelper.KEY_CON, fconTextViewValue);

            // If we are using multiple whereClauseArguments, array size should have to change
            String[] whereClauseArgument = new String[1];
            whereClauseArgument[0] = updatefuelLogPojo.getdate();

            System.out.println("whereClauseArgument[0] is :" + whereClauseArgument[0]);

            /**
             * This is the normal SQL query for UPDATE
            UPDATE table_name
            SET column1=value, column2=value2,...
            WHERE some_column=some_value
            */      

            sqliteDatabase.update(AndroidOpenDbHelper.TABLE_NAME_LOG, contentValues, AndroidOpenDbHelper.KEY_DATE+"=?", whereClauseArgument);
            // For two whereClauseArguments
            //sqliteDatabase.update(AndroidOpenDbHelper.TABLE_NAME_GPA, contentValues, BaseColumns._ID+"=? AND name=?", whereClauseArgument);

            sqliteDatabase.close();
            finish();
        }       





        public static class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {

            public EditText editText;
            DatePicker dpResult;

        public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker

        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        //return new DatePickerDialog(getActivity(), (EditSessionActivity)getActivity(), year, month, day);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
        }

        public void onDateSet(DatePicker view, int year, int month, int day) {

            dateEdit.setText(String.valueOf(day) + "/"
                    + String.valueOf(month + 1) + "/" + String.valueOf(year));
            // set selected date into datepicker also
    }}}

1 个答案:

答案 0 :(得分:1)

密切关注pumpEditStr中的值。如果它为零,那么你的除法运算将等于无穷大。

Why is number divided by zero infinity in Java?