Androidアプリ開発のUIやオーディオ関連の開発メモです。 調べたことを書いてきます。
public class MainActivity extends ActionBarActivity implements AdapterView.OnItemClickListener { private List<Map<String, String>> mSongDataList = new ArrayList<Map<String, String>>(); private Handler mHandler; private SongViewThread mSongViewThread; private ListView mSongListView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mHandler = new Handler(); mSongDataList.clear(); mSongListView = (ListView) findViewById(R.id.listView); mSongListView.setOnItemClickListener(this); mSongViewThread = new SongViewThread(); mSongViewThread.start(); }
@Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Map<String, String> temp = mSongDataList.get(i); Log.d(TAG, temp.get("title")); Log.d(TAG, temp.get("data")); Log.d(TAG, temp.get("duration")); }
// オーディオ情報を取得し、リストビューに表示させるスレッド private class SongViewThread extends Thread { ContentResolver mContentResolver; Cursor mCursor;
//取得したい情報をここに String[] columns = new String[]{MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.DURATION}; String tempSong, tempSongPath, tempDuration; public void run() { mContentResolver = getContentResolver(); mCursor = mContentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, columns, null, null, null); while (mCursor.moveToNext()) { tempSong = mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.TITLE)); tempSongPath = mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.DATA)); tempDuration = mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.DURATION)); Map<String, String> tempMap = new HashMap<String, String>(); tempMap.put("title", tempSong); tempMap.put("data", tempSongPath); tempMap.put("duration", tempDuration); mSongDataList.add(tempMap); } mHandler.post(new Runnable() { // run()の中の処理はメインスレッドで動作 public void run() { SimpleAdapter adapter; adapter = new SimpleAdapter(getApplicationContext(), mSongDataList, R.layout.custom_listview, new String[]{"title"}, new int[]{ R.id.song_name}) { }; mSongListView.setAdapter(adapter); } }); } } }
プロフィール
最新記事
P R
フリーエリア