1

In one class I set the ID to my Warranty class. I added my full code so you can see where it goes wrong. I declare the class so I can call it. Set the ID on the setter and finally I retrieve it on the other class with the getter.

public class HomeActivity extends AppCompatActivity {`

    DatabaseHelper db = new DatabaseHelper(this);
    LinearLayout buttonViewWarrantys;
    Warranty wr = new Warranty();

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

        Display display = getWindowManager().getDefaultDisplay();
        int width_display = display.getWidth();
        width_display = (width_display/10) * 8;
        Button addWarBtn = findViewById(R.id.add_warranty);
        buttonViewWarrantys = findViewById(R.id.show_warratys);
        addWarBtn.getLayoutParams().width = width_display;
        addWarBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent inputScreen = new Intent(HomeActivity.this, InputScreen.class);
                startActivity(inputScreen);
            }
        });

        updateWarrantys();
    }

    @Override
    public void onResume() {
        super.onResume();
        updateWarrantys();
    }

    void updateWarrantys(){
        List<Warranty> warrantys = db.getAllContacts();
        for (Warranty cn : warrantys) {
            final String ID = Integer.toString(cn.getID());
            String buttonName = ID + "_" + cn.getName();
            String buttonText = "Name: " + cn.getName() + '\n' + " Bought on: " + cn.getStartDate() + '\n' + " Warranty stops on: " + cn.getEndDate();

            Button buttons = new Button(this);
            buttons.setTag(buttonName);
            buttons.setText(buttonText);
            buttons.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            final String warranty_name = cn.getName();
            final String start_date = cn.getStartDate();
            final String end_date = cn.getEndDate();
            final String img_path = cn.getImgFilePath();

            buttons.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    System.out.println(ID);
                    wr.setID(Integer.parseInt(ID));
                    //new Warranty(Integer.parseInt(ID), warranty_name, start_date, end_date, img_path);
                    Intent showWarranty = new Intent(HomeActivity.this, ShowWarranty.class);
                    startActivity(showWarranty);

                }
            });
            buttonViewWarrantys.addView(buttons);
            String log = "Id: " + cn.getID() + " ,Name: " + cn.getName() + " ,Startdate: " + cn.getStartDate() + " ,EndDate: " + cn.getEndDate() + " ,Path: " + cn.getImgFilePath();
            System.out.println(log);
        }
    }
}

This is my Warranty class with my setter and getter.

public int getID() {
    return ID;
}

public void setID(int ID) {
    this.ID = ID;
}

After this I try to retrieve it but the result of the ID is 0 and when I set it it is 8.

public class ShowWarranty extends AppCompatActivity {`
    Warranty wr = new Warranty();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_warranty);
        int test = wr.getID();
        System.out.println("The number is: " + test);
    }
}

Why is this not possible?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Jochem
  • 75
  • 3
  • 10

2 Answers2

1

You create a new instance in both the classes, meaning when you grab it in the second class you haven't set the value on the instance in that class. From what you're describing, the variable is not static, meaning the value depends on the instance. The default value for integer primitives is 0, which is the reason why it has that specific value.

Pseudocode example of what you're doing:

Warranty x = new Warranty();
x.setID(8);
Warranty y = new Warranty();
System.out.println(y.getID());//Since the ID of y isn't set, it'll print 0

As for a possible solution:

If your class is fairly basic, just make it implement Serializable and put it as an extra in the intent. And by "fairly basic" I mean a class that you actually can serialize. Classes like Context cannot be serialized, so you'd need to add a method for adding it back after it's been deserialized and mark unserializable fields as transient. Or just pass the primitives directly.

You can use parcelable instead of Serializable, but that's a matter of preference.

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • Serializable good word for this. Same idea as flattening. Serilizable is a marker interface – Pomagranite Jan 09 '18 at 19:25
  • And serializable doesn't require manually writing code for doing it. It is possible to override the methods, but serialization pretty much just requires you to implement the interface (and override `serialVersionUID` if necessary). Parcelable is a "manual" approach to it, but it still works. There's still the matter of version incompatibilities and unserializable fields, but that's an issue with parcelable as well – Zoe Jan 09 '18 at 19:28
0

You add the data into the intent. See how to pass primitives. Or if you want to pass an object, you must first flatten it, and then pass the parcelable. Each intent has its own address space

Zoe
  • 27,060
  • 21
  • 118
  • 148
Pomagranite
  • 696
  • 5
  • 11