Toast challenge


#1

Where is the best place for the toast? We need the xml from Flickr so I found the total photos while parsing:

[code] void parseItems(ArrayList items, XmlPullParser parser)
throws XmlPullParserException, IOException {
int eventType = parser.next();

	while (eventType != XmlPullParser.END_DOCUMENT) {
		if (eventType == XmlPullParser.START_TAG && TOTAL_PHOTO.equals(parser.getName())
				&& searched == 1 && mContext != null) {
			CharSequence total = (parser.getAttributeValue(null, "total")) + " results";
			Log.i(TAG, "Total number of photos = " + total);

// Toast.makeText(mContext, total, Toast.LENGTH_SHORT).show();
}
if (eventType == XmlPullParser.START_TAG && XML_PHOTO.equals(parser.getName())) {
String id = parser.getAttributeValue(null, “id”);
String caption = parser.getAttributeValue(null, “title”);
String smallUrl = parser.getAttributeValue(null, EXTRA_SMALL_URI);

			GalleryItem item = new GalleryItem();
			item.setId(id);
			item.setCaption(caption);
			item.setUrl(smallUrl);
			items.add(item);
		}
		
		eventType = parser.next();
	}
}

[/code]

However, the Toast causes a force close when placed here. The context comes from:

in the FetchItemsTask method.


#2

Try displaying your toast from the main thread. I can’t say with certainty without a stack trace, but I imagine that Android doesn’t want you to show a Toast from a background thread (which is where your parse code is running).