gpt4 book ai didi

android - 在 fragment 中显示graphview仅在应用程序重启后显示数据

转载 作者:行者123 更新时间:2023-11-29 01:10:01 26 4
gpt4 key购买 nike

您好,我的代码遇到了障碍。我正在将一些数据保存到 sqlite,然后读取这些数据并将其显示在图表上。该图和保存一样工作正常。

我有 fragment 选项卡,可以轻松滑动。现在,当我保存数据时,我想在图表上检查它。然而,问题是当我向右滑动有图表的地方时,图表中没有数据。但是当我关闭应用程序并重新打开它时,数据就会出现。对此有什么好的解决方案?

图表代码:

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;
}


}

最佳答案

您正在使用 View 寻呼机来显示您的 fragment ,对吧?

View Pager 保留 View 的缓存,默认是当前 View 的缓存,左 View 和右 View 。

如果您从当前 View 滚动两个 View 并返回,您的 View 将更新,因为 fragment 将被重新创建。

您可以使用 setOffscreenPageLimit 禁用 View 寻呼机缓存(0);

或者你可以实现一个特殊的行为来知道 fragment 何时可​​见,你可以检查这个问题的例子:How to determine when Fragment becomes visible in ViewPager

我建议采用特殊行为,禁用屏幕外的查看寻呼机会对性能产生影响。

关于android - 在 fragment 中显示graphview仅在应用程序重启后显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43823162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com