mp3播放器(三)(代码重构)

package com.example.mp3player00;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import xml.Mp3ListContentHandler;

import model.Mp3Info;

import com.example.download.HttpDownloader;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SimpleAdapter;

public class Mp3ListActivity extends ListActivity {
    private static final int upadate=1;
    private static final int about=2;
   //以上定义常量,看到值的名字就知道按钮的名字    
   //且为后面修改提供方便
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mp3_list);
        updateListView();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_mp3_list, menu);
        menu.add(0, 1, 1,R.string.mp3list_update);
        menu.add(0, 2, 2, R.string.mp3list_about);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        //System.out.println("--->"+item.getItemId());
        if(item.getItemId()==upadate)
        {
            updateListView();
            
            
        }
        else if(item.getItemId()==about)
        {
            //用户点击了关于按钮
        }
        return super.onOptionsItemSelected(item);
    }
    private void updateListView(){
        //用户点击了更新按钮
        
        String xml=downloadXML("http://192.168.1.100:8080/mp3/resources.xml");
        //System.out.println("----->"+xml);
        
            List<Mp3Info> mp3infos = null;
            try {
                mp3infos = parse(xml);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }//List<Mp3Info>可以看出一个int,String这样一个类型
            //构建Adapter,首先生成一个List对象。List对象里面存的是HashMap,HashMap里面存的是String
        
            List<HashMap<String,String>> list=new ArrayList<HashMap<String,String>>();
           //将Mp3Info迭代出来
            for (Iterator iterator = mp3infos.iterator(); iterator.hasNext();) {
                Mp3Info mp3Info = (Mp3Info) iterator.next();
                 //生成一个HashMap对象
                HashMap<String,String> map=new HashMap<String,String>();
                //构造一个map
                map.put("mp3_name", mp3Info.getMp3Name());
                map.put("mp3_size", mp3Info.getMp3Size());
                //构造好map后添加到List中,即将HashMap添加到List中
                list.add(map);
            }
            //生成adapter
            SimpleAdapter simpleAdapter=
new SimpleAdapter(this,list,R.layout.mp3info_item,new String[]{"mp3_name","mp3_size"},new int[]{R.id.mp3_name,R.id.mp3_size}); setListAdapter(simpleAdapter); } //写一个单独的下载函数,更加清晰 private String downloadXML(String urlStr) { HttpDownloader httpdownloader=new HttpDownloader(); String result=httpdownloader.download(urlStr); return result; } //在这个函数中解析xml //首先生成xml解析器 private List<Mp3Info> parse(String xmlStr) throws IOException { List<Mp3Info> info=new ArrayList<Mp3Info>(); SAXParserFactory saxParserFactory=SAXParserFactory.newInstance(); try { XMLReader xmlReader=saxParserFactory.newSAXParser().getXMLReader(); Mp3ListContentHandler mp3ListContentHandler=new Mp3ListContentHandler(info); xmlReader.setContentHandler(mp3ListContentHandler); xmlReader.parse(new InputSource(new StringReader(xmlStr))); //解析完数据都天道info里面,然后用for循环迭代出来 for (Iterator iterator = info.iterator(); iterator.hasNext();) { Mp3Info mp3Info = (Mp3Info) iterator.next(); System.out.println(mp3Info); } } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } return info; } }


