Skip to content

Instantly share code, notes, and snippets.

@amlcurran
Last active December 13, 2015 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amlcurran/4986106 to your computer and use it in GitHub Desktop.
Save amlcurran/4986106 to your computer and use it in GitHub Desktop.
XmlFileParser example project
<xml>
<list-item>
<label>Functional Groups</label>
<type>-2</type>
<extra>fcn</extra>
</list-item>
<list-item>
<label>Kinetics</label>
<type>-1</type>
<extra>chem_kinetics</extra>
</list-item>
<list-item>
<label>Thermodynamics</label>
<type>-1</type>
<extra>chem_tdynamics</extra>
</list-item>
<list-item>
<label>Electrochemistry &amp; SCM</label>
<type>-1</type>
<extra>chem_electro</extra>
</list-item>
<list-item>
<label>Quantum Mechanics</label>
<type>-1</type>
<extra>chem_quantum</extra>
</list-item>
<list-item>
<label>Statistical Mechanics</label>
<type>-1</type>
<extra>chem_statmech</extra>
</list-item>
<list-item>
<label>Spectroscopy</label>
<type>-1</type>
<extra>chem_spectro</extra>
</list-item>
<list-item>
<label>Named Reactions</label>
<type>-2</type>
<extra>rxn</extra>
</list-item>
<list-item>
<label>Crystal Structures</label>
<type>-2</type>
<extra>cry</extra>
</list-item>
<list-item>
<label>Amino Acids</label>
<type>-2</type>
<extra>amn</extra>
</list-item>
</xml>
package com.espian.formulae.data;
/**
* Created with IntelliJ IDEA.
* User: alexcurran
* Date: 18/02/2013
*/
public class ListItem {
public String label;
public int type;
public String extra;
public ListItem() { }
public ListItem(String label, int type, String extra) {
this.label = label;
this.type = type;
this.extra = extra;
}
@Override
public String toString() {
return label;
}
}
package com.espian.formulae.lib;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.actionbarsherlock.app.SherlockListFragment;
import com.espian.formulae.content.Equation;
import com.espian.formulae.data.ListItem;
import com.espian.formulae.data.ProProvider;
import com.espian.formulae.data.ProviderHelper;
import com.espian.formulae.data.XmlListItemParser;
import com.espian.formulae.utils.Helper;
import java.util.List;
public class MenuFragment extends SherlockListFragment implements AdapterView.OnItemClickListener {
/.../
public void onActivityCreated(Bundle saved) {
super.onActivityCreated(saved);
extra = getArguments() == null ? null : getArguments().getString("extra");
type = getArguments() == null ? -1 : getArguments().getInt("type");
if (extra != null && type == TYPE_MENU) {
setArrayAdapter(getResources().getIdentifier("menu_" + extra, "raw", getActivity().getPackageName()));
}
}
protected void setArrayAdapter(int Rid) {
List<ListItem> items = new XmlListItemParser().readFile(Rid, getActivity(), "list-item");
setListAdapter(new ArrayAdapter<ListItem>(mHost, getMenuType(), items));
getListView().setOnItemClickListener(this);
}
protected int getMenuType() {
if (BaseHostActivity.isMultiView() && Helper.isPro(getActivity())) return android.R.layout.simple_list_item_activated_1;
return android.R.layout.simple_list_item_1;
}
}
package com.espian.formulae.data;
import com.espian.formulae.lib.XmlFileParser;
/**
* Created with IntelliJ IDEA.
* User: alexcurran
* Date: 19/02/2013
*/
public class XmlListItemParser extends XmlFileParser<ListItem> {
public XmlListItemParser() {
super();
}
@Override
public ListItem createBlankClass() {
return new ListItem();
}
@Override
public Object parseValue(String tag, String text) {
if (tag.equals(XmlListParser.TAG_TYPE))
return Integer.valueOf(text);
else return super.parseValue(tag, text);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment