0

I have check out answers like thees Variable is accessed within inner class. Needs to be declared final but it does not address what I am after.

I'm using GraphView to plot retrieved data from Google Firebase. The data is supposed to be a continuous line like this :

enter image description here

But when I run the program, it goes on like this instead :

enter image description here

Here's my lines of code :

public class RetrieveApp extends AppCompatActivity {

Integer hrValue;
ArrayList<Integer> array2; //array to contain hrValue
private ListView mListView;
LineGraphSeries<DataPoint> series ;



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

    GraphView graph = (GraphView)findViewById(R.id.graph);
    series = new LineGraphSeries<DataPoint>();

    DatabaseReference userdatabase = FirebaseDatabase.getInstance().getReference().child("Users");
    auth = FirebaseAuth.getInstance();
    user = auth.getCurrentUser();

    DatabaseReference ref = userdatabase.child(user.getUid());

    array2 = new ArrayList<>(); //array untuk hr



    //RETRIEVE DATA HR VALUE 
    ref.child("hrvalue").child("nilaihr").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            hrValue = dataSnapshot.child("tmpHR").getValue(Integer.class);
            showData(dataSnapshot);
            double y,x ;
            x = 0.0;
GraphView graph = (GraphView)findViewById(R.id.graph);
        series = new LineGraphSeries<DataPoint>();
            for (int i=0; i<1000; i++){
                x = x+i;
                y = hrValue;
                series.appendData(new DataPoint(x, y), true, 1000);
            }
            graph.addSeries(series);
        }

Everytime I try to move these two lines to onCreate,

GraphView graph = (GraphView)findViewById(R.id.graph);
        series = new LineGraphSeries<DataPoint>();

The graph.addSeries(series) line keeps on telling me :

Variable 'graph' is accessed from within inner class, needs to be declared final.

What should I do to address this problem? Any kind of help would be appreciated. Thank you!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Astrid Gloria
  • 11
  • 1
  • 5
  • You can keep the declaration statement in the global and initialization in the same position to fix this – Sushin Pv May 17 '18 at 11:36
  • I have defined GraphView graph; in public class, and graph=(GraphView)findViewById(R.id.graph); to onCreate. But now i don't know where to put series = new LineGraphSeries();. – Astrid Gloria May 17 '18 at 12:29

1 Answers1

1

Basically, you need to do what the error message implies that you should do. Declare graph as final. Like this:

protected void onCreate(Bundle savedInstanceState) {

    ...

    final GraphView graph = (GraphView)findViewById(R.id.graph);

    ...

    ref.child("hrvalue").child("nilaihr").addChildEventListener(
        new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                ...

                graph.addSeries(series);
            }
        });
     ...

The rule (prior to Java 8) is that an inner class can only use a local variable that is declared in an enclosing scope (like graph is ...) if the variable is declared as final.

In Java 8, they relaxed this rule. Now the variable only needs to be effectively final at the point at which it is captured. (Your code would be valid if compiled with a Java 8 compatible compiler.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216