2

I'm creating an editor in Android and for every type of ImageView i'm creating a .class

The types of the ImageViews are the following:

private Class<?> popups[] = {
Clock_pop_up.class, TV_kitchen_pop_up.class, Thermometer_pop_up.class, CoffeeMachine_pop_up.class, Fridge_pop_up.class, Oven_pop_up.class, Air_pop_up.class, TV_livingroom_pop_up.class
};

I have modified these classes with a <style> that for every Intent it's shown as a pop-up dialog

The problem is this:

Intent intent = new Intent(getApplicationContext(), (Class<?>) popups[finalI]);
startActivity(intent);

It shows the pop-up correctly, although if a .class repeats it self on the grid then i have the same "settings" for every two or more same classes

For Example

Let's say i add a Clock_pop_up.class and i set the "settings", Hour to 06 and Minutes to 05

enter image description here

Then i'll add a Thermometer_pop_up.class and set the "settings"

enter image description here

The Problem: If i add another Clock_pop_up.class or Thermometer_pop_up.class , i'll get the previous set "settings"

enter image description here

I've already tried this:

Is there any other way i can create instances of a .class every time i put the ImageView on the grid? Is there any other workaround?

Edit: I have to mention i'm using static fields in .class

Phill Alexakis
  • 1,449
  • 1
  • 12
  • 31

1 Answers1

0

I managed to do it by adding a HashMap Object into each PopUp.class mapping each time a new Object.

For example:

New Clock Object:

import java.io.Serializable;

public class Clock implements Serializable {
    private String hour;
    private String minute;
    private String condition;

    public Clock(String hour, String minute,String condition) {
        this.hour = hour;
        this.minute = minute;
        this.condition=condition;
    }

    public Clock() {
    }

    public String getHour() {
        return hour;
    }

    public void setHour(String hour) {
        this.hour = hour;
    }

    public String getMinute() {
        return minute;
    }

    public void setMinute(String minute) {
        this.minute = minute;
    }

    @Override
    public String toString() {
        return "Test{" +
                "hour='" + hour + '\'' +
                ", minute='" + minute + '\'' +
                '}';
    }

    public String getCondition() {
        return condition;
    }

    public void setCondition(String condition) {
        this.condition = condition;
    }
}

And at Clock_pop_up.class i create the HashMap Object like so:

 public static  HashMap<String, Clock> currentObject = new HashMap<>(); 

So for each PopUp.class i have a HashMap for every Object on the GridLayout and afterwards i can collect all my Objects via the HashMap

Phill Alexakis
  • 1,449
  • 1
  • 12
  • 31
  • Static map? It is unlikely that this will work correctly after process death. Refer to https://stackoverflow.com/questions/49046773/singleton-object-becomes-null-after-app-is-resumed/49107399#49107399 to see how you can crash your app on your second screens. – EpicPandaForce Jun 10 '20 at 15:16
  • I'm refreshing the static `HashMap` on the next `Intent`, haven't crashed yet – Phill Alexakis Jun 10 '20 at 15:26
  • so if i deal with Low Memory i have to maybe use `SharedPrefs` instead? – Phill Alexakis Jun 10 '20 at 15:36