mp3播放器(二)(adapter模式将显示在屏幕上)

要在ListView中显示数据,首先在布局文件中要有一个ListView
ListView里面每一个Item的展示需要另外一个布局文件控制里面的内容,每一个选项的内容就靠它来控制
往List里面添加数据主要就是用的simpleAdapter这个对象
for迭代语句就是把List里面的东西弄出来

Mp3ListActivity.java

  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     }
 40 
 41     @Override
 42     public boolean onCreateOptionsMenu(Menu menu) {
 43         // Inflate the menu; this adds items to the action bar if it is present.
 44         getMenuInflater().inflate(R.menu.activity_mp3_list, menu);
 45         menu.add(0, 1, 1,R.string.mp3list_update);
 46         menu.add(0, 2, 2, R.string.mp3list_about);
 47         return true;
 48     }
 49 
 50     @Override
 51     public boolean onOptionsItemSelected(MenuItem item) {
 52         // TODO Auto-generated method stub
 53         //System.out.println("--->"+item.getItemId());
 54         if(item.getItemId()==upadate)
 55         {
 56             //用户点击了更新按钮
 57     
 58             String xml=downloadXML("http://192.168.1.100:8080/mp3/resources.xml");
 59             //System.out.println("----->"+xml);
 60             try {
 61                 List<Mp3Info> mp3infos=parse(xml);//List<Mp3Info>可以看出一个int,String这样一个类型
 62                 //构建Adapter,首先生成一个List对象。List对象里面存的是HashMap,HashMap里面存的是String
 63             
 64                 List<HashMap<String,String>> list=new ArrayList<HashMap<String,String>>();
 65                //将Mp3Info迭代出来
 66                 for (Iterator iterator = mp3infos.iterator(); iterator.hasNext();) {
 67                     Mp3Info mp3Info = (Mp3Info) iterator.next();
 68                      //生成一个HashMap对象
 69                     HashMap<String,String> map=new HashMap<String,String>();
 70                     //构造一个map
 71                     map.put("mp3_name", mp3Info.getMp3Name());
 72                     map.put("mp3_size", mp3Info.getMp3Size());
 73                     //构造好map后添加到List中,即将HashMap添加到List中
 74                     list.add(map);
 75                 }
 76                 //生成adapter
 77                 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}); 78 setListAdapter(simpleAdapter); 79 80 } catch (IOException e) { 81 // TODO Auto-generated catch block 82 e.printStackTrace(); 83 } 84 } 85 else if(item.getItemId()==about) 86 { 87 //用户点击了关于按钮 88 } 89 return super.onOptionsItemSelected(item); 90 } 91 //写一个单独的下载函数,更加清晰 92 private String downloadXML(String urlStr) 93 { 94 95 HttpDownloader httpdownloader=new HttpDownloader(); 96 String result=httpdownloader.download(urlStr); 97 return result; 98 } 99 //在这个函数中解析xml 100 //首先生成xml解析器 101 private List<Mp3Info> parse(String xmlStr) throws IOException 102 { 103 List<Mp3Info> info=new ArrayList<Mp3Info>(); 104 SAXParserFactory saxParserFactory=SAXParserFactory.newInstance(); 105 try { 106 XMLReader xmlReader=saxParserFactory.newSAXParser().getXMLReader(); 107 Mp3ListContentHandler mp3ListContentHandler=new Mp3ListContentHandler(info); 108 xmlReader.setContentHandler(mp3ListContentHandler); 109 xmlReader.parse(new InputSource(new StringReader(xmlStr))); 110 //解析完数据都天道info里面,然后用for循环迭代出来 111 for (Iterator iterator = info.iterator(); iterator.hasNext();) { 112 Mp3Info mp3Info = (Mp3Info) iterator.next(); 113 System.out.println(mp3Info); 114 } 115 116 117 } catch (SAXException e) { 118 // TODO Auto-generated catch block 119 e.printStackTrace(); 120 } catch (ParserConfigurationException e) { 121 // TODO Auto-generated catch block 122 e.printStackTrace(); 123 } 124 return info; 125 126 } 127 128 }


mp3info_item.xml

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:orientation="horizontal"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:paddingLeft="10dip"
 6     android:paddingRight="10dip"
 7     android:paddingTop="1dip"
 8     android:paddingBottom="1dip"
 9     >
10     <TextView android:id="@+id/mp3_name"
11         android:layout_width="180dip"
12         android:layout_height="30dip"
13         android:textSize="10pt"/>
14     <TextView android:id="@+id/mp3_size"
15         android:layout_width="180dip"
16         android:layout_height="30dip"
17         android:textSize="10pt"/>
18 </LinearLayout>

 

posted @ 2010-10-11 22:36  Kstyjobx!!  阅读(329)  评论(0编辑  收藏  举报