Chinaunix首页 | 论坛 | 博客
  • 博客访问: 205362
  • 博文数量: 102
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1015
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-05 16:45
文章存档

2014年(73)

2013年(29)

我的朋友

分类: Android平台

2014-04-06 16:26:29

Mp3Player(3) LocalMp3ListActivity、ListActivity、SimpleAdapter

点击(此处)折叠或打开

  1. //LocalMp3ListActivity.java
  2. package com.lwb.mp3player;

  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Iterator;
  6. import java.util.List;

  7. import com.lwb.model.Mp3Info;
  8. import com.lwb.utils.FileUtils;

  9. import android.app.ListActivity;
  10. import android.content.Intent;
  11. import android.os.Bundle;
  12. import android.view.View;
  13. import android.widget.ListView;
  14. import android.widget.SimpleAdapter;

  15. /*LocalMp3ListActivity是ListActivity列表型的Activity
  16.  * 用于显示本地歌曲列表,点击某首歌曲后进入播放界面
  17.  * 重点有
  18.  * 1、在OnResume()方法中添加了显示本地歌曲的代码
  19.  * 2、点击某个歌曲后,进入播放界面onListItemClick(ListView l, View v, int position, long id)
  20.  * */
  21. public class LocalMp3ListActivity extends ListActivity{
  22.     private List<Mp3Info> mp3Infos = null;    
  23.     
  24.     /*onResume()方法中添加了显示本地歌曲的代码
  25.      * 1、创建一FileUtils对象
  26.      * 2、获取外部存储器mp3/目录下歌曲信息返回到mp3Infos中,mp3Infos = fileUtils.getMp3Files("mp3/");
  27.      * 这里以后添加为可选择路径会更棒
  28.      * 3、把mp3Infos中的所有mp3info放大List>表中
  29.      * 4、设置SimpleAdater内容:SimpleAdapter simpleAdater = new SimpleAdapter(this,list,R.layout.mp3info_item,new String[]{"mp3_name","mp3_size"},
  30.                 new int []{R.id.mp3_name,R.id.mp3_size});
  31.      * 5、设置Adapter:setlistAdapter(simpleAdater);
  32.      * */
  33.     @Override
  34.     protected void onResume() {    
  35.         FileUtils fileUtils = new FileUtils();
  36.      mp3Infos = fileUtils.getMp3Files("mp3/");    
  37.         List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
  38.         for(Iterator iterator = mp3Infos.iterator();iterator.hasNext();){
  39.             Mp3Info mp3Info = (Mp3Info)iterator.next();
  40.             HashMap<String,String> map = new HashMap<String,String>();
  41.             map.put("mp3_name", mp3Info.getMp3Name());
  42.             map.put("mp3_size", mp3Info.getMp3Size());
  43.             list.add(map);
  44.         }    
  45.         //注意new String[]{"mp3_name","mp3_size"} 而不是new String[]{"mp3_name,mp3_size"},这里只是一个字符串而且
  46.         SimpleAdapter simpleAdater = new SimpleAdapter(this,list,R.layout.mp3info_item,new String[]{"mp3_name","mp3_size"},
  47.                 new int []{R.id.mp3_name,R.id.mp3_size});
  48.         setListAdapter(simpleAdater);
  49.         super.onResume();
  50.         }

  51.     @Override
  52.     protected void onCreate(Bundle savedInstanceState) {
  53.         super.onCreate(savedInstanceState);
  54.         setContentView(R.layout.local_mp3_list);
  55.     }
  56.     
  57.     /*OnListItemClick(ListView l,View v,int position,long id)
  58.      *当点击列表中的某一歌曲时进行播放,播放歌曲是在另一Activity界面中控制进行
  59.      * 其中的position就是被点击歌曲在ListActivity中的序号
  60.      * 重点是 由Activity启动Activity的方法是:
  61.      * 1、生成一个Intent Intent Intent = new Intent()
  62.      * 2、把要传递给Activity的信息通过 Intent.putExtra("mp3Info",mp3Info)进行传递
  63.      * 3、设置要启动的Activity intent.setClass(this,PlayerActivity.class);
  64.      * 4、启动Activity:startActivity(intent)
  65.      * */
  66.     @Override
  67.     protected void onListItemClick(ListView l, View v, int position, long id) {        
  68.         if(mp3Infos!=null){
  69.             Mp3Info mp3Info = mp3Infos.get(position);
  70.             Intent intent = new Intent();
  71.             intent.putExtra("mp3Info", mp3Info);
  72.             intent.setClass(this,PlayerActivity.class);
  73.             startActivity(intent);
  74.         }
  75.         super.onListItemClick(l, v, position, id);
  76.     }
  77. }
布局 local_mp3_list.xml:

点击(此处)折叠或打开

  1. <LinearLayout xmlns:android=""
  2.     android:orientation="vertical"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent" >
  5.     <LinearLayout
  6.         android:id="@+id/listLinearLayout"
  7.         android:layout_width="fill_parent"
  8.         android:layout_height="wrap_content"
  9.         android:orientation="vertical">
  10.         <ListView
  11.             android:id="@id/android:list"
  12.             android:layout_width="fill_parent"
  13.             android:layout_height="wrap_content"
  14.             android:drawSelectorOnTop="true"
  15.             android:scrollbars="vertical"/>
  16.         </LinearLayout>
  17.   </LinearLayout>
布局  mp3info_item.xml:

点击(此处)折叠或打开

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

阅读(411) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~