我正在将listview示例转换为通过读取xml文件然后使用其内容填充列表视图来运行的程序。我似乎遇到了障碍。我不知道如何处理我为第一个例子制作的适配器以及如何在这个新程序中使用它,任何指针都会受到赞赏。 主要是
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);
继承了xml处理程序
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;
}
}
}
适配器视图,当它是一个简单的列表视图文件时,它是相同的。我不知道如何将xml考虑在内。
公共类PhonebookAdapterView扩展了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;
}
}
修改
MyXMLHAndler
public class MyXMLHandler扩展了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;
}
}
}
第二次编辑
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;
}
}
答案 0 :(得分:0)
要使其发挥作用,还有很多事情要做。
首先,您必须通过阅读XML文件来创建电话簿列表 并将数据放入MyXMLHandler类的List中 你的
public static Phonebook phonebook = null;
应该是
public static List<Phonebook> phonebook = new ArrayList<Phonebook>();
并在您的XMLHandler类中相应地填充 然后将此列表设置到适配器中 -
MyAdapter.setList(phonebook); // your method inside adapter
然后您正在使用此列表中的电话簿对象
在getView()
类中填充TextView和addView(TextView)
进入ConvertView对象并返回它。
我在这篇文章how can i select and kill multiple application中制作了一个示例列表应用程序 或者有很多关于如何创建列表视图的帖子。
更新:
在您的XMLHandler类中
从电话簿public static List<Phonebook> phonebook
中删除静态
来自set / getPhonebook()方法
您正在修改课程并在活动课程中使用myXMLHandler.getPhonebook();
。
假设您有以下xml
<phonebook>
<entry>
<name>abc</name>
<phone>def</phone>
<mail>ghi</mail>
</entry>
<entry>
<name>abc1</name>
<phone>def2</phone>
<mail>ghi3</mail>
</entry>
</phonebook>
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.util.ArrayList;
import java.util.List;
public class ListActivity extends Activity {
ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(lp1);
ll.setOrientation(1);
lv = new ListView(this);
MyXMLHandler myXMLHandler = readXML();
List<Phonebook> listOfPhonebook = myXMLHandler.getPhonebook();
//adapter = new TaskListAdapter(this);
MyListAdapter adapter = new MyListAdapter(this, listOfPhonebook);
lv.setAdapter(adapter);
ll.addView(lv);
setContentView(ll);
}
private MyXMLHandler readXML() {
MyXMLHandler myXMLHandler = null;
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.a));
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(is.getByteStream()));
} catch (Exception e) {
System.out.println("XML Parsing Exception = " + e);
}
return myXMLHandler;
}
@Override
protected void onDestroy() {
super.onDestroy();
}
class MyListAdapter extends BaseAdapter {
private static final String TAG = "TaskListAdapter";
List<Phonebook> phonebook;
Context context;
public MyListAdapter(Context context, List<Phonebook> phonebook) {
this.phonebook = phonebook;
Log.d(TAG, "created new task list adapter");
this.context = context;
}
public int getCount() {
return phonebook.size();
}
public Phonebook getItem(int position) {
return phonebook.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
RelativeLayout rl = new RelativeLayout(context);
TextView textPid = new TextView(context);
textPid.setId(222222);
textPid.setText(getItem(position).getName());
TextView textName = new TextView(context);
textName.setId(333333);
textName.setText(getItem(position).getEmail());
TextView textEmail = new TextView(context);
textEmail.setId(444444);
textEmail.setText(getItem(position).getEmail());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(1, 0, 0, 0);
rl.addView(textName, lp);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.RIGHT_OF, textName.getId());
lp1.setMargins(1, 0, 0, 0);
rl.addView(textPid, lp1);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.RIGHT_OF, textPid.getId());
lp2.setMargins(1, 0, 0, 0);
rl.addView(textEmail, lp2);
return rl;
}
}
class MyXMLHandler extends DefaultHandler {
Boolean currentElement = false;
String currentValue = null;
public List<Phonebook> phonebook = new ArrayList<Phonebook>();
int entryCount = 0;
public List<Phonebook> getPhonebook() {
return phonebook;
}
public void setPhonebook(List<Phonebook> p) {
phonebook = p;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
currentElement = true;
if (localName.equals("entry")) {
/** Start */
Phonebook pb = new Phonebook();
getPhonebook().add(pb);
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
/** set value */
if (localName.equalsIgnoreCase("name"))
(phonebook.get(entryCount)).setName(currentValue);
if (localName.equalsIgnoreCase("phone"))
(phonebook.get(entryCount)).setPhone(currentValue);
if (localName.equalsIgnoreCase("mail"))
(phonebook.get(entryCount)).setEmail(currentValue);
if (localName.equalsIgnoreCase("entry")) entryCount++;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (currentElement) {
currentValue = new String(ch, start, length);
currentElement = false;
}
}
}
}