英文:
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
};
我已经使用一个<style>修改了这些类,对于每个Intent,它会显示为一个弹出对话框。
问题在于:
Intent intent = new Intent(getApplicationContext(), (Class<?>) popups[finalI]);
startActivity(intent);
它可以正确显示弹出窗口,但是如果一个.class在网格中重复出现,那么对于每两个或更多相同的类,我都会有相同的“设置”。
例如:
假设我添加了一个Clock_pop_up.class并且我设置了“设置”,小时设置为06,分钟设置为05
然后我会添加一个Thermometer_pop_up.class并设置“设置”
**问题:**如果我添加另一个
Clock_pop_up.class或Thermometer_pop_up.class,我会得到先前设置的“设置”
我已经尝试过这个:
- 使用
Class<?>创建新实例,但这对我没有起作用。
是否有其他方法可以在每次将ImageView放入网格时创建.class的instances?是否有其他解决方法?
编辑:我必须提到我在.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<?> 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
Then i'll add a Thermometer_pop_up.class and set the "settings"
> The Problem: If i add another Clock_pop_up.class or Thermometer_pop_up.class , i'll get the previous set "settings"
I've already tried this:
- Create new instance using
Class<?>, but it didn't work for me.
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 "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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。





评论