I am in the middle of converting a listview example into a program that operates by reading an xml file and then populate a list view with its contents. I seem to have hit a snag though. I’m not sure what to do with the Adapter I made for the first example and how to use it in this new program, any pointers would be appreciated.
Heres the main
Phonebook phonebook = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView list = (ListView) findViewById(R.id.ListView01);
list.setClickable(true);
final List<Phonebook> listOfPhonebook = new ArrayList<Phonebook>();
PhonebookAdapterView adapter = new PhonebookAdapterView(this, listOfPhonebook);
list.setAdapter(adapter);
try {
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Send URL to parse XML Tags */
InputSource is = new InputSource(getResources().openRawResource(R.raw.example));
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(is.getByteStream()));
} catch (Exception e) {
System.out.println("XML Parsing Exception = " + e);
}
phonebook = MyXMLHandler.phonebook;
list.setAdapter(adapter);
Heres the xml handler
Boolean currentElement = false;
String currentValue = null;
public static Phonebook phonebook = null;
public static Phonebook getPhonebook() {
return phonebook;
}
public static void setPhonebook(Phonebook p) {
MyXMLHandler.phonebook = p;
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
currentElement = true;
if (localName.equals("maintag"))
{
/** Start */
phonebook = new Phonebook();
}
}
/** Called when tag closing ( ex:- <name>AndroidPeople</name>
* -- </name> )*/
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
currentElement = false;
/** set value */
if (localName.equalsIgnoreCase("name"))
phonebook.setName(currentValue);
else if (localName.equalsIgnoreCase("phone"))
phonebook.setPhone(currentValue);
else if (localName.equalsIgnoreCase("Mail"))
phonebook.setMail(currentValue);
}
/** Called to get tag characters ( ex:- <name>AndroidPeople</name>
* -- to get AndroidPeople Character ) */
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (currentElement) {
currentValue = new String(ch, start, length);
currentElement = false;
}
}
}
And the adapter view, which is the same back when it was a simple listview file. Im not sure how to factor in xml though.
public class PhonebookAdapterView extends BaseAdapter {
private Context context;
private List<Phonebook> phonebook;
public PhonebookAdapterView(Context theContext, List<Phonebook> theListPhonebook) {
context = theContext;
phonebook = theListPhonebook;
}
@Override
public int getCount() {
return phonebook.size();
}
@Override
public Object getItem(int position) {
return phonebook.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
Phonebook entry = phonebook.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.phone_row, null);
}
TextView tvContact = (TextView) convertView.findViewById(R.id.tvContact);
tvContact.setText(entry.getName());
TextView tvPhone = (TextView) convertView.findViewById(R.id.tvMobile);
tvPhone.setText(entry.getPhone());
TextView tvMail = (TextView) convertView.findViewById(R.id.tvMail);
tvMail.setText(entry.getMail());
return convertView;
}
}
EDIT
MyXMLHAndler
public class MyXMLHandler extends DefaultHandler {
Boolean currentElement = false;
String currentValue = null;
public static List<Phonebook> phonebook = new ArrayList<Phonebook>();
public static List<Phonebook> getPhonebook() {
return phonebook;
}
public static void setPhonebook(List<Phonebook> p) {
MyXMLHandler.phonebook = p;
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
currentElement = true;
if (localName.equals("maintag"))
{
/** Start */
phonebook = new ArrayList<Phonebook>();
}
}
/** Called when tag closing ( ex:- <name>AndroidPeople</name>
* -- </name> )*/
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
currentElement = false;
/** set value */
if (localName.equalsIgnoreCase("name"))
((Phonebook) phonebook).setName(currentValue);
else if (localName.equalsIgnoreCase("phone"))
((Phonebook) phonebook).setPhone(currentValue);
else if (localName.equalsIgnoreCase("Mail"))
((Phonebook) phonebook).setMail(currentValue);
}
/** Called to get tag characters ( ex:- <name>AndroidPeople</name>
* -- to get AndroidPeople Character ) */
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (currentElement) {
currentValue = new String(ch, start, length);
currentElement = false;
}
}
}
Second Edit
public List<Phonebook> phonebook = new ArrayList<Phonebook>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView list = (ListView) findViewById(R.id.ListView01);
list.setClickable(true);
final List<Phonebook> listOfPhonebook = MyXMLHandler.getPhonebook();
PhonebookAdapterView adapter = new PhonebookAdapterView(this, listOfPhonebook);
list.setAdapter(adapter);
try {
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Send URL to parse XML Tags */
InputSource is = new InputSource(getResources().openRawResource(R.raw.example));
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(is.getByteStream()));
} catch (Exception e) {
System.out.println("XML Parsing Exception = " + e);
}
XMLHandler
public class MyXMLHandler extends DefaultHandler {
Boolean currentElement = false;
String currentValue = null;
public List<Phonebook> phonebook = new ArrayList<Phonebook>();
public List<Phonebook> getPhonebook() {
return phonebook;
}
public void setPhonebook(List<Phonebook> p) {
phonebook = p;
}
int entryCount;
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
currentElement = true;
if (localName.equals("maintag"))
{
/** Start */
Phonebook pb = new Phonebook();
getPhonebook().add(pb);
entryCount++;
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
/** set value */
if (localName.equalsIgnoreCase("name"))
((Phonebook)phonebook.get(entryCount)).setName(currentValue);
if (localName.equalsIgnoreCase("phone"))
((Phonebook)phonebook.get(entryCount)).setPhone(currentValue);
if (localName.equalsIgnoreCase("Mail"))
((Phonebook)phonebook.get(entryCount)).setMail(currentValue);
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (currentElement) {
currentValue = new String(ch, start, length);
currentElement = false;
}
}
There’re quite a few things to do to make it work.
First of all you will have to create your phonebook list by reading an XML file
and putting a data into a List of the MyXMLHandler class
your
should be
and populated accordingly within your XMLHandler class
and after that you set this list into your adapter –
Then you’re using the Phonebook objects from this list
within your
getView()class to populate your TextViews andaddView(TextView)theminto your ConvertView object and return it.
I’ve made an example list application in this post how can i select and kill multiple application
or there’s a bunch of posts on how to create a listview.
UPDATED:
in your XMLHandler class
Remove the statics from phonebook
public static List<Phonebook> phonebookas wellas from the set/getPhonebook() methods
you are NEWing the class and use the
myXMLHandler.getPhonebook();in your activity class.assuming you have the following xml
Working code: