Themen einer ListPreference...

ui_3k1

ui_3k1

Gesperrt
197
Hallo,

ich bin so langsam am Durchdrehen. :-/
Es will mir nicht gelingen eine ListPreference (zur Auswahl von Benachrichtigungstönen) zu themen.

Habe jetzt schon die komplette Klasse "extrahiert", aber komme trotzdem nicht weiter.

Code:
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.provider.MediaStore;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.ListView;
import android.widget.RadioButton;

import java.util.ArrayList;
import java.util.List;

import static android.preference.PreferenceManager.getDefaultSharedPreferences;

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;

    ListView listView;

    String mThemeSettings = MyVars.THEME_KEY_DEFAULT;


    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);
    }


    //    private int mClickedDialogEntryIndex;
    private CharSequence mDialogTitle;


    public void loadData() {

        SharedPreferences sp = 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 {
                            if (!path.contains("NONE")) {
                                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();
        }
    }

}
der "Aufruf" erfolgt so... (alles etwas umständlich - aber ich habe sonst keine Alternative gefunden)

Code:
<de.clevercomputing.libredrive.activities.CustomListPreference
            android:summary="Wähle zwischen verschiedenen Warntönen"
            android:title="Warntöne"
            />
Beste Grüße und Danke vorab
Martin


Lösung gefunden :D :D :D

context.setTheme(R.style.Theme_Darkwing); <--- das brachte das erwünschte Ergebnis.
 
Zuletzt bearbeitet:

Ähnliche Themen

D
  • Data2006
Antworten
14
Aufrufe
483
jogimuc
J
B
Antworten
3
Aufrufe
1.305
swa00
swa00
A
Antworten
10
Aufrufe
1.017
swa00
swa00
Zurück
Oben Unten