从SQLite数据库检索的数据在Android中不会显示,但不会显示错误(使用GraphView作为库)

时间:2018-08-05 15:45:44

标签: java android sqlite graph android-graphview

我是编程新手。我正在开发一个Android应用程序,其中必须使用SQLite数据库绘制图形。我用于Graph的表之一是Table_order,它具有item_id,item_name,table_number和order_date作为列。但是它并没有得到我正在使用的GraphView的图。下面的代码是GraphView的Java类。

Dim Rng As Range, xCell As Range
Dim xRows As Integer, xRows1 As Integer
Dim LastRowTE As Long

LastRowTE = Worksheets("Test_Execution").Cells(Worksheets("Test_Execution").Rows.Count, "A").End(xlUp).Row
Set WorkRng = Range("A2:A" & LastRowTE)
Set WorkRng1 = Range("B2:B" & LastRowTE)

Application.ScreenUpdating = False
Application.DisplayAlerts = False

xRows = WorkRng.Rows.Count
xRows1 = WorkRng1.Rows.Count

For Each Rng In WorkRng.Columns
    For i = 1 To xRows - 1
        For j = i + 1 To xRows
            If Rng.Cells(i, 1).Value <> Rng.Cells(j, 1).Value Then
                Exit For
            End If
        Next
        WorkRng.Parent.Range(Rng.Cells(i, 1), Rng.Cells(j - 1, 1)).Merge
        i = j - 1
    Next
Next

For Each Rng In WorkRng1.Columns
    For i = 1 To xRows1 - 1
        For j = i + 1 To xRows1
            If Rng.Cells(i, 1).Value <> Rng.Cells(j, 1).Value Then
                Exit For
            End If
        Next
        WorkRng1.Parent.Range(Rng.Cells(i, 1), Rng.Cells(j - 1, 1)).Merge
        i = j - 1
    Next
Next

Application.DisplayAlerts = True
Application.ScreenUpdating = True

Range("A1:O" & LastRowTE).WrapText = True

下面的代码是Xml文件:

public class MomoGraph extends AppCompatActivity {
  GraphView momoGraphView;
  LineGraphSeries < DataPoint > series;
  DatabaseHelper myDBHelper;
  SQLiteDatabase mDatabase;
  TableOrderDBAdapter mOrderDBHelper;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_momo_graph);

    momoGraphView = (GraphView) findViewById(R.id.graphViewMomo);

    myDBHelper = new DatabaseHelper(this);
    mOrderDBHelper = new TableOrderDBAdapter(this);

    series = new LineGraphSeries < > (getDataPoint());
    momoGraphView.addSeries(series);
    series.resetData(getDataPoint());

  }

  private DataPoint[] getDataPoint() {
    myDBHelper.getWritableDatabase();
    int n = mOrderDBHelper.getX_axis().size();
    DataPoint[] values = new DataPoint[n];

    for (int i = 0; i < n; i++) {
      DataPoint v = new DataPoint(Double.parseDouble(mOrderDBHelper.getX_axis().get(i)), Double.parseDouble(mOrderDBHelper.getY_axis().get(i)));
      values[i] = v;
    }
    return values;
  }

表适配器类的代码,我将数据库中的值存储到arraylist中。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MomoGraph">
    
    <com.jjoe64.graphview.GraphView
        android:id="@+id/graphViewMomo"
        android:layout_width="379dp"
        android:layout_height="520dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true" />
</RelativeLayout>
我在graphview类中调用了方法getX_axis和getY_axis。 我不明白为什么没有绘制值。 我已经在Google中浏览了很多帖子,但无法解决。请帮忙!

1 个答案:

答案 0 :(得分:0)

series = new LineGraphSeries < > (getDataPoint());
momoGraphView.addSeries(series);
series.resetData(getDataPoint()); //once remove this

private DataPoint[] getDataPoint() {
    myDBHelper.getWritableDatabase();
    ArrayList<String> Xval = mOrderDBHelper.getX_axis();
    ArrayList<String> Yval = mOrderDBHelper.getY_axis();

    if (Xval==null || Xval.size()==0)
      return new DataPoint[0];

    DataPoint[] values = new DataPoint[Xval.size()];

    for (int i = 0; i < Xval.size(); i++) {
      values[i] = new DataPoint(Double.parseDouble(Xval.get(i)), Double.parseDouble(Yval.get(i)));
    }
    return values;
}

尝试一次此代码。