3

Hello i have come to an obstacle in my code. I am saving some data to sqlite, then reading this data and displaying it on the graph. The graph works fine as does the saving.

I have fragment tabs for easy swiping. Now when i save data i want to check it on the graph. However the problem is when i swipe to the right where there is graph there is no data in graph. But when i close the app and reopen it then the data appears. What would be a good solution for this?

The code for graph:

public class overwiew extends Fragment {

        private LineGraphSeries<DataPoint> series1;
        private LineGraphSeries<DataPoint> series2;
        private LineGraphSeries<DataPoint> series3;
        private LineGraphSeries<DataPoint> series4;
        private LineGraphSeries<DataPoint> series5;
        private LineGraphSeries<DataPoint> series6;
        private LineGraphSeries<DataPoint> series7;
        Sqlite_DBHelper sqliteDbHelper_;
        SQLiteDatabase sqLiteDatabase;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.owerview, container, false);


            sqliteDbHelper_ = new Sqlite_DBHelper(getActivity());
            sqLiteDatabase = sqliteDbHelper_.getWritableDatabase();

            GraphView graph = (GraphView) rootView.findViewById(R.id.graph);
            series1 = new LineGraphSeries<DataPoint>(getDataToday());
            series2 = new LineGraphSeries<DataPoint>(getDataToday_minus1());
            series3 = new LineGraphSeries<DataPoint>(getDataToday_minus2());
            series4 = new LineGraphSeries<DataPoint>(getDataToday_minus3());
            series5 = new LineGraphSeries<DataPoint>(getDataToday_minus4());
            series6 = new LineGraphSeries<DataPoint>(getDataToday_minus5());
            series7 = new LineGraphSeries<DataPoint>(getDataToday_minus6());


            series1.setColor(Color.parseColor("#D50000"));
            series2.setColor(Color.parseColor("#D500F9"));
            series3.setColor(Color.parseColor("#4527A0"));
            series4.setColor(Color.parseColor("#1565C0"));
            series5.setColor(Color.parseColor("#00838F"));
            series6.setColor(Color.parseColor("#1B5E20"));
            series7.setColor(Color.parseColor("#FF9800"));

            graph.addSeries(series1);
            graph.addSeries(series2);
            graph.addSeries(series3);
            graph.addSeries(series4);
            graph.addSeries(series5);
            graph.addSeries(series6);
            graph.addSeries(series7);

            graph.getViewport().setMinX(6);
            graph.getViewport().setMaxX(24);
            graph.getViewport().setMinY(0);
            graph.getViewport().setMaxY(10);
            graph.getViewport().setYAxisBoundsManual(true);
            graph.getViewport().setXAxisBoundsManual(true);

            graph.setTitle("Tedenski prikaz za zadnje 7 dni");

            series1.setTitle("danes");
            series2.setTitle("včeraj");
            series3.setTitle("x");
            series4.setTitle("x");
            series5.setTitle("x");
            series6.setTitle("x");
            series7.setTitle("x");

            graph.getLegendRenderer().setVisible(true);
            graph.getLegendRenderer().setAlign(LegendRenderer.LegendAlign.TOP);

        return rootView;

        }

        private DataPoint[] getDataToday() {

            String[] columns = {"hour_only","JAKOST"};

            GregorianCalendar gc = new GregorianCalendar();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String today = sdf.format(gc.getTime());

            //  Cursor cursor = sqLiteDatabase.query("podatki", columns,null,null,null,null,null);
            Cursor cursor = sqLiteDatabase.query("podatki", columns,"date(timeStamp) = ?", new String[] {today},null,null,null);



            DataPoint[] dp = new DataPoint[cursor.getCount()];

            for (int i=0; i < cursor.getCount();i++){
                cursor.moveToNext();
                dp[i]=new DataPoint(cursor.getInt(0),cursor.getInt(1));
            }
            return dp;
        }


        private DataPoint[] getDataToday_minus1() {

            String[] columns = {"hour_only","JAKOST"};

            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DATE, -1);
            String today_minus1 = dateFormat.format(cal.getTime());

            Cursor cursor = sqLiteDatabase.query("podatki", columns,"date(timeStamp) = ?", new String[] {today_minus1},null,null,null);



            DataPoint[] dp = new DataPoint[cursor.getCount()];

            for (int i=0; i < cursor.getCount();i++){
                cursor.moveToNext();
                dp[i]=new DataPoint(cursor.getInt(0),cursor.getInt(1));
            }
            return dp;
        }
        private DataPoint[] getDataToday_minus2() {

            String[] columns = {"hour_only","JAKOST"};

            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DATE, -2);
            String today_minus2 = dateFormat.format(cal.getTime());

            Cursor cursor = sqLiteDatabase.query("podatki", columns,"date(timeStamp) = ?", new String[] {today_minus2},null,null,null);



            DataPoint[] dp = new DataPoint[cursor.getCount()];

            for (int i=0; i < cursor.getCount();i++){
                cursor.moveToNext();
                dp[i]=new DataPoint(cursor.getInt(0),cursor.getInt(1));
            }
            return dp;
        }

        private DataPoint[] getDataToday_minus3() {

            String[] columns = {"hour_only","JAKOST"};

            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DATE, -3);
            String today_minus3 = dateFormat.format(cal.getTime());

            Cursor cursor = sqLiteDatabase.query("podatki", columns,"date(timeStamp) = ?", new String[] {today_minus3},null,null,null);



            DataPoint[] dp = new DataPoint[cursor.getCount()];

            for (int i=0; i < cursor.getCount();i++){
                cursor.moveToNext();
                dp[i]=new DataPoint(cursor.getInt(0),cursor.getInt(1));
            }
            return dp;
        }

        private DataPoint[] getDataToday_minus4() {

            String[] columns = {"hour_only","JAKOST"};

            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DATE, -4);
            String today_minus4 = dateFormat.format(cal.getTime());

            Cursor cursor = sqLiteDatabase.query("podatki", columns,"date(timeStamp) = ?", new String[] {today_minus4},null,null,null);



            DataPoint[] dp = new DataPoint[cursor.getCount()];

            for (int i=0; i < cursor.getCount();i++){
                cursor.moveToNext();
                dp[i]=new DataPoint(cursor.getInt(0),cursor.getInt(1));
            }
            return dp;
        }

        private DataPoint[] getDataToday_minus5() {

            String[] columns = {"hour_only","JAKOST"};

            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DATE, -5);
            String today_minus5 = dateFormat.format(cal.getTime());

            Cursor cursor = sqLiteDatabase.query("podatki", columns,"date(timeStamp) = ?", new String[] {today_minus5},null,null,null);



            DataPoint[] dp = new DataPoint[cursor.getCount()];

            for (int i=0; i < cursor.getCount();i++){
                cursor.moveToNext();
                dp[i]=new DataPoint(cursor.getInt(0),cursor.getInt(1));
            }
            return dp;
        }

        private DataPoint[] getDataToday_minus6() {

            String[] columns = {"hour_only","JAKOST"};

            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DATE, -6);
            String today_minus6 = dateFormat.format(cal.getTime());

            Cursor cursor = sqLiteDatabase.query("podatki", columns,"date(timeStamp) = ?", new String[] {today_minus6},null,null,null);



            DataPoint[] dp = new DataPoint[cursor.getCount()];

            for (int i=0; i < cursor.getCount();i++){
                cursor.moveToNext();
                dp[i]=new DataPoint(cursor.getInt(0),cursor.getInt(1));
            }
            return dp;
        }


    }

1 Answers1

1

You are using a view pager to show your fragments, right?

View Pager keeps cache of views, the default is a cache from the current view, the left one and the right one.

If you scroll two views from current one and get back, your view will be update because the fragment will be recreated.

You can disable the view pager cache with setOffscreenPageLimit(0);

Or you can implement a special behavior to know when the fragment is visible, you can check this question for an example: How to determine when Fragment becomes visible in ViewPager

I suggest going with the special behavior, disabling the view pager offscreen will have performance impacts.

Community
  • 1
  • 1
jonathanrz
  • 4,206
  • 6
  • 35
  • 58
  • Yes i use ViewPager but when i swipe as you mentioned the graph does not update. I then close the app completly, reopen and the data is there. –  May 06 '17 at 17:24
  • You are setting the offscreen page limit by hand? How many fragments do you have? – jonathanrz May 06 '17 at 17:26
  • i have 3 fragment (settings, mainpage and graph overview) –  May 06 '17 at 17:29
  • You will never be able to empty view pager cache using your app. ViewPager will keep all the 3 fragments into cache. See my suggestions for solutioning your problem. – jonathanrz May 06 '17 at 19:39