MP3Player2.0版本

package guoximing.mp3player;

import guoximing.download.HttpDownloader;
import guoximing.model.Mp3Info;
import guoximing.xml.Mp3ListContentHandler;

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

import javax.xml.parsers.SAXParserFactory;

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

import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SimpleAdapter;

import com.example.mp3player.R;

@SuppressLint("NewApi")
public class MP3ListActivity extends ListActivity{
	
	private static final int UPDATE = 1;
	private static final int ABOUT  = 2;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		 //详见StrictMode文档
	    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
	                .detectDiskReads()
	                .detectDiskWrites()
	                .detectNetwork()   // or .detectAll() for all detectable problems
	                .penaltyLog()
	                .build());
	    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
	                .detectLeakedSqlLiteObjects()
	                .detectLeakedClosableObjects()
	                .penaltyLog()
	                .penaltyDeath()
	                .build());
	    
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0,UPDATE,1,R.string.mp3List_update);
		menu.add(0,ABOUT,2,R.string.mp3List_about);
		updateListView();
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(UPDATE == item.getItemId()){
			updateListView();
		}else{
			System.out.println(ABOUT);
		}
		return super.onOptionsItemSelected(item);
	}
	
	private String downloaderXML(String urlStr){
		HttpDownloader httpDownloader = new HttpDownloader();
		String result = null;
		result = httpDownloader.download(urlStr);
		return result;
	}
	
	
	private SimpleAdapter buildSimpleAdapter(List<Mp3Info> mp3Infos){
		List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		for (Iterator iterator = mp3Infos.iterator(); iterator.hasNext();) {
			Mp3Info mp3Info = (Mp3Info) iterator.next();

			HashMap<String, String> map = new HashMap<String, String>();
			map.put("mp3_name", mp3Info.getMp3Name());
			map.put("mp3_size", mp3Info.getMp3Size());
			list.add(map);
		}
		SimpleAdapter simpleAdapture = new SimpleAdapter(this, list,
				R.layout.mp3info_item, new String[] { "mp3_name",
						"mp3_size" }, new int[] { R.id.mp3_name,
						R.id.mp3_size });
		return simpleAdapture;
		
	}
	
	private void updateListView(){
		String xml = downloaderXML("http://121.49.87.4:8080/mp3/resources.xml");
		List<Mp3Info> mp3Infos = parse(xml);
		SimpleAdapter simpleAdapture = buildSimpleAdapter(mp3Infos);
		setListAdapter(simpleAdapture);

	}
	private List<Mp3Info> parse(String xmlStr){
		SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
		List<Mp3Info> infos = new ArrayList<Mp3Info>();
		try{
			XMLReader xmlReader = saxParserFactory.newSAXParser()
					.getXMLReader();
			Mp3ListContentHandler mp3ListContentHandler = new Mp3ListContentHandler(
					infos);
			xmlReader.setContentHandler(mp3ListContentHandler);
			xmlReader.parse(new InputSource(new StringReader(xmlStr)));
			for (Iterator iterator = infos.iterator(); iterator.hasNext();) {
				Mp3Info mp3Info = (Mp3Info) iterator.next();
				System.out.println(mp3Info);
				
			}
		}catch(Exception e){			
		}
		return infos;
	}
}

只是修改了这么一个类

增加了显示目录的功能,就是刷新成功。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:orientation="horizontal"
	android:paddingLeft="10dip"
	android:paddingRight="10dip"
	android:paddingTop="1dip"
	android:paddingBottom="1dip"
	>
	<TextView android:id="@+id/mp3_name"
	android:layout_height="30dip"
	android:layout_width="180dip"
	android:textSize="10pt"/>
	<TextView android:id="@+id/mp3_size"
	android:layout_height="30dip"
	android:layout_width="180dip"
	android:textSize="10pt"/>
</LinearLayout>


你可能感兴趣的:(android)