在文本更改时从sqlite数据库获取特定行

时间:2016-03-16 08:16:31

标签: java android database sqlite android-studio

我的sqlite数据库中有多条记录,并且文本发生了变化(更改了日期编辑文本'中的日期),我希望相应的行显示在表格中。我有以下代码适用于数据库中的最后一条记录,但我不知道如何显示与所选日期相对应的行。请帮忙,因为我对android很新。 这是我的代码:

    public class measurement extends AppCompatActivity{
    Button button;
    EditText Date;
    int index=0;
    TextView HEIGHT,WEIGHT,BMI,BLOODPRESSURE,PULSE,TEMPERATURE,GLUCOSELEVEL,OXYGENLEVEL;
DatabaseAdapterMeasurements MeasurementsDataBaseAdapter;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.measurement);


    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(measurement.this);
            final DatePicker picker = new DatePicker(measurement.this);
            picker.setCalendarViewShown(false);

            builder.setTitle("Create Year");
            builder.setView(picker);
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();

                }
            });
            builder.setPositiveButton("Select", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    int day = picker.getDayOfMonth();
                    int month = (picker.getMonth() + 1);
                    int year = picker.getYear();
                    Date.setText(day + "/" + month + "/" + year);


                    // Toast.makeText(measurement.this, picker.getDayOfMonth() + " / " + (picker.getMonth() + 1) + " / " + picker.getYear(), Toast.LENGTH_LONG).show();


                }
            });
            builder.show();


        }

    });


    MeasurementsDataBaseAdapter = new DatabaseAdapterMeasurements(this);
    MeasurementsDataBaseAdapter = MeasurementsDataBaseAdapter.open();
    Measurements studentCourse = new Measurements();
    studentCourse.setDATE("ppp");
    studentCourse.setEMAIL("hh@HOTMAIL.COM");
    studentCourse.setREFERENCE("lkj");
    studentCourse.setHEIGHT("1121");
    studentCourse.setWEIGHT("CIS11");
    studentCourse.setBMI("A-");
    studentCourse.setBLOODPRESSURE("A-");
    studentCourse.setPULSE("A-");
    studentCourse.setTEMPERATURE("A-");
    studentCourse.setGLUCOSELEVEL("A-");
    studentCourse.setOXYGENLEVEL("A-");
    MeasurementsDataBaseAdapter.insertEntry(studentCourse);
    studentCourse.setDATE("lll");
    studentCourse.setEMAIL("v=bb@HOTMAIL.COM");
    studentCourse.setREFERENCE("uhu");
    studentCourse.setHEIGHT("111");
    studentCourse.setWEIGHT("CIS11");
    studentCourse.setBMI("A-");
    studentCourse.setBLOODPRESSURE("A-");
    studentCourse.setPULSE("A-");
    studentCourse.setTEMPERATURE("A-");
    studentCourse.setGLUCOSELEVEL("A-");
    studentCourse.setOXYGENLEVEL("A-");
    MeasurementsDataBaseAdapter.insertEntry(studentCourse);
    studentCourse.setDATE("jjj");
    studentCourse.setEMAIL("cc@HOTMAIL.COM");
    studentCourse.setREFERENCE("lol");
    studentCourse.setHEIGHT("1671");
    studentCourse.setWEIGHT("CIS11");
    studentCourse.setBMI("A-");
    studentCourse.setBLOODPRESSURE("A-");
    studentCourse.setPULSE("A-");
    studentCourse.setTEMPERATURE("A-");
    studentCourse.setGLUCOSELEVEL("A-");
    studentCourse.setOXYGENLEVEL("A-");
    MeasurementsDataBaseAdapter.insertEntry(studentCourse);
    studentCourse.setDATE("jjj");
    studentCourse.setEMAIL("hhfg@HOTMAIL.COM");
    studentCourse.setREFERENCE("plo");
    studentCourse.setHEIGHT("1091");
    studentCourse.setWEIGHT("CIS11");
    studentCourse.setBMI("A-");
    studentCourse.setBLOODPRESSURE("A-");
    studentCourse.setPULSE("A-");
    studentCourse.setTEMPERATURE("A-");
    studentCourse.setGLUCOSELEVEL("A-");
    studentCourse.setOXYGENLEVEL("A-");
    MeasurementsDataBaseAdapter.insertEntry(studentCourse);














            List<Measurements> measurements= MeasurementsDataBaseAdapter.getAllMeasurements();

            HEIGHT = (TextView) findViewById(R.id.HEIGHT);
            WEIGHT = (TextView) findViewById(R.id.WEIGHT);
            BMI = (TextView) findViewById(R.id.BMI);
            BLOODPRESSURE = (TextView) findViewById(R.id.BP);
            PULSE = (TextView) findViewById(R.id.PULSE);

            TEMPERATURE=(TextView) findViewById(R.id.TEMP );
            GLUCOSELEVEL = (TextView) findViewById(R.id.GL);
            OXYGENLEVEL = (TextView) findViewById(R.id.OL);
             Date = (EditText) findViewById(R.id.Date);

            for(Measurements mn: measurements)
            {
                String log="EMAIL"+mn.getEMAIL()+"REFERENCE"+mn.getREFERENCE()+"HEIGHT"+mn.getHEIGHT()+"WEIGHT"+mn.getWEIGHT()+"BMI"+mn.getBMI()+"BLOODPRESSURE"+mn.getBLOODPRESSURE()+"PULSE"+mn.getPULSE()+"TEMPERATURE"+mn.getTEMPERATURE()+"GLUCOSELEVEL"+mn.getGLUCOSELEVEL()+"OXYGENLEVEL"+mn.getOXYGENLEVEL();




                HEIGHT.setText(mn.getHEIGHT());
                WEIGHT.setText(mn.getWEIGHT());
                BMI.setText(mn.getBMI());
                BLOODPRESSURE.setText(mn.getBLOODPRESSURE());
                PULSE.setText(mn.getPULSE());
                TEMPERATURE.setText(mn.getTEMPERATURE());
                GLUCOSELEVEL.setText(mn.getGLUCOSELEVEL());
               OXYGENLEVEL.setText(mn.getOXYGENLEVEL());



            }







        }

}

1 个答案:

答案 0 :(得分:0)

尝试这样的事情......

  public Cursor  getData(String qno) {



      Cursor cursor = null;



          SQLiteDatabase db = this.getReadableDatabase();
          cursor = db.rawQuery("SELECT * FROM QuizTable WHERE qno=?", new String[] { qno + ""});

          if(cursor.getCount() > 0) {

              cursor.moveToFirst();
          }

         return cursor;


 }// method body finish