好吧,在花了半个晚上让它工作之后,我得到了一个合适的解决方案。我接受了 Trinitrotoluol 在这里写的建议,并将其与我自己的功能合并。这是我的 CustomListPreference (完整,没有缺少步骤)...
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.preference.ListPreference;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.RadioButton;
import java.util.ArrayList;
import java.util.List;
public class CustomListPreference extends ListPreference {
/* Konstante */
private final String TAG = ((Object) this).getClass().getSimpleName();
Context mContext;
LayoutInflater mInflater;
ArrayList<RadioButton> mButtonList;
private MediaPlayer mMediaPlayer;
CharSequence[] mEntries;
CharSequence[] mEntryValues;
private int mActivePositionInList;
private int mTmpActivePositionInList;
private String mValue;
private String mPath;
private String mTmpPath;
public CustomListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
mInflater = LayoutInflater.from(context);
mButtonList = new ArrayList<RadioButton>();
Log.d(TAG, "Konstruktor");
// Manager für Benachrichtigungstöne
RingtoneManager ringtoneManager = new RingtoneManager(context);
ringtoneManager.setType(RingtoneManager.TYPE_NOTIFICATION);
final Cursor ringtones = ringtoneManager.getCursor();
List<String> entries = new ArrayList<String>();
List<String> entryValues = new ArrayList<String>();
loadData();
// keine Töne (disabled notification / "warnings")
entries.add("Keine");
entryValues.add("NONE");
for (ringtones.moveToFirst(); !ringtones.isAfterLast(); ringtones.moveToNext()) {
// Anzeige (displays data to screen)
entries.add(ringtones.getString(RingtoneManager.TITLE_COLUMN_INDEX));
// ID beziehen (getting the ID to add it at the end of the path)
int id = ringtones.getInt(ringtones.getColumnIndex(MediaStore.MediaColumns._ID));
// ID ans Ende anfügen (attach ID to the end of the path)
entryValues.add(ringtones.getString(RingtoneManager.URI_COLUMN_INDEX) + "/" + id);
Log.d(TAG, "Tone: " + ringtones.getString(RingtoneManager.URI_COLUMN_INDEX) + "/" + id);
}
setEntryValues(entryValues.toArray(new CharSequence[entryValues.size()]));
setEntries(entries.toArray(new CharSequence[entries.size()]));
}
public CustomListPreference(Context context) {
super(context);
}
public void loadData() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor edit = sp.edit();
if (sp.contains("audio_path")) {
mTmpPath = mPath = sp.getString("audio_path", "NONE");
} else {
edit.putString("audio_path", mPath);
edit.commit();
}
if (sp.contains("audio_id")) {
mTmpActivePositionInList = mActivePositionInList = sp.getInt("audio_id", 0);
} else {
edit.putInt("audio_id", 0);
edit.commit();
}
}
public void setValue(String audioPath, int cursorPosition) {
SharedPreferences sp = getPreferenceManager()
.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor edit = sp.edit();
edit.putString("audio_path", audioPath);
edit.putInt("audio_id", cursorPosition);
edit.commit();
}
public void setOldValues() {
SharedPreferences sp = getPreferenceManager()
.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor edit = sp.edit();
edit.putString("audio_path", mTmpPath);
edit.putInt("audio_id", mTmpActivePositionInList);
edit.commit();
}
public void setValueIndex(int index) {
if (mEntryValues != null) {
setValue(mEntryValues[index].toString());
}
}
public String getValue() {
return mValue;
}
public CharSequence getEntry() {
int index = getValueIndex();
return index >= 0 && mEntries != null ? mEntries[index] : null;
}
public int findIndexOfValue(String value) {
if (value != null && mEntryValues != null) {
for (int i = mEntryValues.length - 1; i >= 0; i--) {
if (mEntryValues[i].equals(value)) {
return i;
}
}
}
return -1;
}
private int getValueIndex() {
return findIndexOfValue(mValue);
}
@Override
protected void onPrepareDialogBuilder(Builder builder) {
super.onPrepareDialogBuilder(builder);
mMediaPlayer = new MediaPlayer();
mEntries = getEntries();
mEntryValues = getEntryValues();
if (mEntries == null || mEntryValues == null) {
throw new IllegalStateException(
"ListPreference requires an entries array and an entryValues array.");
}
builder.setSingleChoiceItems(mEntries, mActivePositionInList,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mActivePositionInList = which;
String path = mEntryValues[which].toString();
try {
RingtoneManager.getRingtone(getContext(), Uri.parse(path)).play();
mPath = path;
} catch (Exception e) {
Log.e(TAG, "Fehler beim Abspielen (Error) : " + e);
}
}
});
builder.setPositiveButton("OK", this);
builder.setNegativeButton("Abbrechen", this);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult) {
Log.d(TAG, "pos");
setValue(mPath, mActivePositionInList);
}
if (!positiveResult) {
Log.d(TAG, "neg");
mPath = mTmpPath;
mActivePositionInList = mTmpActivePositionInList;
setOldValues();
}
}
}
要将其显示在屏幕前面,您可以将此代码段添加到您的 prefs.xml
<de.yourpackage.right.activities.CustomListPreference
android:dialogTitle="Some text"
android:summary="some text too"
android:title="and more text"
/>
最后我很高兴现在让它工作..
PS:如有疑问,欢迎提问。