-1

I have a record class that stored some data got from API calls. Here is my Record class.

public class Record {
private int id;
private int measure_id;
private double weight;
private double bmi;
private int diastolic;
private int systolic;
private int temperature;
private int heartRate;
private String date;
private int user_id;

public Record() {
    this.measure_id = 0;
    this.heartRate = 0;
    this.weight = 0;
    this.diastolic = 0;
    this.systolic = 0;
    this.temperature = 0;
    this.date = "0";
    this.bmi = 0;
}

public int getId() {
    return id;
}

public void setMeasure_id(int measure_id) {
    this.measure_id = measure_id;
}

public int getMeasure_id() {
    return measure_id;
}

public double getWeight() {
    return weight;
}

public void setWeight(double weight) {
    this.weight = weight;
}

public int getTemperature(){
    return temperature;
}

public void setTemperature(int temperature){
    this.temperature = temperature;
}

public int getHeartRate(){
    return heartRate;
}

public void setHeartRate(int heartRate){
    this.heartRate = heartRate;
}

public int getDiastolic(){
    return diastolic;
}

public void setDiastolic(int diastolic){
    this.diastolic = diastolic;
}

public int getSystolic(){
    return systolic;
}

public void setSystolic(int systolic){
    this.systolic = systolic;
}

public double getBmi() {
    return bmi;
}

public void setBmi(double bmi) {
    this.bmi = bmi;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}
}

Here is my API call class. I only show the important part. this class is used to get data from the Withings server. The is temperature, blood pressure and heart rate. I able to get json file and all the data.

private ArrayList convertData(String withings_records)
{
    ArrayList<Record> records = new ArrayList<Record>();
    List<MeasureGroup> withings_measures = new ArrayList<>();
    String record_test = "";

    try {

        JsonObject jsonObject = new JsonParser().parse(withings_records).getAsJsonObject();

        int status = jsonObject.get("status").getAsInt(); 

        if (status == 0) {
            JsonElement body = jsonObject.get("body");
            withings_measures = gson.fromJson(body.getAsJsonObject(),
                    MeasureResult.class).measureGroups; 
        }

        for(MeasureGroup measure : withings_measures) 
        {
            Record record = new Record();

            record.setDate(Integer.toString(measure.date));

            for(Measure item : measure.measures) 
            {
                record.setMeasure_id(measure.groupId);

                if(item.type == MeasureType.DIASTOLIC_BLOOD_PRESSURE)
                {
                    record.setDiastolic(item.getValue());
                }
                if(item.type == MeasureType.HEART_PULSE)
                {
                    record.setHeartRate(item.getValue());
                }

                if(item.type == MeasureType.BODY_TEMPERATURE)
                {
                    record.setTemperature(item.getValue());
                }

            }
            records.add(0,record);

        }
    } catch (Exception ex) {
    }

    return records;
}

Unfortunately, I cannot use the record inside another activity class. The value I got is zero. I am thinking that maybe I got the constructor value and have no idea on how to reuse the data in another activity. I try to use intent but it is not possible. In my main activity, I try to use record.getTemperature() function and others function, But the value I am getting is zero. Thanks in advance for those who are willing to help with my issues.

2 Answers2

0

It's very simple. If you are sure that the values are initialised in the first class, then create a static object of the first class. And with that object, you can access all variables of that class.

public static RecordClass recordObject;
recordObject = new RecirdClass();
Pang
  • 9,564
  • 146
  • 81
  • 122
Sivaram Boina
  • 125
  • 1
  • 8
0

For transferring any form of data between two activities you can transfer it by using putExtra to intent. Say you are calling Example activity from your current activity. You have a data ArrayList which you can attach to that intent. Your record should be Serializable. You can use putExtra like this:

Intent mIntent = new Intent(this, Example.class);
mIntent.putExtra(String key_name, ArrayList<Record> value);

Then in the Example activity called by you you can use that value as following:

ArrayList<Record> myList = (ArrayList<Record>) getIntent().getSerializableExtra(key_name);
LoveForDroid
  • 1,072
  • 12
  • 25