简体   繁体   English

Android studio 如何从 CalendarView 获取 int 值(年、月、日)并用它保存在 SQLite 中

[英]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这是从 editText 获取文本(它是重量),这很糟糕

        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)我尝试实现将有关日期的信息添加到数据(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?如何设置 mYear、mMonth、mDay Int 数? 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?这不仅与日历有关,我如何才能为某物取值,而不是用于 TaxteView,仅用于在数据库中使用?

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.将它们设置为函数之外并在函数中设置它们的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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