From c701c4d58ea3759e5a974881c99b6e38edd0949c Mon Sep 17 00:00:00 2001 From: Ishan09811 <156402647+Ishan09811@users.noreply.github.com> Date: Fri, 26 Jan 2024 18:28:50 +0530 Subject: [PATCH] SingleSelectionPreferences : test --- .../SingleSelectionPreferences.java | 43 ++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/pandroid/app/src/main/java/com/panda3ds/pandroid/view/preferences/SingleSelectionPreferences.java b/src/pandroid/app/src/main/java/com/panda3ds/pandroid/view/preferences/SingleSelectionPreferences.java index a4cf70f9f..bef4b886d 100644 --- a/src/pandroid/app/src/main/java/com/panda3ds/pandroid/view/preferences/SingleSelectionPreferences.java +++ b/src/pandroid/app/src/main/java/com/panda3ds/pandroid/view/preferences/SingleSelectionPreferences.java @@ -24,12 +24,18 @@ public class SingleSelectionPreferences extends PreferenceCategory implements Pr private final Drawable transparent = new ColorDrawable(Color.TRANSPARENT); private final Drawable doneDrawable = ContextCompat.getDrawable(getContext(), R.drawable.ic_done); + // New attributes for handling entries and entryValues + private CharSequence[] entries; + private CharSequence[] entryValues; + public SingleSelectionPreferences(@NonNull Context context) { super(context); + init(); } public SingleSelectionPreferences(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); + init(); } public SingleSelectionPreferences(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { @@ -40,11 +46,13 @@ public SingleSelectionPreferences(@NonNull Context context, @Nullable AttributeS super(context, attrs, defStyleAttr, defStyleRes); } - { + private void init() { try { TypedArray color = getContext().obtainStyledAttributes(new int[]{ android.R.attr.textColorSecondary }); + entries = getEntries(); + entryValues = getEntryValues(); doneDrawable.setTint(color.getColor(0, Color.RED)); color.recycle(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { @@ -55,6 +63,19 @@ public SingleSelectionPreferences(@NonNull Context context, @Nullable AttributeS } } + @Override + public void onBindViewHolder(@NonNull PreferenceViewHolder holder) { + super.onBindViewHolder(holder); + + // Set summary based on selected entry + if (entries != null && entryValues != null) { + CharSequence selectedEntry = getEntry(); + if (selectedEntry != null) { + holder.setSummary(selectedEntry); + } + } + } + @Override public void onAttached() { super.onAttached(); @@ -96,7 +117,7 @@ private void showMaterialDialog(final int selectedIndex) { new MaterialAlertDialogBuilder(getContext()) .setTitle(getTitle()) - .setSingleChoiceItems(entriesArray, selectedIndex, (dialog, which) -> { + .setSingleChoiceItems(entries, selectedIndex, (dialog, which) -> { updateSelectedPreference(which); dialog.dismiss(); }) @@ -115,4 +136,22 @@ private void updateSelectedPreference(int selectedIndex) { callChangeListener(selectedIndex); } + + // New method to get entries from the XML attribute + private CharSequence[] getEntries() { + if (this instanceof ListPreference) { + return ((ListPreference) this).getEntries(); + } else { + return null; // Handle appropriately for other types of preferences + } + } + + // New method to get entryValues from the XML attribute + private CharSequence[] getEntryValues() { + if (this instanceof ListPreference) { + return ((ListPreference) this).getEntryValues(); + } else { + return null; // Handle appropriately for other types of preferences + } + } }