From aaf05368e2dca6e6752007ec6c1a9dc90b0524db Mon Sep 17 00:00:00 2001 From: Robert Kolner Date: Wed, 21 Sep 2011 21:46:33 +0200 Subject: [PATCH] Much work done on how the app shows special characters. Now it is in sync with the server and it seems like it works. --- src/com/studentersamfundet/app/Event.java | 10 +++---- .../studentersamfundet/app/FeedFetcher.java | 18 +++++++++---- src/com/studentersamfundet/app/XmlParser.java | 2 +- .../app/ui/EventViewActivity.java | 17 +++++------- .../app/ui/ProgramListActivity.java | 26 +++++++++++-------- 5 files changed, 40 insertions(+), 33 deletions(-) diff --git a/src/com/studentersamfundet/app/Event.java b/src/com/studentersamfundet/app/Event.java index 7aea4a4..0e1a0cf 100644 --- a/src/com/studentersamfundet/app/Event.java +++ b/src/com/studentersamfundet/app/Event.java @@ -22,11 +22,11 @@ public class Event implements Serializable { String category) { this.id = id; this.title = title; - this.description = description; - this.date = date; - this.location = location; - this.text = text; - this.category = category; + this.description = (description == null) ? "" : description; + this.date = (date == null) ? "" : date; + this.location = (location == null) ? "" : location; + this.text = (text == null) ? "" : text; + this.category = (category == null) ? "" : category; } public String toString() { diff --git a/src/com/studentersamfundet/app/FeedFetcher.java b/src/com/studentersamfundet/app/FeedFetcher.java index 41da7f4..cf4953a 100644 --- a/src/com/studentersamfundet/app/FeedFetcher.java +++ b/src/com/studentersamfundet/app/FeedFetcher.java @@ -22,10 +22,18 @@ import android.content.Context; public class FeedFetcher { private final String LOCAL_FILENAME = "dns_events"; - private InputStream openHttpConnection(String URL) throws IOException { + private Context context; + private String url; + + public FeedFetcher(Context context, String url) { + this.context = context; + this.url = url; + } + + private InputStream openHttpConnection() throws IOException { InputStream in = null; int response = -1; - URL url = new URL(URL); + URL url = new URL(this.url); URLConnection con = url.openConnection(); if (!(con instanceof HttpURLConnection)) @@ -53,20 +61,20 @@ public class FeedFetcher { * @throws IOException Is thrown if the app cannot download the file from * the internet and there is no local copy. */ - public NodeList fetch(String URL, Context c) throws IOException { + public NodeList fetch() throws IOException { InputStream in = null; NodeList itemNodes = null; /* Update the local file if possible. */ try { - saveFile(openHttpConnection(URL), c); + saveFile(openHttpConnection(), context); } catch(IOException e) { /* OK, so we failed to update it. It doesn't matter yet, because we can try to... */ } /* ...load local file: */ - in = loadFile(c); // if we fail here, we fail ultimately. Throw the Exception! + in = loadFile(context); // if we fail here, we fail ultimately. Throw the Exception! Document doc = null; diff --git a/src/com/studentersamfundet/app/XmlParser.java b/src/com/studentersamfundet/app/XmlParser.java index c24162f..3b66d75 100644 --- a/src/com/studentersamfundet/app/XmlParser.java +++ b/src/com/studentersamfundet/app/XmlParser.java @@ -34,7 +34,7 @@ public class XmlParser { NodeList descriptionNodes = (itemElement).getElementsByTagName("description"); NodeList dateNodes = (itemElement).getElementsByTagName("date"); NodeList locationNodes = (itemElement).getElementsByTagName("location"); - NodeList textNodes = (itemElement).getElementsByTagName("text"); + NodeList textNodes = (itemElement).getElementsByTagName("content"); NodeList categoryNodes = (itemElement).getElementsByTagName("category"); // Convert a Node into an Element. diff --git a/src/com/studentersamfundet/app/ui/EventViewActivity.java b/src/com/studentersamfundet/app/ui/EventViewActivity.java index ff4ffe5..4d0974c 100644 --- a/src/com/studentersamfundet/app/ui/EventViewActivity.java +++ b/src/com/studentersamfundet/app/ui/EventViewActivity.java @@ -31,20 +31,15 @@ public class EventViewActivity extends BaseDnsActivity { TextView datetime = (TextView) findViewById(R.id.event_view_datetime); Button link = (Button) findViewById(R.id.event_view_link); - title.setText(e.title); - location.setText(e.location); - datetime.setText(e.date); + title.setText(Html.fromHtml(e.title)); + location.setText(Html.fromHtml(e.location)); + datetime.setText(Html.fromHtml(e.date)); - StringBuilder sb = new StringBuilder(); - if (e.description.length() > 0) { - sb.append(""); - sb.append(e.description); - sb.append("

"); - } if (e.text.length() > 0) { - sb.append(e.text); + description.setText(Html.fromHtml(e.text)); + } else if (e.description.length() > 0) { + description.setText(Html.fromHtml(e.description)); } - description.setText(Html.fromHtml(sb.toString())); if (e.id > 0) { // if id exists and seems legit link.setVisibility(View.VISIBLE); diff --git a/src/com/studentersamfundet/app/ui/ProgramListActivity.java b/src/com/studentersamfundet/app/ui/ProgramListActivity.java index f90ba4c..39d2945 100644 --- a/src/com/studentersamfundet/app/ui/ProgramListActivity.java +++ b/src/com/studentersamfundet/app/ui/ProgramListActivity.java @@ -6,6 +6,7 @@ import org.w3c.dom.NodeList; import android.content.Intent; import android.os.Bundle; +import android.text.Html; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -23,24 +24,27 @@ import com.studentersamfundet.app.R; import com.studentersamfundet.app.XmlParser; public class ProgramListActivity extends BaseDnsActivity { - public static final String feedURL = "http://dl.dropbox.com/u/293287/test.xml"; - //public static final String feedURL = "http://studentersamfundet.no/"; - public static final String localURL = "/data/com.studentersamfundet.app/files/dns_events"; - public static FeedFetcher feed = new FeedFetcher(); - public static XmlParser parser = new XmlParser(); - public DataHandler dh; + //public static final String feedURL = "http://dl.dropbox.com/u/293287/test.xml"; + public static final String feedURL = "http://studentersamfundet.no/rss/lars_program_feed.php"; + + private FeedFetcher feed; + private XmlParser parser; + private DataHandler dataHandler; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.event_list); + this.feed = new FeedFetcher(this, feedURL); + this.parser = new XmlParser(); + // Do we have intarwebs? // YAY = Fetch the feed. // NAY = Inform about no connection. try { - NodeList itemNodes = feed.fetch(feedURL, this); - dh = parser.parse(itemNodes); + NodeList itemNodes = feed.fetch(); + dataHandler = parser.parse(itemNodes); } catch (IOException e) { Toast toast = Toast.makeText(this, R.string.error_noconnection_noupdate, Toast.LENGTH_LONG); @@ -54,7 +58,7 @@ public class ProgramListActivity extends BaseDnsActivity { } protected ListAdapter createAdapter() { - Event[] events = dh.populateList(); + Event[] events = dataHandler.populateList(); ListAdapter adapter = new ArrayAdapter(this, R.layout.event_list_row, R.id.event_list_row_text, events) { @Override @@ -71,10 +75,10 @@ public class ProgramListActivity extends BaseDnsActivity { final Event e = getItem(position); TextView titleView = (TextView) row.findViewById(R.id.event_list_row_text); - titleView.setText(e.title); + titleView.setText(Html.fromHtml(e.title)); TextView dateView = (TextView) row.findViewById(R.id.event_list_row_date); - dateView.setText(e.getDateString()); + dateView.setText(Html.fromHtml(e.getDateString())); row.setOnClickListener(new OnClickListener() {