Shmuel
Shmuel

Reputation: 3916

MultiSelectListPreference use data from Cursor

I'm trying to have a multiselect preference with data that is populated from a database via a cursor. I've got this so far

 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[]{}));

and it fill up the prefrence fine, but I cannot figure out how to get the saved values and persist them to the database.

How can I get the new values from the preference? I tried setOnPreferenceChangeListener but it gave me the same values that I filled the list with, not the new ones.

(Would it be easier to just build a listview with checkboxes?)

Upvotes: 0

Views: 282

Answers (1)

Shmuel
Shmuel

Reputation: 3916

So I solved the problem by just creating a popup list activity.

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


    }
}

Upvotes: 0

Related Questions