如何从数据库中检索数据并将其显示在日历中?

时间:2018-10-17 12:30:40

标签: android android-sqlite android-calendar

我已经从数据库中检索了当前日期数据,但是在检索其他日期数据时遇到了问题。在单击其他日期应用程序后停止了。以下是我的代码

CalendarActivity.java

SELECT   emp_no
       , year
       , january
       , february
       , march
       , april
       , (january + february + march + april) AS total
FROM     employee_expense
ORDER BY emp_no, year

下面是logcat

public class CalendarActivity extends AppCompatActivity implements View.OnClickListener,CalendarCellDecorator {
    @BindView(R.id.lowerFrame)
FrameLayout frameLayout;

    @BindView(R.id.calendar)
    CalendarPickerView calendarPickerView;

    @BindView(R.id.textqua)
     EditText txtquantity;

    @BindView(R.id.textrate)
    EditText txtrate;

    ContentResolver resolver;
    @BindView(R.id.buttonupdate)
    Button btn;


    @BindView(R.id.textViewquestion)
            TextView txtquestion;

    User user;
    ArrayList<User> users;
    String strDate;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calendar);
        getSupportActionBar().hide();
        ButterKnife.bind(this);
        resolver=getContentResolver();
        btn.setOnClickListener(this);
        Intent rcv=getIntent();
        users=new ArrayList<>();
        user = (User) rcv.getSerializableExtra("keyuser");


       Calendar nextYear = Calendar.getInstance();
        nextYear.add(Calendar.YEAR,2);


       Calendar lastYear= Calendar.getInstance();
       lastYear.add(Calendar.YEAR, -2);
        Date today = new Date();
        calendarPickerView.init(lastYear.getTime(), nextYear.getTime()).withSelectedDate(today).inMode(CalendarPickerView.SelectionMode.RANGE);
        List<CalendarCellDecorator> decoratorList = new ArrayList<>();
        decoratorList.add(new CalendarActivity());
        calendarPickerView.setDecorators(decoratorList);
        calendarPickerView.setOnDateSelectedListener(new CalendarPickerView.OnDateSelectedListener() {
            @Override
            public void onDateSelected(Date date) {
               // strDate=formatDate(date);
               // Toast.makeText(CalendarActivity.this, formatDate(date), Toast.LENGTH_SHORT).show();
                queryUsersFromDB();
                strDate=formatDate(date);


                }

            @Override
            public void onDateUnselected(Date date) {

                }
        });

    }


    private String formatDate(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy");
        return simpleDateFormat.format(date);
    }



    @Override
    public void onClick(View v) {
        Intent intent = new Intent(CalendarActivity.this, AllUsersActivity.class);
        startActivity(intent);
        }



    private ArrayList<Date> Highlight(){
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
        String dateInString = user.date;
        Date date=null ;
        try {

            date = sdf.parse(dateInString);

        } catch (Exception e) {
            e.printStackTrace();
        }
        ArrayList<Date> holidays = new ArrayList<>();
        holidays.add(date);
        return holidays;
    }
    void queryUsersFromDB(){
        String[] projection={Util.COL_QUANTITY,Util.COL_RATE,Util.COL_DATE,Util.COL_TYPE};
        String where=Util.COL_DATE+"="+strDate;
        Cursor cursor=resolver.query(Util.USER_URI,projection,where,null,null);

        if(cursor!=null){

            while(cursor.moveToNext()){
                User user=new User();
                user.quantity=cursor.getDouble(cursor.getColumnIndex(Util.COL_QUANTITY));
                user.rate=cursor.getDouble(cursor.getColumnIndex(Util.COL_RATE));
                user.date=cursor.getString(cursor.getColumnIndex(Util.COL_DATE));
                user.type=cursor.getString(cursor.getColumnIndex(Util.COL_TYPE));

            }
            txtquantity.setText("Quantity: "+String.valueOf(user.quantity));
            txtrate.setText("Rate: "+String.valueOf(user.quantity * user.rate));
        }

    }
    @Override
    public void onBackPressed() {
        new AlertDialog.Builder(this)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle("Closing App")
                .setMessage("Are you sure you want to close this app?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }

                })
                .setNegativeButton("No", null)
                .show();
    }

    @Override
    public void decorate(CalendarCellView cellView, Date date) {

            if (date.getDate()<5) {
                cellView.setBackgroundResource(R.drawable.red_background);
            } else {
                cellView.setBackgroundResource(R.drawable.blue);
            }

    }

}

1 个答案:

答案 0 :(得分:0)

您的错误在这里:

    String where=Util.COL_DATE+"="+strDate;

这产生

    WHERE DATE=18.10.2018

这不是有效的SQL语法。

我没有使用SQLite的经验。如果可以选择使用参数化查询,则应该这样做,但恐怕无法为您提供详细信息。

如果否,则假设您的date列的类型为charvarchar,则将日期字符串放在单引号中应该可以,但是由于它为SQL注入攻击:

    String where = Util.COL_DATE + "='" + strDate + "'";