Android 下 ListView 的使用方法詳解
ListView 的使用比我想像中的要麻煩很多,所以有必要記錄下來(lái)。
首先在界面拖放一個(gè) ListView 控件,生成的 XML。
ListView 每個(gè)子項(xiàng)顯示需要與 Adapter 配合,這對(duì)于我來(lái)說(shuō)不太好理解。
每個(gè) ListView 子項(xiàng)需要格式說(shuō)明,其 XML 如下(文件名是: my_mp3filelist.xml, 在后繼的代碼中要使用的):
以上,ListView?的準(zhǔn)備就緒了。接下來(lái)就是準(zhǔn)備數(shù)據(jù),此示例用于顯示音樂(lè)文件列表,音樂(lè)文件的信息來(lái)自于系統(tǒng)的類(lèi):?MediaStore.Audio.Media。
將 MediaStore.Audio.Media 中程序需要的信息轉(zhuǎn)存在自定義的類(lèi)中,自定義的類(lèi)如下:
// 此類(lèi)中成員的個(gè)數(shù)根據(jù)個(gè)人需要定義,沒(méi)有必要的成員可以刪除的
public?class?Mp3Info?{ private?String?id; private?String?mp3Name; private?String?mp3Size; private?String?lrcName; private?String?lrcSize; private?String?mp3Artist; private?long?mp3Duration; private?String?mp3Url; public?Mp3Info()?{ super(); } public?Mp3Info(String?id,?String?mp3Name,?String?mp3Size,?String?lrcName, String?lrcSize,?String?mp3Artist)?{ super(); this.id?=?id; this.mp3Name?=?mp3Name; this.mp3Size?=?mp3Size; this.lrcName?=?lrcName; this.lrcSize?=?lrcSize; this.mp3Artist?=?mp3Artist; } @Override public?String?toString()?{ return?"Mp3Info?[id="?+?id?+?",?mp3Name="?+?mp3Name?+?",?mp3Size=" +?mp3Size?+?",?lrcName="?+?lrcName?+?",?lrcSize="?+?lrcSize +?",?duration="?+?mp3Duration?+?"]"; } public?String?getId()?{ return?id; } public?void?setId(String?id)?{ this.id?=?id; } public?void?setId(long?id)?{ this.id?=?Long.toString(id); } public?String?getMp3Name()?{ return?mp3Name; } public?void?setTitle(String?strName){ this.mp3Name?=?strName; } public?void?setArtist(String?strArtist){ mp3Artist?=?strArtist; } public?String?GetArtist()?{ return?this.mp3Artist; } public?void?setDuration(long?duration){ mp3Duration?=??duration; } public?void?setMp3Name(String?mp3Name)?{ this.mp3Name?=?mp3Name; } public?String?getMp3Size()?{ return?mp3Size; } public?void?setMp3Size(String?mp3Size)?{ this.mp3Size?=?mp3Size; } public?void?setSize(long?strSize)?{ this.mp3Size?=?Long.toString(strSize); } public?String?getLrcName()?{ return?lrcName; } public?void?setLrcName(String?lrcName)?{ this.lrcName?=?lrcName; } public?String?getLrcSize()?{ return?lrcSize; } public?void?setLrcSize(String?lrcSize)?{ this.lrcSize?=?lrcSize; } public?void?setUrl(String?strUrl)?{ this.mp3Url?=?strUrl; } public?String?GetUrl()?{ return?this.mp3Url; } }
音樂(lè)信息的獲取與轉(zhuǎn)存代碼如下:
/** *?用于從數(shù)據(jù)庫(kù)中查詢(xún)歌曲的信息,保存在List當(dāng)中 *?class?Mp3Info?有什么成員,?由?MediaStore.Audio.Media?和實(shí)際需要確定 */ public?static?ListgetMp3Infos(Context?context)?{ Cursor?cursor?=?context.getContentResolver().query( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,?null,?null,?null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER); Listmp3Infos?=?new?ArrayList(); for?(int?i?=?0;?i?<?cursor.getCount();?i++)?{ Mp3Info?mp3Info?=?new?Mp3Info(); cursor.moveToNext(); long?id?=?cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID)); //?音樂(lè)id String?title?=?cursor.getString((cursor.getColumnIndex(MediaStore.Audio.Media.TITLE))); //?音樂(lè)標(biāo)題 String?artist?=?cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)); //?藝術(shù)家 long?duration?=?cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION)); //?時(shí)長(zhǎng) long?size?=?cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE)); //?文件大小 String?url?=?cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)); //?文件路徑 int?isMusic?=?cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC)); //?是否為音樂(lè) ?? if?(isMusic?!=?0)?{ //?只把音樂(lè)添加到集合當(dāng)中 ?? mp3Info.setId(id); ?? mp3Info.setTitle(title); ?? mp3Info.setArtist(artist); ?? mp3Info.setDuration(duration); ?? mp3Info.setSize(size); ?? mp3Info.setUrl(url); ?? mp3Infos.add(mp3Info); ?? } ??} return?mp3Infos; }
將保存后的音樂(lè)信息顯示在 ListView 中:
(1) 定義相應(yīng)變量及初始化
private?ListView?lvMp3File; Listmp3Infos; lvMp3File?=?(ListView)findViewById(R.id.listView1);
(2) 數(shù)據(jù)顯示
//?測(cè)試音樂(lè)文件遍歷?-?需改為線程(考慮若數(shù)據(jù)量大時(shí)是否會(huì)影響?UI?顯示的時(shí)長(zhǎng)) //?生成動(dòng)態(tài)數(shù)組,并且加載數(shù)據(jù) ArrayList<HashMap>?mylist?=?new?ArrayList<HashMap>(); mp3Infos?=?getMp3Infos(getApplicationContext()); for?(Iteratoriterator?=?mp3Infos.iterator();?iterator.hasNext();)?{ Mp3Info?mp3Info?=?(Mp3Info)?iterator.next();?? Log.v("Leo?-?MP3?Name:?",?mp3Info.getMp3Name()); HashMapmap?=?new?HashMap(); ???? map.put("ItemTitle",?mp3Info.getMp3Name()); ???? map.put("ItemText",?mp3Info.GetArtist()); ???? //?map.put("url",?mp3Info.GetUrl()); ???? mylist.add(map); ???? ????//?生成?Apapter(適配器),?數(shù)組?->?ListItem ????SimpleAdapter?mSchedule?=?new?SimpleAdapter(this, ???? //?數(shù)據(jù)來(lái)源 ???? mylist, ???? //?ListItem?的?XML?實(shí)現(xiàn) ???? R.layout.my_mp3filelist, ???? //?動(dòng)態(tài)數(shù)組與?ListItem?對(duì)應(yīng)的子項(xiàng) ???? new?String[]?{"ItemTitle",?"ItemText"}, ???? //?ListItem?的?XML?文件里面的兩個(gè)?TextView?ID ???? new?int[]?{R.id.ItemTitle,R.id.ItemText}); lvMp3File.setAdapter(mSchedule); }
此段代碼在示例中,是放在主窗體的 protected void onCreate(Bundle savedInstanceState) 過(guò)程中。
ListView 還需要響應(yīng)用戶(hù)的點(diǎn)擊操作,需要如下的聲明:
lvMp3File.setOnItemClickListener(new?MusicListItemClickListener());
MusicListItemClickListener 的實(shí)現(xiàn)如下:
private?class?MusicListItemClickListener?implements?OnItemClickListener { ????@Override ????public?void?onItemClick(AdapterView?parent,?View?view,?int?position,long?id) ????{ ????????if(mp3Infos?!=?null) ????????{ ???????? //?Leo?界面切換:?切換到播放界面 ???????? //?Intent?goPlaying?=?new?Intent(MainActivity.this,?PlayingActivity.class); ???????? //?startActivity(goPlaying); ????????????Intent?goPlayingIntent?=?new?Intent(); ????????????goPlayingIntent.setClass(MainActivity.this,?PlayingActivity.class); ????????????startActivity(goPlayingIntent); ???????????? ????????????//?啟動(dòng)音樂(lè)后臺(tái)播放服務(wù) ????????????/*?若不做延時(shí)直接調(diào)用?run?中的代碼,歌曲信息無(wú)法正確獲取到?*/ ????????????positionSelected?=?position; ????????????Timer?timer?=?new?Timer(); ????????????TimerTask?task?=?new?TimerTask(){ ????????????????public?void?run(){ ???? ????????????Mp3Info?mp3Info?=?mp3Infos.get(positionSelected); ???? ????????????Log.d("mp3Info-->",?mp3Info.toString()); ???? ????????????Intent?intent?=?new?Intent(); ???? ????????????intent.putExtra("url",?mp3Info.GetUrl()); ???? ????????????intent.putExtra("listPosition",?positionSelected/*getPlayingIndex(mp3Info.GetUrl())*/); ???? ????????????intent.putExtra("MSG",?AppConstant.PlayerMsg.PLAY_MSG.ordinal()); ???? ????????????intent.setClass(MainActivity.this,?PlayerService.class); ???? ????????????startService(intent);???????//?啟動(dòng)服務(wù) ?????????????????} ????????????}; ????????????timer.schedule(task,?300);?//?0.3?秒后執(zhí)行?run?中的代碼段 ????} }