如何在Android Studio中为每个意图启动一个类的新实例。

huangapple 未分类评论50阅读模式
英文:

How to start a new instance of a class for every intent in android studio

问题

我正在在Android中创建一个编辑器,对于每种类型的ImageView,我正在创建一个.class

ImageViews的类型如下:

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
};

我已经使用一个&lt;style&gt;修改了这些类,对于每个Intent,它会显示为一个弹出对话框。

问题在于:

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

它可以正确显示弹出窗口,但是如果一个.class在网格中重复出现,那么对于每两个或更多相同的类,我都会有相同的“设置”。

例如:

假设我添加了一个Clock_pop_up.class并且我设置了“设置”,小时设置为06,分钟设置为05

如何在Android Studio中为每个意图启动一个类的新实例。

然后我会添加一个Thermometer_pop_up.class并设置“设置”

如何在Android Studio中为每个意图启动一个类的新实例。

**问题:**如果我添加另一个Clock_pop_up.classThermometer_pop_up.class,我会得到先前设置的“设置”

如何在Android Studio中为每个意图启动一个类的新实例。

我已经尝试过这个:

是否有其他方法可以在每次将ImageView放入网格时创建.classinstances?是否有其他解决方法?

编辑:我必须提到我在.class中使用了static字段。

英文:

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&lt;?&gt; 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 &lt;style&gt; that for every Intent it's shown as a pop-up dialog

The problem is this:

Intent intent = new Intent(getApplicationContext(), (Class&lt;?&gt;) 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

如何在Android Studio中为每个意图启动一个类的新实例。

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

如何在Android Studio中为每个意图启动一个类的新实例。

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

如何在Android Studio中为每个意图启动一个类的新实例。

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

答案1

得分: 0

我通过在每个PopUp.class中添加一个HashMap对象来完成这个操作,每次映射一个新的对象。

例如:

新的时钟对象:

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;
    }
}

然后在Clock_pop_up.class中创建HashMap对象,如下所示:

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

因此,对于每个PopUp.class,我都有一个HashMap,用于每个GridLayout上的对象,然后可以通过HashMap收集所有我的对象。

英文:

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 &quot;Test{&quot; +
                &quot;hour=&#39;&quot; + hour + &#39;\&#39;&#39; +
                &quot;, minute=&#39;&quot; + minute + &#39;\&#39;&#39; +
                &#39;}&#39;;
    }

    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&lt;String, Clock&gt; currentObject = new HashMap&lt;&gt;(); 

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

huangapple
  • 本文由 发表于 2020年5月5日 17:16:42
  • 转载请务必保留本文链接:https://java.coder-hub.com/61609709.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定