0

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;


}
Ahmad Meer
  • 89
  • 1
  • 9

2 Answers2

0

I am not sure I understand what the problem is.

However to check the day of week, you can get some ideas from this:

check if date() is monday? java

On a side note: Your cursor contains all of the data in the table! It is usually better to get the results you want (last seven elements) on your cursor.

Limit your DB query, down to the interesting data. Instead of taking all data from the DB (select * from), you should specialize your query. Look for SQL expressions ORDER BY, ASC, DESC, LIMIT, and you will get there.

gpolic
  • 16
  • 3
  • the date field is storing a string of what day it is. how can i read each row of the table and check the string if equal and assign to the write place, sudko code: bring one row by GetWeaklyPrograss and see date field if equal Saturday then Saturday variable = that row percent – Ahmad Meer Jun 28 '17 at 14:16
  • you could just replicate the code that you already have for the seven days: if(statsitcsHelper.WeakleyDate.equals ("monday")){ saturday = statsitcsHelper.WeakleyPercent; } – gpolic Jun 28 '17 at 14:43
  • GetWeaklyPrograss is inside the DatabaseHelper where it iterate inside the list to bring the 7 rows. the work should be inside the Statistics class where it should read each row and check if its monday then it assign the percentage to an int variable there – Ahmad Meer Jun 28 '17 at 14:47
0

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


    }
Krish
  • 3,860
  • 1
  • 19
  • 32