简体   繁体   中英

Android studio How to get int value (year, month, day) from CalendarView and use it to save in SQLite

my app is you can write your weight and connect that this date (day,month,year)

private void insertWeight(){
        String weightString = mWeightEditText.getText().toString().trim();
        int weight = Integer.parseInt(weightString);

        final int mYear;
        final int mMonth;
        final int mDay;

That is getting text from editText (it is weight) here it's wors

        CalendarView calendarView = (CalendarView) findViewById(R.id.calendarView);
        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

            @Override
            public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {

                mYear = year;    
                 mMonth = month + 1;
                 mDay = dayOfMonth;
}
        });

that I try realise add information about date to data (SQLite)

WeightDbHelper mDbHelper = new WeightDbHelper(this);
        SQLiteDatabase db = mDbHelper.getReadableDatabase();
        ContentValues values = new ContentValues();


        values.put(WeightContract.WeightEntry.COLUMN_WEIGHT,weightString);
        values.put(WeightContract.WeightEntry.COLUMN_DAY, mDay );
        values.put(WeightContract.WeightEntry.COLUMN_MONTH, mMonth );
        values.put(WeightContract.WeightEntry.COLUMN_YEAR, mYear );

here puting to the data base

problem is

@Override
            public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {

                mYear = year;    
                 mMonth = month + 1;
                 mDay = dayOfMonth;
}
        });

how to set mYear, mMonth, mDay Int number? how i can use this have already been recognised value? it's not only about calendar how i can take a value of something, not for TaxteView, just for using in database?

I'm not sure but I think you can set them before functions. As I saw, you set them in a function and used them in another one . set them out of function and set their values in function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM