Android,如何庆祝我的 simplexml 类?
抱歉,我对 Android 还很陌生...... 我有一个活动创建我的类的对象实例:
file MyActivity.java:
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
public class MyActivity extends Activity {
Artworks myArtworks;
...
myArtworks = serial.read(Artworks.class, artworksXmlFile);
...
}
--------------------------------------------------------------
file Artworks.java:
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
@Element
public class Artworks {
@ElementList(entry = "artwork", inline = true)
private List<Artwork> list;
...
}
--------------------------------------------------------------
file Artwork.java:
public class Artwork {
// how to use here Toast.makeText(..., text, Toast.LENGTH_SHORT).show() ???
}
--------------------------------------------------------------
How do I use the Toast class (or any UI class) in my class Artwork,我从不直接实例化它,但它是通过调用serial.read(间接创建的)艺术品.类)? 我到底该怎么做呢?
提前致谢!
Sorry, I'm quite new in Android...
I have an activity which creates an object instance of my class:
file MyActivity.java:
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
public class MyActivity extends Activity {
Artworks myArtworks;
...
myArtworks = serial.read(Artworks.class, artworksXmlFile);
...
}
--------------------------------------------------------------
file Artworks.java:
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
@Element
public class Artworks {
@ElementList(entry = "artwork", inline = true)
private List<Artwork> list;
...
}
--------------------------------------------------------------
file Artwork.java:
public class Artwork {
// how to use here Toast.makeText(..., text, Toast.LENGTH_SHORT).show() ???
}
--------------------------------------------------------------
How do I use the Toast class (or any UI class) in my class Artwork, which I do never instantiate directly, but it's indirectly created by a call to serial.read(Artworks.class)?
How do I do it, exactly?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用“静态”帮助器类来维护对 Activity 上下文的静态引用。因此,在反序列化 Artwork 类之前,请调用
UtilClass.setContext(getApplicationContext());
。然后,在 Artwork 类中,您可以检索该上下文,然后执行诸如Toast.makeText(UtilClass.getContext(), "text", Toast.LENGTH_LONG).show();
之类的操作。You can use a "static" helper class to maintain a static reference to your Activity's context. So, before you deserialize your Artwork class, call
UtilClass.setContext(getApplicationContext());
. Then, within your Artwork class, you can retrieve that context and then do something likeToast.makeText(UtilClass.getContext(), "text", Toast.LENGTH_LONG).show();
.