vendredi 8 janvier 2016

Compare current value with last value sqlite to show in listview

I have a table in sqlite where weight is recorded. Now I want to compare if the new value entered is < or == or > than the last value. My problem is that I cannot compare "only" with the ultimate value, the comparison makes me all the records in the table. This is my code

    private class MyCursorAdapter extends SimpleCursorAdapter {
    public MyCursorAdapter(Context context, int layout, Cursor c,
                           String[] from, int[] to, int flags) {
        super(context, layout, c, from, to, flags);}

    @Override
    public Object getItem(int position) {
        return super.getItem(super.getCount() - position - 1);}

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        super.bindView(view, context, cursor);

        String query = "SELECT nombre FROM miembros order by nombre DESC limit 1";
        SQLiteDatabase datos;
        datos = getApplicationContext().openOrCreateDatabase("DBMIEMBRO", Context.MODE_PRIVATE, null);
        Cursor c = datos.rawQuery(query, null);

        int previous_day=0;
        if (c != null && c.moveToFirst()) {
            previous_day= (int) c.getLong(0)-1;
        }

        int present_day=(int) cursor.getLong(cursor.getColumnIndex(DBhelper.MIEMBRO_NOMBRE));

        if (present_day > previous_day){
            view.setBackgroundColor(Color.parseColor("#F0A1A1"));}//red
        if (present_day < previous_day){
            view.setBackgroundColor(Color.parseColor("#AAF0A1"));}//green
        if (present_day == previous_day){
            view.setBackgroundColor(Color.parseColor("#18DBDB"));}//blue

Any ideas? Thanks

Aucun commentaire:

Enregistrer un commentaire