对不起,我在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() ???
}
--------------------------------------------------------------
如何在我的类Artwork中使用Toast类(或任何UI类),我从不直接实例化,但它是通过调用serial.read(Artworks.class)间接创建的? 我该怎么办呢?
提前致谢!
答案 0 :(得分:1)
您可以使用“静态”辅助类来维护对Activity的上下文的静态引用。因此,在反序列化您的艺术作品课程之前,请致电UtilClass.setContext(getApplicationContext());
。然后,在您的Artwork类中,您可以检索该上下文,然后执行Toast.makeText(UtilClass.getContext(), "text", Toast.LENGTH_LONG).show();
。
答案 1 :(得分:0)
import android.widget.Toast;
public class Artwork {
Toast.makeText(Artwork.this, "text", Toast.LENGTH_LONG).show();
}