Note that there are some explanatory texts on larger screens.

plurals
  1. POmove code out of onCreate method and into its own class file
    primarykey
    data
    text
    <p>I have the code below which I am using from an Android java tutorial. </p> <p>The code is all in the apps main java file onCreate method, and I would like to move the XML processing part to a separate class. </p> <p>I tried doing this several different ways, but all of them ended up failing horribly and became more cumbersome that they were worth.</p> <p>So my question is, is there a good/standard way of moving the code out of the main java file and into a separate class?</p> <pre><code>public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView myXmlContent = (TextView)findViewById(R.id.my_xml); String stringXmlContent; try { stringXmlContent = getEventsFromAnXML(this); myXmlContent.setText(stringXmlContent); } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private String getEventsFromAnXML(Activity activity) throws XmlPullParserException, IOException { StringBuffer stringBuffer = new StringBuffer(); Resources res = activity.getResources(); XmlResourceParser xpp = res.getXml(R.xml.myxml); String elType = "mt"; int counter = 1; int mainTopics = 0; int subTopics = 0; xpp.next(); int eventType = xpp.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { if(eventType == XmlPullParser.START_DOCUMENT) { stringBuffer.append("News Categories"); } else if(eventType == XmlPullParser.START_TAG) { if(xpp.getName().equalsIgnoreCase("subTopic")) { elType = "st"; subTopics += 1; } else if(xpp.getName().equalsIgnoreCase("mainTopic")) { elType = "mt"; mainTopics += 1; } else { elType = ""; } //stringBuffer.append("\nSTART_TAG: "+xpp.getName()); } else if(eventType == XmlPullParser.END_TAG) { //stringBuffer.append("\nEND_TAG: "+xpp.getName()); } else if(eventType == XmlPullParser.TEXT) { if(elType.equalsIgnoreCase("st")) { stringBuffer.append("\n *"+xpp.getText()); } else { stringBuffer.append("\n " + counter + ") " + xpp.getText()); counter+= 1; } } eventType = xpp.next(); } //stringBuffer.append("\n--- End XML ---"); stringBuffer.append("\n Total Topics: " + mainTopics + " Total SubTopics: " + subTopics); return stringBuffer.toString(); } </code></pre> <p>I got this to work: moving it into another class called readXML like this:</p> <pre><code> readXML processXml = new readXML(); try { stringXmlContent = processXml.getEventsFromAnXML(this); myXmlContent.setText(stringXmlContent); } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } </code></pre> <p>I just wrapped the method "private String getEventsFromAnXML" in the new class </p> <p>like this:</p> <pre><code>public class readXML { private String getEventsFromAnXML(Activity activity) ....process XML... } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload