【发布时间】:2014-04-06 10:47:25
【问题描述】:
似乎找不到任何不推荐使用的 (HONEYCOMB+) 示例来展示如何将动态创建的首选项添加到 preferencefragment,而不是使用预定义表单字段的 xml 资源。
具体来说,我正在尝试使用已与手机配对的蓝牙设备创建复选框列表。使用mBluetoothAdapter.getBondedDevices(),大概是CheckBoxPreference。
class GeneralPreferenceFragment extends PreferenceFragment {
private Context context = getBaseContext();
private int i;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//get Bluetooth Adapter
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
SharedPreferences prefs = context.getSharedPreferences("bt_devices", 0);
this.i = 0;
for (BluetoothDevice device : pairedDevices) {
CheckBoxPreference checkBoxPreference = new CheckBoxPreference(context);
checkBoxPreference.setKey("btDevice_"+this.i);
checkBoxPreference.setChecked(prefs.getBoolean("btDevice_"+this.i, false));
this.i++;
//(Somehow add CheckboxPreference to PreferenceFragment)
}// end for loop
}// end if(paired devices)
}// end onCreate()
} // end PreferenceFragment
任何建议将不胜感激。
【问题讨论】:
标签: android dynamic android-activity preferencefragment