Search code examples
androidclassmethodsreturnreturn-type

How to read a return from a class - android


I have doing a file from the server, and my idea is to pass the filesizedownloaded to a class and on my progressbar class get value to update the progress bar; I have a function that passes a value to a class

this is how i try to pass the value

new updateValue(fileSizeDownloaded);

value gets stored here public class updateValue{ private long v_value;

        public updateValue(long value) {
            v_value = value;
        }

        public long getV_value() { return v_value; };
    }

and from a different class I'm trying to get the value of getV_value but I keep getting an error; update(long) in update cannot be applied to 0

How can i get the value stored :/ all this is on mainactivity.java

updateValue value = new updateValue();
value.getV_value();

Solution

  • What you can do is create a new class and extend Application

    public class yourcalss extends Application {
          public long getValue() {
                return value;
            }
    
            public void setValue(long value_e) {
                value = value_e;
            }
    }
    

    This is how you save the value

    final yourcalss value = (yourcalss ) getApplicationContext();
    value.setValue(fileSizeDownloaded);
    

    and this is how you can access the value

    final yourcalss val = (yourcalss ) getApplicationContext();
    final Long v = val.getValue(); 
    

    and manifiest

    <application android:name="yourcalss