Mp3Player(3) LocalMp3ListActivity、ListActivity、SimpleAdapter
-
//LocalMp3ListActivity.java
-
package com.lwb.mp3player;
-
-
import java.util.ArrayList;
-
import java.util.HashMap;
-
import java.util.Iterator;
-
import java.util.List;
-
-
import com.lwb.model.Mp3Info;
-
import com.lwb.utils.FileUtils;
-
-
import android.app.ListActivity;
-
import android.content.Intent;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.widget.ListView;
-
import android.widget.SimpleAdapter;
-
-
/*LocalMp3ListActivity是ListActivity列表型的Activity
-
* 用于显示本地歌曲列表,点击某首歌曲后进入播放界面
-
* 重点有
-
* 1、在OnResume()方法中添加了显示本地歌曲的代码
-
* 2、点击某个歌曲后,进入播放界面onListItemClick(ListView l, View v, int position, long id)
-
* */
-
public class LocalMp3ListActivity extends ListActivity{
-
private List<Mp3Info> mp3Infos = null;
-
-
/*onResume()方法中添加了显示本地歌曲的代码
-
* 1、创建一FileUtils对象
-
* 2、获取外部存储器mp3/目录下歌曲信息返回到mp3Infos中,mp3Infos = fileUtils.getMp3Files("mp3/");
-
* 这里以后添加为可选择路径会更棒
-
* 3、把mp3Infos中的所有mp3info放大List>表中
-
* 4、设置SimpleAdater内容:SimpleAdapter simpleAdater = new SimpleAdapter(this,list,R.layout.mp3info_item,new String[]{"mp3_name","mp3_size"},
-
new int []{R.id.mp3_name,R.id.mp3_size});
-
* 5、设置Adapter:setlistAdapter(simpleAdater);
-
* */
-
@Override
-
protected void onResume() {
-
FileUtils fileUtils = new FileUtils();
-
mp3Infos = fileUtils.getMp3Files("mp3/");
-
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);
-
}
-
//注意new String[]{"mp3_name","mp3_size"} 而不是new String[]{"mp3_name,mp3_size"},这里只是一个字符串而且
-
SimpleAdapter simpleAdater = 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(simpleAdater);
-
super.onResume();
-
}
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.local_mp3_list);
-
}
-
-
/*OnListItemClick(ListView l,View v,int position,long id)
-
*当点击列表中的某一歌曲时进行播放,播放歌曲是在另一Activity界面中控制进行
-
* 其中的position就是被点击歌曲在ListActivity中的序号
-
* 重点是 由Activity启动Activity的方法是:
-
* 1、生成一个Intent Intent Intent = new Intent()
-
* 2、把要传递给Activity的信息通过 Intent.putExtra("mp3Info",mp3Info)进行传递
-
* 3、设置要启动的Activity intent.setClass(this,PlayerActivity.class);
-
* 4、启动Activity:startActivity(intent)
-
* */
-
@Override
-
protected void onListItemClick(ListView l, View v, int position, long id) {
-
if(mp3Infos!=null){
-
Mp3Info mp3Info = mp3Infos.get(position);
-
Intent intent = new Intent();
-
intent.putExtra("mp3Info", mp3Info);
-
intent.setClass(this,PlayerActivity.class);
-
startActivity(intent);
-
}
-
super.onListItemClick(l, v, position, id);
-
}
-
}
布局 local_mp3_list.xml:
-
<LinearLayout xmlns:android=""
-
android:orientation="vertical"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent" >
-
<LinearLayout
-
android:id="@+id/listLinearLayout"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:orientation="vertical">
-
<ListView
-
android:id="@id/android:list"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:drawSelectorOnTop="true"
-
android:scrollbars="vertical"/>
-
</LinearLayout>
-
</LinearLayout>
布局 mp3info_item.xml:
-
<LinearLayout xmlns: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>
阅读(421) | 评论(0) | 转发(0) |