0

我正在尝试对通过游标从数据库填充的数据进行多选首选项。到目前为止我有这个

 Cursor   cursor = getActivity().getContentResolver().query(EntryProvider.WEBSITES_URI,null,null,null,EntryProvider.WEBSITES_SORT_ORDER);


    while (cursor.moveToNext()) {
        String name = cursor.getString(EntryData.COL_WEBSITE_NAME);
        String displayName = cursor.getString(EntryData.COL_WEBSITE_ENABLED);
        entries.add(name);
        entriesValues.add(displayName);
    }
    feedSelectionPref = (MultiSelectListPreference)findPreference("feedChooser");
    feedSelectionPref.setEntries(entries.toArray(new CharSequence[]{}));
    feedSelectionPref.setEntryValues(entriesValues.toArray(new CharSequence[]{}));

并且它很好地填补了prefrence,但我无法弄清楚如何获取保存的值并将它们持久化到数据库中。

如何从首选项中获取新值?我试过setOnPreferenceChangeListener了,但它给了我与我填写列表相同的值,而不是新值。

(用复选框构建一个列表视图会更容易吗?)

4

1 回答 1

0

所以我通过创建一个弹出列表活动解决了这个问题。

public class FeedChooserPopupActivity extends Activity {

ListView listView;
Cursor cursor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_feed_chooser);

    initViews();
}


private void initViews(){
    setupCursor();
    setupListView();
    setupButton();
}

private void setupButton() {
    ((Button)findViewById(R.id.feedOkbutton)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FeedChooserPopupActivity.this.finish();
        }
    });
}

private void setupCursor() {
    cursor = getContentResolver().query(EntryProvider.WEBSITES_URI,null,null,null,EntryProvider.WEBSITES_SORT_ORDER);

}

private void setupListView() {
    listView = (ListView)findViewById(R.id.feedListView);

    listView.setAdapter(new FeedCursorAdapter(this,
            cursor,
            CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER

    ));

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


            if (view != null) {
                String websiteName = ((TextView)view.findViewById(R.id.feedTextView)).getText().toString();
                CheckBox checkBox = (CheckBox)view.findViewById(R.id.feedCheckBox);
                checkBox.setChecked(!checkBox.isChecked());

                ContentValues cv = new ContentValues();
                int enabled = 0;
                if(checkBox.isChecked()){
                    enabled = 1;
                }
                cv.put(EntryData.KEY_WEBSITE_ENABLED, enabled);

                String where = EntryData.KEY_WEBSITE_NAME+" = '"+websiteName+"'";
                getContentResolver().update(EntryProvider.WEBSITES_URI,cv,where,null);
            }
        }
    });

}






private class FeedCursorAdapter extends CursorAdapter {


    public FeedCursorAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.feed_chooser_list_item,parent,false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        ((TextView)view.findViewById(R.id.feedTextView)).setText(cursor.getString(EntryData.COL_WEBSITE_NAME));
        ((CheckBox)view.findViewById(R.id.feedCheckBox)).setChecked(
                cursor.getInt(EntryData.COL_WEBSITE_ENABLED)==1
        );


    }
}
于 2014-05-28T17:12:15.507 回答