Java tutorial
/* * Copyright (c) 2016 <kaku201313@163.com | 3772304@qq.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package com.kaku.weac.fragment; import android.app.Activity; import android.content.SharedPreferences; import android.database.Cursor; import android.os.Bundle; import android.provider.MediaStore; import android.support.v4.app.LoaderManager; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ListView; import com.kaku.weac.R; import com.kaku.weac.adapter.RingSelectAdapter; import com.kaku.weac.bean.RingSelectItem; import com.kaku.weac.common.WeacConstants; import com.kaku.weac.util.AudioPlayer; import com.kaku.weac.util.MyUtil; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; /** * ?Fragment * * @author * @version 1.0 2015/05 */ public class LocalMusicFragment extends BaseListFragment implements LoaderManager.LoaderCallbacks<Cursor> { /** * ??Adapter */ RingSelectAdapter mLocalMusicAdapter; /** * loader Id */ private static final int LOADER_ID = 1; /** * ? */ private int mPosition = 0; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fm_ring_local_music, container, false); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); // ?cursor LoaderManager loaderManager = getLoaderManager(); // Loader loaderManager.initLoader(LOADER_ID, null, this); } @Override public void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); Map<String, String> map = mLocalMusicAdapter.getItem(position); // ??? String ringName = map.get(WeacConstants.RING_NAME); // ?? String ringUrl = map.get(WeacConstants.RING_URL); // ?? mLocalMusicAdapter.updateSelection(ringName); // ? mLocalMusicAdapter.notifyDataSetChanged(); // ????? RingSelectItem.getInstance().setRingPager(1); // AudioPlayer.getInstance(getActivity()).play(ringUrl, false, false); ViewPager pager = (ViewPager) getActivity().findViewById(R.id.fragment_ring_select_sort); PagerAdapter f = pager.getAdapter(); SystemRingFragment systemRingFragment = (SystemRingFragment) f.instantiateItem(pager, 0); RecorderFragment recorderFragment = (RecorderFragment) f.instantiateItem(pager, 2); // ? if (systemRingFragment.mSystemRingAdapter != null) { systemRingFragment.mSystemRingAdapter.updateSelection(""); systemRingFragment.mSystemRingAdapter.notifyDataSetChanged(); } // ? if (recorderFragment.mRecorderAdapter != null) { recorderFragment.mRecorderAdapter.updateSelection(""); recorderFragment.mRecorderAdapter.notifyDataSetChanged(); } } @Override public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) { // return new CursorLoader(getActivity(), MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DATA }, null, null, MediaStore.Audio.Media.DISPLAY_NAME); } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { switch (loader.getId()) { case LOADER_ID: // ?????? String ringName1; if (RingSelectFragment.sRingName != null) { ringName1 = RingSelectFragment.sRingName; } else { SharedPreferences share = getActivity().getSharedPreferences(WeacConstants.EXTRA_WEAC_SHARE, Activity.MODE_PRIVATE); // ?????? ringName1 = share.getString(WeacConstants.RING_NAME, ""); } // ??List List<Map<String, String>> list = new ArrayList<>(); // ??Set HashSet<String> set = new HashSet<>(); if (cursor != null) { for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { // ?? String ringName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)); // ?? String ringUrl = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)); if (ringName != null) { // ?????[.amr]? if (!set.contains(ringName) && !ringUrl.contains("/WeaAlarmClock/audio/record") && !ringName.equals("record_start.mp3") && !ringName.equals("record_stop.mp3") && !ringName.equals("ring_weac_alarm_clock_default.mp3")) { // ??? set.add(ringName); // ?? ringName = MyUtil.removeEx(ringName); Map<String, String> map = new HashMap<>(); map.put(WeacConstants.RING_NAME, ringName); map.put(WeacConstants.RING_URL, ringUrl); list.add(map); // ???? if (ringName.equals(ringName1)) { mPosition = list.size() - 1; RingSelectItem.getInstance().setRingPager(1); } } } } } mLocalMusicAdapter = new RingSelectAdapter(getActivity(), list, ringName1); setListAdapter(mLocalMusicAdapter); setSelection(mPosition); break; } } @Override public void onLoaderReset(Loader<Cursor> arg0) { } }