Android,如何庆祝我的 simplexml 类?

发布于 2024-11-07 10:44:39 字数 1035 浏览 1 评论 0原文

抱歉,我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

混吃等死 2024-11-14 10:44:39

您可以使用“静态”帮​​助器类来维护对 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 like Toast.makeText(UtilClass.getContext(), "text", Toast.LENGTH_LONG).show();.

小鸟爱天空丶 2024-11-14 10:44:39
import android.widget.Toast;

public class Artwork {
   Toast.makeText(Artwork.this, "text", Toast.LENGTH_LONG).show();
}
import android.widget.Toast;

public class Artwork {
   Toast.makeText(Artwork.this, "text", Toast.LENGTH_LONG).show();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文