Search code examples
androidandroid-sqliteandroid-arrayadapterandroid-cursor

Check from ArrayList in DB if equal


I have a table that store two variables Days and percent’s. I want to assign them to a specific variable. From the Database Helper class, I’m getting the last 7 entries:

//----------------Graping the last seven elements ----------------------------------//
public ArrayList<StatsitcsHelper> GetWeaklyPrograss() {
    SQLiteDatabase db = this.getReadableDatabase ();

    Cursor cursor =  db.rawQuery ("select * from " + TABLE_PROGGRES, null);

    ArrayList<StatsitcsHelper> datas = new ArrayList<>();

    if (cursor != null) {
        cursor.moveToFirst ();
        for (int i = cursor.getCount () - 7 ; i < cursor.getCount(); i++) {
            cursor.moveToPosition(i);
            StatsitcsHelper data = new StatsitcsHelper();
            data.WeakleyDate= cursor.getString(cursor.getColumnIndex(COL_P_Date));
            data.WeakleyPercent = cursor.getInt (cursor.getColumnIndex(COL_P_Percentage));
            datas.add(data);
            cursor.moveToNext ();
        }
        cursor.close ();
    }
    return datas;
}

I want to build if statement that will say if day is Saturday then assign Saturday Percent Variable is Statistics Class to the percent associated from the database. Same goes for Sunday ….etc.

Inside the Statistics Class:

public void WeaklyStatstics(){

    int saturday = 0,
        sunday = 0,
        monday = 0,
        tuesday = 0,
        wednsday = 0,
        thersday = 0,
        friday = 0;

StatsitcsHelper statsitcsHelper = new StatsitcsHelper ();
DatabaseHelper databaseHelper = new DatabaseHelper (getActivity ());

//---------------------TO DO----------------------------------------//
}}

I don’t know how to analysis each item from the list in the database to another class.

Here is the Insertion of the Table:

    // ----------------Proggres Table ------------------------------------//
public boolean insertPrograss(String Date, Integer percentage) {
    SQLiteDatabase db = this.getWritableDatabase ();
    ContentValues contentValues = new ContentValues ();

    contentValues.put (COL_P_Date, Date);
    contentValues.put (COL_P_Percentage, percentage);


    long result = db.insert (TABLE_PROGGRES, null, contentValues);
    db.close ();
    return result != -1;
}

the method is called by scheduler that will store the date into just day by using date formate, and the output will be Monday, 87.

i want to write a method to get the last 7 inputs through GetWeaklyPrograss method. and assign it to the variables something like this

        if(statsitcsHelper.WeakleyDate.equals ("monday")){
        saturday = statsitcsHelper.WeakleyPercent;
    }

and here is the statsitcsHelper:

public class StatsitcsHelper {

//-------------- Weakly Progress -----------------------/
public String WeakleyDate;
public int WeakleyPercent;


}

Solution

  • can you try this logic ,

    public void WeaklyStatstics(){
    
            int saturday = 0,
                    sunday = 0,
                    monday = 0,
                    tuesday = 0,
                    wednsday = 0,
                    thersday = 0,
                    friday = 0;
    
            //StatsitcsHelper statsitcsHelper = new StatsitcsHelper ();
            DatabaseHelper databaseHelper = new DatabaseHelper (getActivity());
    
            ArrayList<StatsitcsHelper> statsitcsHelperList = databaseHelper.GetWeaklyPrograss();
    
            for (StatsitcsHelper statsitcsHelper : statsitcsHelperList)
            {
                if(statsitcsHelper.WeakleyDate.equals("Monday")){
                    monday = statsitcsHelper.WeakleyPercent;
                }else if (statsitcsHelper.WeakleyDate.equals("Tuesday")){
                    tuesday = statsitcsHelper.WeakleyPercent;
                }
                //todo and write for other days too     
            }
    
            // In here you can use all valid  data
    
    
        }