編輯:關於 Android 程式設計
本篇文章要解決的核心問題是:如何把儲存裝置裡的檔案掃描出來,並逐項載入到清單元件(item)中呈現。
這套做法並不限於音樂播放器,任何需要讀取外部儲存空間檔案列表的 Android 專案都能直接套用。這裡我先寫了一個體積很小的 Demo 來示範,只有一個 Java 檔與兩個 XML 檔,非常輕量。
後續的文章會把這個功能正式整合進播放器,讓剛下載完成的歌曲能夠自動被掃描並載入播放清單。
本次 Demo 的源碼可於以下專案頁下載,名稱為 SdcardSearchToList(若暫時看不到,可能仍在審核中):
https://download.csdn.net/album/detail/3105
截至目前為止的完整源碼同樣在上面這個連結(請下載最後一個版本,本文對應版本 2.1;若想查看逐步實作的過程,則可下載全部版本)。即使不下載也無妨,因為檔案數量極少,跟著下面的程式碼走就能理解。
實現效果如下圖所示:


實現代碼如下:
GetListActivity(主 Activity):
package com.iwanghang.sdcardsearchtolist; import android.os.Bundle; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.SimpleAdapter; /** * 不要忘記加權限 **/ public class GetListActivity extends Activity { private Button myBtn = null; private ListView lv = null; private SimpleAdapter sa = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_get_list); lv = (ListView)findViewById(R.id.listv); String[] from = {"text_content"}; int[] to = {R.id.myView1}; sa = new SimpleAdapter(this, getList(), R.layout.itemlist, from, to); myBtn = (Button)findViewById(R.id.myButton); myBtn.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { // TODO Auto-generated method stub lv.setAdapter(sa); } }); } public List
這裡的 getList() 負責掃描本地檔案並取得檔名,接著透過 SimpleAdapter 把掃描結果呈現在 ListView 上。要特別留意的是,SimpleAdapter 的最後兩個參數(from 與 to)都必須是陣列。
activity_get_list.xml 如下:
<linearlayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.iwanghang.sdcardsearchtolist.GetListActivity" android:orientation="vertical"> <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="獲取本地文件信息"><button android:id="@+id/myButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="掃描"> <listview android:id="@+id/listv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scrollbars="vertical"> </listview></button></textview></linearlayout>
itemlist.xml 如下:
<tablelayout xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/lvitem" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:stretchcolumns="*"> <tablerow> <textview android:id="@+id/myView1" android:layout_width="wrap_content" android:layout_height="wrap_content"> </textview> <textview android:id="@+id/myView2" android:layout_width="wrap_content" android:layout_height="wrap_content"> </textview> <textview android:id="@+id/myView3" android:layout_width="wrap_content" android:layout_height="wrap_content"> </textview> </tablerow> </tablelayout>
別忘了在清單檔中加入讀取外部儲存空間的權限:<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE">,否則掃描會失敗。
Android 自定義控制項入門:Canvas 與 Paint 繪製基礎圖形詳解
前言 上一篇寫自定義 View 已經是大半年前的事了,其間一直想接著寫,但總覺得自己火候不夠,怕誤人子弟。這段時間專案剛收尾,終於有點空檔,於是跟著前輩們的思路,把自定義
Android 之重寫 View 與 LinearLayout(實現背景氣泡與波紋效果)
前兩天看了仿 Android L 裡面水波紋效果的兩篇博客:一篇實現了一個水波紋佈局,放在裡面的所有控件點擊後都會出現波紋效果;另一篇實現了一個水波紋 view,點擊之後
自訂 ViewGroup 實作淘寶風格的商品尺寸選擇按鈕組
先看效果: 一、原理 1、本質上這是一個自訂 ViewGroup(命名為 GoodsViewGroup),把若干子 View 加進來,再自己決定它們的排列位置,就能做出
Android RecyclerView 模仿探探左右滑動卡片
這類卡片式左右滑動效果,常用於相簿、履歷、推薦卡片等場景。以 RecyclerView 實作時,可以分成兩個關鍵部分:自定義 LayoutManager 負責卡片堆疊,