无法在android中的数据库中提交数据

时间:2014-07-16 04:18:13

标签: android android-layout android-intent android-activity

我是Android应用程序的新手。我创建了一个关于医疗目的的应用程序。早期它工作正常。但是当我添加一些验证时,数据没有插入数据库中。无法找到任何错误。当我正在尝试调试代码,它正在之后逃避验证码!BloodGlucose.equals(“”)。无法找到解决方案 请帮忙......

package com.example.app;

import java.util.Calendar;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.os.Build;

public class AddBloodSugarInfo extends ActionBarActivity implements OnClickListener,
OnItemSelectedListener 
{

    private Button mSave;
    private Button mDelete;

    private EditText mBgreading;
    private Spinner mMeal;
    private Spinner mMealType;
    private EditText mListMed;
    private EditText mListFood;
    private EditText mNotes;
    private EditText tvDisplayDate;


     String []BloodGlucose=new String[100];
     String []Meal1=new String[100];
     String []MealType1=new String[100];
     String []ListMed=new String[100];
     String []ListFood=new String[100];
     String []Date=new String[100];
     String []Notes=new String[100];
    private Button btnChangeDate;

    private int year;
    private int month;
    private int day;

    static final int DATE_DIALOG_ID = 999;
    private String Meal;
    private String MealType;
    int int_glucose;
    protected static DBHelper1 DB1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_blood_sugar_info);


        DB1=DBHelper1.getDB();
        // Assignment of UI fields to the variables
        mSave = (Button) findViewById(R.id.save);
        mSave.setOnClickListener(this);

        mDelete = (Button) findViewById(R.id.delete);
        mDelete.setOnClickListener(this);


        mBgreading = (EditText) findViewById(R.id.eBgreading);
        mMeal = (Spinner) findViewById(R.id.spinner2);
        mMealType = (Spinner) findViewById(R.id.spinner1);
        mListMed = (EditText) findViewById(R.id.eListMed);
        mListFood = (EditText) findViewById(R.id.eListFood);
        mNotes = (EditText) findViewById(R.id.eNotes);
        tvDisplayDate = (EditText) findViewById(R.id.eDate);
        setCurrentDateOnView();
        addListenerOnButton();

        // Spinner method to read the on selected value
                ArrayAdapter<State> spinnerArrayAdapter = new ArrayAdapter<State>(this,
                        android.R.layout.simple_spinner_item, new State[] {
                                new State("Before a Meal"), new State("After a Meal") });
                mMeal.setAdapter(spinnerArrayAdapter);
                mMeal.setOnItemSelectedListener(this);

                // Spinner method to read the on selected value
                ArrayAdapter<State> spinnerArrayAdapter1 = new ArrayAdapter<State>(this,
                        android.R.layout.simple_spinner_item, new State[] {
                                new State("Breakfast"), new State("Lunch"),new State("Dinner") });
                mMealType.setAdapter(spinnerArrayAdapter1);
                mMealType.setOnItemSelectedListener(this);

            /*  mBgreading.setOnTouchListener(new OnTouchListener() {

                    @Override
                    public boolean onTouch(View arg0, MotionEvent arg1) {

                        return false;
                    }
                });*/
    }



    @Override
    public void onClick(View v) 
    {
        switch (v.getId()) {

        case R.id.delete:
            Intent i = new Intent(getBaseContext(), BloodSugar.class);
            startActivity(i);
            // finish();
            break;

        case R.id.save:
            String BloodGlucose = mBgreading.getText().toString().trim();
            Meal = mMeal.getSelectedItem().toString();
            MealType=mMealType.getSelectedItem().toString();
            String ListMedication = mListMed.getText().toString();
            String ListFood = mListFood.getText().toString();
            String Notes = mNotes.getText().toString();

            String Date = tvDisplayDate.getText().toString();


            boolean invalid = false;

            if (BloodGlucose.equals("")) {
                invalid = true;
                Toast.makeText(getApplicationContext(), "Enter your BloodGlucose",
                        Toast.LENGTH_SHORT).show();
            }
             else if (!BloodGlucose.equals("")) {
                  int_glucose = Integer.parseInt(BloodGlucose); 
                 if (!(int_glucose >= 80 && int_glucose <= 160)) { 
                 invalid = true; 
                 System.out.println("in address2");
                 Toast.makeText(getApplicationContext(), "Please valid reading", Toast.LENGTH_SHORT) .show(); 
                 } 
                        /*invalid = true;
                        Toast.makeText(getApplicationContext(),
                                "Please valid reading", Toast.LENGTH_SHORT)
                                .show();*/
                    }
            else if (ListMedication.equals("")) {
                invalid = true;
                System.out.println("in ListMedication");
                Toast.makeText(getApplicationContext(),
                        "Please enter ListMedication", Toast.LENGTH_SHORT)
                        .show();
            } else

            if (ListFood.equals("")) {
                invalid = true;
                System.out.println("in ListFood");
                Toast.makeText(getApplicationContext(),
                        "Please enter ListFood", Toast.LENGTH_SHORT)
                        .show();
            } else

            if (Notes.equals("")) {
                invalid = true;
                System.out.println("in Notes");
                Toast.makeText(getApplicationContext(),
                        "Please enter Notes", Toast.LENGTH_SHORT)
                        .show();

            } else if (Date.equals("")) {
                invalid = true;
                System.out.println("in Date");
                Toast.makeText(getApplicationContext(),
                        "Please enter Date", Toast.LENGTH_SHORT)
                        .show();
            } 
            else if (invalid == false) 
            {

                addEntry(BloodGlucose, Meal, MealType, ListMedication, ListFood, Notes,
                        Date);
                getBloodSugar();
                Intent i_register = new Intent(AddBloodSugarInfo.this,
                        BloodSugar.class);
                startActivity(i_register);




                // finish();
            }

            break;
        }
    }


    public void onDestroy() {
        super.onDestroy();
        //DB.close();
    }

    private void addEntry(String BloodGlucose,String Meal,String MealType,String ListMedication,String ListFood,String Notes,String Date) {

        SQLiteDatabase db1 = DB1.getWritableDatabase();
        ContentValues values = new ContentValues();
        //values.put("id", 2);
        values.put("patientid", LoginActivity1.getPatientId());
        values.put("BloodGlucose", BloodGlucose);
        values.put("Meal", Meal);
        values.put("MealType", MealType);
        values.put("ListMedication", ListMedication);
        values.put("ListFood", ListFood);
        values.put("Notes", Notes);
        values.put("Date", Date);
        try {
            long rowId = db1.insert(DBHelper1.DATABASE_TABLE_NAME6, null, values);
            System.out.println("rowId: "+rowId);
            Toast.makeText(getApplicationContext(),
                    "your details submitted Successfully...",
                    Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
@Override
    public void onItemSelected(AdapterView<?> parent, View view, int position,
            long id) {

    }

@Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }


//display current date
    public void setCurrentDateOnView() {




        final Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);

        // set current date into textview
        tvDisplayDate.setText(new StringBuilder()
            // Month is 0 based, just add 1
            .append(month + 1).append("-").append(day).append("-")
            .append(year).append(" "));

        // set current date into datepicker
        //dpResult.init(year, month, day, null);

    }

    public void addListenerOnButton() {

        btnChangeDate = (Button) findViewById(R.id.btnChangeDate);

        btnChangeDate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                showDialog(DATE_DIALOG_ID);

            }

        });

    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
           // set date picker as current date
           return new DatePickerDialog(this, datePickerListener, 
                    year, month,day);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener datePickerListener 
           = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {
            year = selectedYear;
            month = selectedMonth;
            day = selectedDay;

            // set selected date into textview
            tvDisplayDate.setText(new StringBuilder().append(month + 1)
               .append("-").append(day).append("-").append(year)
               .append(" "));

            // set selected date into datepicker also
            //dpResult.init(year, month, day, null);

        }

        /*@Override
        public void onDateSet(android.widget.DatePicker arg0, int arg1,
                int arg2, int arg3) {
            // TODO Auto-generated method stub

        }*/
    };


    public void getBloodSugar()
    {


        DB1=DBHelper1.getDB();
        String id=String.valueOf(LoginActivity1.getPatientId());
    System.out.println("Patient id "+id);
        System.out.println("in getBloodSugar---1");
      SQLiteDatabase db1 = DB1.getReadableDatabase();
      System.out.println("in getBloodSugar--3");

        String[] columns = { "BloodGlucose,Meal,MealType,ListMedication,ListFood,Notes,Date" };

        String selection = "patientid=?";
        String[] selectionArgs = { id };

        Cursor cursor = null;
        try {

            cursor = db1.query(DBHelper1.DATABASE_TABLE_NAME6, columns, selection,
                    selectionArgs, null, null, null);
            cursor.moveToFirst();

           String bloodGlucose=null;
           String meal1=null;
           String mealType1=null;
           String listMed=null;
           String listFood=null;
           String notes=null;
           String date=null;
           System.out.println(" Cursor  count "+cursor.getCount());
           for(int i=0;i<cursor.getCount();i++)
           {

                 bloodGlucose=cursor.getString((cursor.getColumnIndex("BloodGlucose")));
               meal1 =cursor.getString((cursor.getColumnIndex("Meal")));
               mealType1 =cursor.getString((cursor.getColumnIndex("MealType")));
               listMed =cursor.getString((cursor.getColumnIndex("ListMedication")));
               listFood =cursor.getString((cursor.getColumnIndex("ListFood")));
               notes =cursor.getString((cursor.getColumnIndex("Notes")));
               date =cursor.getString((cursor.getColumnIndex("Date")));

               BloodGlucose[i]=bloodGlucose;
               Meal1[i]=meal1;
               MealType1[i]=mealType1;
               ListMed[i]=listMed;
               ListFood[i]=listFood;
               Notes[i]=notes;
               Date[i]=date;

             /*  System.out.println("date"+  Date[i]+ " "+i);
               System.out.println("date"+ TestName[i]+ " "+i);
               System.out.println("date"+ Result[i]+ " "+i);
               System.out.println("date"+ Notes[i]+ " "+i);*/
              cursor.moveToNext();

           }

           BloodSugar BloodSugar=new BloodSugar();
           BloodSugar.BloodGlucose=BloodGlucose;
           BloodSugar.Meal1=Meal1;
           BloodSugar.MealType1=MealType1;
           BloodSugar.ListMed=ListMed;
           BloodSugar.ListFood=ListFood;
           BloodSugar.Notes=Notes;
           BloodSugar.Date=Date;
           BloodSugar.fillCountryTable();
            System.out.println("in getLabAndTestResult---4");
            //startManagingCursor(cursor);
        } catch (Exception e)

        {
           System.out.println("Excetion in getLabAndTestResult"+e.getMessage());
        }

        int numberOfRows = cursor.getCount();
        System.out.println("numberOfRows::"+numberOfRows);


        }





    }

但是当我尝试使用以下验证时,数据没有插入到表中。我没有在logcat中收到任何错误。

else if (!BloodGlucose.equals("")) {
                  int_glucose = Integer.parseInt(BloodGlucose); 
                 if (!(int_glucose >= 80 && int_glucose <= 160)) { 
                 invalid = true; 
                 System.out.println("in address2");
                 Toast.makeText(getApplicationContext(), "Please valid reading", Toast.LENGTH_SHORT) .show(); 
                 } 
                        /*invalid = true;
                        Toast.makeText(getApplicationContext(),
                                "Please valid reading", Toast.LENGTH_SHORT)
                                .show();*/
                    }

1 个答案:

答案 0 :(得分:0)

有趣!

您创建了 if-else链。您必须在条件之前制定单独的条件,如同(无效== false),即删除其他。它会起作用。