将updateListView()继续重构

  1 package com.example.mp3player00;
  2 
  3 import java.io.IOException;
  4 import java.io.StringReader;
  5 import java.util.ArrayList;
  6 import java.util.HashMap;
  7 import java.util.Iterator;
  8 import java.util.List;
  9 
 10 import javax.xml.parsers.ParserConfigurationException;
 11 import javax.xml.parsers.SAXParserFactory;
 12 
 13 import org.xml.sax.InputSource;
 14 import org.xml.sax.SAXException;
 15 import org.xml.sax.XMLReader;
 16 
 17 import xml.Mp3ListContentHandler;
 18 
 19 import model.Mp3Info;
 20 
 21 import com.example.download.HttpDownloader;
 22 
 23 import android.os.Bundle;
 24 import android.app.Activity;
 25 import android.app.ListActivity;
 26 import android.view.Menu;
 27 import android.view.MenuItem;
 28 import android.widget.SimpleAdapter;
 29 
 30 public class Mp3ListActivity extends ListActivity {
 31     private static final int upadate=1;
 32     private static final int about=2;
 33    //以上定义常量,看到值的名字就知道按钮的名字    
 34    //且为后面修改提供方便
 35     @Override
 36     protected void onCreate(Bundle savedInstanceState) {
 37         super.onCreate(savedInstanceState);
 38         setContentView(R.layout.activity_mp3_list);
 39         updateListView();
 40     }
 41 
 42     @Override
 43     public boolean onCreateOptionsMenu(Menu menu) {
 44         // Inflate the menu; this adds items to the action bar if it is present.
 45         getMenuInflater().inflate(R.menu.activity_mp3_list, menu);
 46         menu.add(0, 1, 1,R.string.mp3list_update);
 47         menu.add(0, 2, 2, R.string.mp3list_about);
 48         return true;
 49     }
 50 
 51     @Override
 52     public boolean onOptionsItemSelected(MenuItem item) {
 53         // TODO Auto-generated method stub
 54         //System.out.println("--->"+item.getItemId());
 55         if(item.getItemId()==upadate)
 56         {
 57             updateListView();
 58             
 59             
 60         }
 61         else if(item.getItemId()==about)
 62         {
 63             //用户点击了关于按钮
 64         }
 65         return super.onOptionsItemSelected(item);
 66     }
 67     private SimpleAdapter buildSimpleAdapter(List<Mp3Info> mp3infos){
 68         //构建Adapter,首先生成一个List对象。List对象里面存的是HashMap,HashMap里面存的是String
 69         
 70         List<HashMap<String,String>> list=new ArrayList<HashMap<String,String>>();
 71         //将Mp3Info迭代出来
 72         for (Iterator iterator = mp3infos.iterator(); iterator.hasNext();) {
 73             Mp3Info mp3Info = (Mp3Info) iterator.next();
 74              //生成一个HashMap对象
 75             HashMap<String,String> map=new HashMap<String,String>();
 76             //构造一个map
 77             map.put("mp3_name", mp3Info.getMp3Name());
 78             map.put("mp3_size", mp3Info.getMp3Size());
 79             //构造好map后添加到List中,即将HashMap添加到List中
 80             list.add(map);
 81         }
 82         //生成adapter
 83         SimpleAdapter simpleAdapter=
new SimpleAdapter(this,list,R.layout.mp3info_item,new String[]{"mp3_name","mp3_size"},new int[]{R.id.mp3_name,R.id.mp3_size}); 84 85 86 return simpleAdapter; 87 88 } 89 private void updateListView(){ 90 //用户点击了更新按钮 91 92 String xml=downloadXML("http://192.168.1.100:8080/mp3/resources.xml"); 93 //System.out.println("----->"+xml); 94 95 List<Mp3Info> mp3infos = null; 96 try { 97 mp3infos = parse(xml); 98 } catch (IOException e) { 99 // TODO Auto-generated catch block 100 e.printStackTrace(); 101 }//List<Mp3Info>可以看出一个int,String这样一个类型 102 SimpleAdapter simpleAdapter=buildSimpleAdapter(mp3infos); 103 setListAdapter(simpleAdapter); 104 105 } 106 //写一个单独的下载函数,更加清晰 107 private String downloadXML(String urlStr) 108 { 109 110 HttpDownloader httpdownloader=new HttpDownloader(); 111 String result=httpdownloader.download(urlStr); 112 return result; 113 } 114 //在这个函数中解析xml 115 //首先生成xml解析器 116 private List<Mp3Info> parse(String xmlStr) throws IOException 117 { 118 List<Mp3Info> info=new ArrayList<Mp3Info>(); 119 SAXParserFactory saxParserFactory=SAXParserFactory.newInstance(); 120 try { 121 XMLReader xmlReader=saxParserFactory.newSAXParser().getXMLReader(); 122 Mp3ListContentHandler mp3ListContentHandler=new Mp3ListContentHandler(info); 123 xmlReader.setContentHandler(mp3ListContentHandler); 124 xmlReader.parse(new InputSource(new StringReader(xmlStr))); 125 //解析完数据都天道info里面,然后用for循环迭代出来 126 for (Iterator iterator = info.iterator(); iterator.hasNext();) { 127 Mp3Info mp3Info = (Mp3Info) iterator.next(); 128 System.out.println(mp3Info); 129 } 130 131 132 } catch (SAXException e) { 133 // TODO Auto-generated catch block 134 e.printStackTrace(); 135 } catch (ParserConfigurationException e) { 136 // TODO Auto-generated catch block 137 e.printStackTrace(); 138 } 139 return info; 140 141 } 142 143 }

 

posted @ 2010-10-12 00:12  Kstyjobx!!  阅读(459)  评论(0编辑  收藏  举报