WEB开发网
开发学院手机开发Android 开发 Android的ListPreference绑定数据 阅读

Android的ListPreference绑定数据

 2010-05-29 05:54:00 来源:WEB开发网   
核心提示:Preference给我们一般设置带来很多方便,我们只要稍微设置下XML选项能满足我们大部分需求,Android的ListPreference绑定数据,项目中有需求用数据库的数据填充到ListPreference的弹出数据中,方法如下:Java代码public class EditPreferences extends

Preference给我们一般设置带来很多方便,我们只要稍微设置下XML选项能满足我们大部分需求,项目中有需求用数据库的数据填充到ListPreference的弹出数据中,方法如下:

Java代码

public class EditPreferences extends PreferenceActivity {

public static final String SELECTED_TARGET_KEY = "SelectedTargetKey";

public static final String NO_SELECTION = "0";

private TargetDbAdapter mDbHelper;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setPreferenceScreen(createPreferenceHierarchy());

}

private PreferenceScreen createPreferenceHierarchy() {

PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);

PreferenceCategory dialogBasedPrefCat = new PreferenceCategory(this);

dialogBasedPrefCat.setTitle(R.string.pref_cat_title);

root.addPreference(dialogBasedPrefCat);

//builds list from DB

mDbHelper = new TargetDbAdapter(this);

mDbHelper.open();

Cursor c = mDbHelper.fetchAllNotes();

startManagingCursor(c);

int count = c.getCount();

CharSequence[] entries = new CharSequence[count];

CharSequence[] entryValues = new CharSequence[count];

c.moveToFirst();

for(int i=0; i< i++)>

entries[i] = c.getString(c.getColumnIndexOrThrow(TargetDbAdapter.KEY_TITLE));

entryValues[i] = c.getString(c.getColumnIndexOrThrow(TargetDbAdapter.KEY_ROWID));

c.moveToNext();

}

ListPreference targets = new ListPreference(this);

targets.setEntries(entries);

targets.setEntryValues(entryValues);

targets.setDefaultValue(NO_SELECTION);

targets.setDialogTitle(R.string.pref_dialog_title);

targets.setKey(SELECTED_TARGET_KEY);

targets.setTitle(R.string.pref_title);

targets.setSummary(R.string.pref_summary);

dialogBasedPrefCat.addPreference(targets);

//add other preference screens

return root;

}

}

Tags:Android ListPreference 绑定

编辑录入:coldstar [复制链接] [打 印]
赞助商链接