Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9199937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:39:11+00:00 2026-06-17T22:39:11+00:00

I have the following problem. I created a custom list preference for displaying a

  • 0

I have the following problem.
I created a custom list preference for displaying a textview with an additional image.
Nearly everything works fine except of the following problem:

The original list preference scrolls to the position which is currently selected, but my custom list preference does not show this behaviour. If I replace the custom list preference with the android version (or in my case the holoeverywhere version), everything works fine.
Therefore, I think that the bug must be in my current implementation of the custom list preference.

public class CustomListPreference extends ListPreference {
private CustomListPreferenceAdapter customListPreferenceAdapter = null;
private Context mContext;
private LayoutInflater mInflater;
private CharSequence[] entries;
private CharSequence[] entryValues;
private Map<String, Boolean> rButtonMapping = null;
private SharedPreferences prefs;
private SharedPreferences.Editor editor;

public CustomListPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
    mInflater = LayoutInflater.from(context);
    rButtonMapping = new HashMap<String, Boolean>();
    prefs = PreferenceManager.getDefaultSharedPreferences(mContext);

    // init the button mapping with the default values
    for (int i = 0; i < Data.getData().length; i++) {
        rButtonMapping.put(Data.getData()[i], false);
    }

    // Set the current selected value to true
    String currentSelectedValue = prefs.getString(
            SettingsActivity.CUST_PREF_KEY, "TEST1");
    rButtonMapping.put(currentSelectedValue, true);

    editor = prefs.edit();
}

@Override
protected void onPrepareDialogBuilder(Builder builder) {
    entries = getEntries();
    entryValues = getEntryValues();

    if (entries == null || entryValues == null
            || entries.length != entryValues.length) {
        throw new IllegalStateException(
                "ListPreference requires an entries array and an entryValues array which are both the same length");
    }

    customListPreferenceAdapter = new CustomListPreferenceAdapter();

    builder.setAdapter(customListPreferenceAdapter,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                }
            });
}

private class CustomListPreferenceAdapter extends BaseAdapter {

    public int getCount() {
        return entries.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView,
            ViewGroup parent) {
        View row = convertView;
        if (row == null) {
            row = mInflater.inflate(R.layout.custom_list_preference_row,
                    parent, false);
        }

        TextView text = (TextView) row
                .findViewById(R.id.settings_tv_test);
        text.setText(entries[position]);

        try {
            InputStream ims = mContext.getAssets().open(
                    "pics/" + entryValues[position] + ".png");
            BitmapDrawable d = new BitmapDrawable(mContext.getResources(),
                    ims);
            d.setTargetDensity(mContext.getResources().getDisplayMetrics().densityDpi);

            text.setCompoundDrawablesWithIntrinsicBounds(d, null, null,
                    null);
        } catch (IOException e) {
            text.setCompoundDrawablesWithIntrinsicBounds(null, null, null,
                    null);
        }

        RadioButton rButton = (RadioButton) row
                .findViewById(R.id.settings_rb);
        // If the current position is checked we also check the current
        // value
        rButton.setChecked(rButtonMapping.get(entryValues[position]));
        row.setClickable(true);
        row.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                for (String currentString : rButtonMapping.keySet()) {
                    rButtonMapping.put(currentString, false);
                }
                // Set the current selected value to true
                rButtonMapping.put((String) entryValues[position], true);

                // Also send the selected value to the editor
                editor.putString(SettingsActivity.CUST_PREF_KEY,
                        (String) entryValues[position]);
                editor.commit();

                getDialog().dismiss();
            }
        });

        rButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                for (String currentString : rButtonMapping.keySet()) {
                    rButtonMapping.put(currentString, false);
                }
                // Set the current selected value to true
                rButtonMapping.put((String) entryValues[position], true);

                // Also send the selected value to the editor
                editor.putString(SettingsActivity.CUST_PREF_KEY,
                        (String) entryValues[position]);
                editor.commit();

                getDialog().dismiss();
            }
        });

        return row;
    }
}

}

I also did research here on Stackoverflow and Google, but no one seems to have this problem.

Does anyone see what the problem is?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:39:13+00:00Added an answer on June 17, 2026 at 10:39 pm

    I suggest to replace the call to setAdapter with a call to setSingleChoiceItems method of the builder:

    builder.setSingleChoiceItems(adapter, adapter.getSelectedItem(), 
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
    
                }
            });
    

    The adapter.getSelectedItem() would be a method that returns the index of the current selected row.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im facing following problem I have created mentioned condition, but when I choose y
I have defined the following ApplicationManifest file the problem is that i have created
I have the following interesting problem. I've created a secondary login form. From that
Probably beginner jquery question... Here is my problem: I have dynamically created list of
I have created a custom Jquery Timer. but i am facing little problem i
I have following problem. I have created a tab based Application with three Views
I have the following problem / bug: I made a custom button-class (called CustomBlitButton)
I have a problem with putting different images in a list. I created a
I have created Custom Annotation with following: -(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { MKPinAnnotationView *view =
i have the following problem: i would like to create a footer. that footer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.