Note that there are some explanatory texts on larger screens.

plurals
  1. POScrolling to selected item in ListPreference
    text
    copied!<p>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:</p> <p>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.</p> <pre><code>public class CustomListPreference extends ListPreference { private CustomListPreferenceAdapter customListPreferenceAdapter = null; private Context mContext; private LayoutInflater mInflater; private CharSequence[] entries; private CharSequence[] entryValues; private Map&lt;String, Boolean&gt; 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&lt;String, Boolean&gt;(); prefs = PreferenceManager.getDefaultSharedPreferences(mContext); // init the button mapping with the default values for (int i = 0; i &lt; 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; } } </code></pre> <p>}</p> <p>I also did research here on Stackoverflow and Google, but no one seems to have this problem. </p> <p>Does anyone see what the problem is?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload