//这个是SAX解析的所有
public class XMLContentHandler extends DefaultHandler {
private boolean IsDisplayNameTag =false;
private boolean IsAvatarTag =false;
private boolean IsReputationTotalTag =false;
private boolean IsUserTag =false;
private String DisplayName="DisplayName";
private String Avatar="Avatar";
private String ReputationTotal="ReputationTotal";
private Users users ;
private List<Users> list;
public List<Users> getUsers() {
return list;
}
//打开xml文档的回调函数
@Override
public void startDocument() throws SAXException{
//这里做list的初始化工作
Log.e("+++++++++>1","1");
list =new ArrayList<Users>();
}
//一旦发现元素开始标记就回调此函数
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (localName.equals("User")) {
users =new Users();
IsUserTag =true;
}
if (localName.equals(DisplayName)) {
IsDisplayNameTag=true;
}else if (localName.equals(Avatar)) {
IsAvatarTag =true;
}else if (localName.equals(ReputationTotal)) {
IsReputationTotalTag =true;
}
}
//一发现元素值或属性值就回调此函数
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
String data =new String(ch,start,length);
if (IsDisplayNameTag) {
users.setDisplayName(data);
}else if (IsAvatarTag) {
users.setAvatar(data);
}else if (IsReputationTotalTag) {
users.setReputationTotal(data);
// Log.i("uuuuuuuuuuuu3",data);
}else {
}
}
//一旦发现元素结束标记就回调此函数
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (localName.equals("User")) {
IsUserTag=false;
list.add(users);
}
if (localName.equals(DisplayName)) {
IsDisplayNameTag =false;
}else if (localName.equals(Avatar)) {
IsAvatarTag =false;
}else if (localName.equals(ReputationTotal)) {
IsReputationTotalTag =false;
}
}
//关闭xml文档的回调函数
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
Log.e("+++++++++>5","5");
super.endDocument();
}
}
// 解析的标签
* User DisplayName Avatar ReputationTotal需要解析的标签
* */
public class Users {
private String User;
private String DisplayName;
private String Avatar;
private String ReputationTotal;
public String getUser() {
return User;
}
public void setUser(String user) {
User = user;
}
public String getDisplayName() {
return DisplayName;
}
public void setDisplayName(String displayName) {
DisplayName = displayName;
}
public String getAvatar() {
return Avatar;
}
public void setAvatar(String avatar) {
Avatar = avatar;
}
public String getReputationTotal() {
return ReputationTotal;
}
public void setReputationTotal(String reputationTotal) {
ReputationTotal = reputationTotal;
}
}
/**自定义的MyAdapter 继承BaseAdapter
*最下面是一个ASYNCTACK的异步刷新
*
*/
public class MyAdapter extends BaseAdapter {
private LayoutInflater inflater;
private List<Users> items;
public MyAdapter(Context context,List<Users> it){
inflater =LayoutInflater.from(context);
items =it;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout layout =(LinearLayout) inflater.inflate(R.layout.listone, null);
Users user =items.get(position);
TextView txtname =(TextView) layout.findViewById(R.id.txtup);
txtname.setTextColor(Color.BLACK);
txtname.setTextSize(20);
txtname.setText(user.getDisplayName());
TextView txtrep = (TextView) layout.findViewById(R.id.txtdown);
txtname.setTextColor(Color.BLACK);
txtrep.setTextSize(10);
txtrep.setText(user.getReputationTotal());
ImageView img =(ImageView) layout.findViewById(R.id.img01);
String image =user.getAvatar();
img.setTag(image);
new AsyncLoader().execute(img);
img.setDrawingCacheEnabled(true);
return layout;
}
//继承于AsyncTask 刷新ImageView,未实现图片下载到SD卡的功能;
class AsyncLoader extends AsyncTask <ImageView, Void, Bitmap>{
private ImageView gView;
protected Bitmap doInBackground(ImageView... views) {
Bitmap bmp = null;
ImageView view = views[0];
// 根据iconUrl获取图片并渲染,iconUrl的url放在了view的tag中。
if (view.getTag() != null) {
try {
URL url = new URL(view.getTag().toString());
GetImage image =new GetImage();
bmp =image.getImage(url);
} catch (Exception e) {
Log.v("img", e.getMessage());
return null;
}
}
this.gView = view;
return bmp;
}
protected void onPostExecute(Bitmap bm) {
if (bm != null) {
this.gView.setImageBitmap(bm);
this.gView = null;
}
}
}
}