程序是一个小播放器,当然还只完成不一部分。
先看MainActivity
- Java code
package wangxin.Activity;import java.io.File;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import wangxin.file.FileUtils;import wangxin.mode.Mp3;import android.app.Activity;import android.app.AlertDialog;import android.app.ListActivity;import android.content.Context;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ListView;import android.widget.SimpleAdapter;public class MainActivity extends Activity { public static final int NEWSONGMENU = 0; public static final int TOOLMENU = 5; public static final int ABOUTMENU = 10; private List<Mp3> mp3Files = null; public ListView listView = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listView = (ListView) findViewById(R.id.listView); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Mp3 mp3 = mp3Files.get(arg2); Intent intent = new Intent(); intent.putExtra("mp3Song", mp3); intent.setClass(MainActivity.this, PlayerActivity.class); startActivity(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub menu.add(0, NEWSONGMENU, 0, "新歌"); menu.add(1, TOOLMENU, 5, "工具"); menu.add(3, ABOUTMENU, 10, "关于"); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case NEWSONGMENU: updateSongs(); break; case TOOLMENU: // 现在不实现 break; case ABOUTMENU: getAlertDialog(MainActivity.this, "关于", "本款小软件由mojia独立完成,灰常不错哦,鼓掌!", "好嘞"); break; default: break; } return super.onOptionsItemSelected(item); } private void updateSongs() { // TODO Auto-generated method stub FileUtils fileUtils = new FileUtils(); mp3Files = fileUtils.getMp3Files("mp3"); if (mp3Files != null) { ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); for (Iterator<Mp3> i = mp3Files.iterator(); i.hasNext();) { Mp3 mp3 = i.next(); HashMap<String, String> hashMap = new HashMap<String, String>(); hashMap.put("mp3Name", mp3.getName()); list.add(hashMap); } SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, list, R.layout.songslist, new String[] { "mp3Name" }, new int[] { R.id.mp3Name }); listView.setAdapter(adapter); } else { getAlertDialog(MainActivity.this, "Sorry", "对不起现在还没有歌曲哦,亲", "OK"); } } public void getAlertDialog(Context context, String title, String message, String str_ok) { // TODO Auto-generated method stub new AlertDialog.Builder(context).setTitle(title).setMessage(message) .setPositiveButton(str_ok, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub finish(); } }).show(); }}