Android, how to toast my simplexml class?
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.Per开发者_如何转开发sister;
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!
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();
.
import android.widget.Toast;
public class Artwork {
Toast.makeText(Artwork.this, "text", Toast.LENGTH_LONG).show();
}
精彩评论