2013-01-21 123 views
1

我遇到以下問題。 我創建了一個自定義列表首選項來顯示帶有附加圖像的文本視圖。 幾乎一切正常,除了以下問題的罰款:滾動到ListPreference中的選定項目

原始列表優先滾動到當前選擇的位置,但我的自定義列表的偏好不顯示此行爲。如果我用android版本(或在我的情況下,holoeverywhere版本)替換自定義列表首選項,一切工作正常。 因此,我認爲該錯誤必須在我當前實現的自定義列表首選項中。

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

}

我也做了研究這裏#2和谷歌,但似乎沒有人有這個問題。

有沒有人看到問題是什麼?

回答

2

我建議更換電話與該製造商的setSingleChoiceItems方法調用setAdapter:

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

      } 
     }); 

的adapter.getSelectedItem()將返回當前選定行的索引的方法。

+0

嘿,謝謝。這個解決方案完美地工作:)我已經放棄尋找解決方案。但是,這很好。再次感謝你。 – Vion

0

滾動到選定的項目在ListPreference,只需使用Android ListViewsetSelection方法在自定義ListPreference類的onCreateDialogView()方法。例如:

public class CustomListPreference extends ListPreference { 
    @Override 
    protected View onCreateDialogView() {    
     ListView mListView = (ListView) view.findViewById(android.R.id.list); 
     // Scroll to selected item in ListView. 
     mListView.setSelection(findIndexOfValue(getValue())); 
    } 
} 
+0

比接受的答案更好的解決方案。謝謝。 – 2014-12-23 14:40:05