//主界面
package com.example.xmlpull14_2;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Parser;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Xml;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
//定义集合,用来接收解析的对象
private List<Book> list_book;
// private TextView tv;
private ListView mListView;
private SQLiteDatabase sd;
private BookOpenHelper mBookOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBookOpenHelper=new BookOpenHelper(getApplicationContext(), "pullxml.db", null, 1);
sd=mBookOpenHelper.getReadableDatabase();
mListView=(ListView) findViewById(R.id.mylistiew);
// tv=(TextView) findViewById(R.id.tv);
try {
intodata();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// tv.setText(list_book.toString());
mListView.setAdapter(adapter);
}
private void intodata() throws XmlPullParserException, IOException {
Book book=null;
String tagName="";
XmlPullParser parser=Xml.newPullParser();
//
parser.setInput(getAssets().open("books.xml"), "utf-8");
//定义一个事件类型
int eventtype=parser.getEventType();
while (eventtype!=parser.END_DOCUMENT){
switch (eventtype) {
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.START_TAG:
tagName=parser.getName();
if (tagName.equals("books")) {
list_book=new ArrayList<Book>();
}else if(tagName.equals("book")){
book=new Book();
String id=parser.getAttributeValue(0);
book.setId(Integer.parseInt(id));
}
break;
case XmlPullParser.TEXT:
String content=parser.getText();
if (tagName.equals("name")) {
book.setName(content);
}else if(tagName.equals("price")){
book.setPrice(content);
}else if(tagName.equals("type")){
book.setType(content);
}
break;
case XmlPullParser.END_TAG:
tagName=parser.getName();
if (tagName.equals("book")) {
list_book.add(book);
sd.execSQL("insert into book(id,name,price,type)values(null,?,?,?)", new Object[]{book.getName(),book.getPrice(),book.getType()});
book=null;
}
tagName="";
break;
case XmlPullParser.END_DOCUMENT:
break;
default:
break;
}
eventtype=parser.next();
}
}
/**
* 定义适配器
*/
BaseAdapter adapter=new BaseAdapter() {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder v;
if (convertView==null) {
v=new ViewHolder();
convertView=LayoutInflater.from(getApplicationContext()).inflate(R.layout.list_item, null);
v.name=(TextView) convertView.findViewById(R.id.name);
v.price=(TextView) convertView.findViewById(R.id.price);
v.type=(TextView) convertView.findViewById(R.id.type);
convertView.setTag(v);
}else{
v=(ViewHolder) convertView.getTag();
}
v.name.setText(list_book.get(position).getName());
v.price.setText(list_book.get(position).getPrice());
v.type.setText(list_book.get(position).getType());
return convertView;
}
class ViewHolder{
TextView name,price,type;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list_book.get(position);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list_book.size();
}
};
}