Corbin Tarrant
Corbin Tarrant

Reputation: 181

Setting Initially checked CheckedTextViews in ListView for choiceMode="multipleChoice"

I am having a really difficult time trying to work with android's ListView multipleChoice mode. Here is what I am trying to do:

I have a "players" button in a game setup screen. When this is clicked it opens another activity with a multipleChoice ListView of all the players in the database in CheckedTextViews. I have this working properly and when you click on a player they will be added or removed from the game via a query to the game_players table.

The problem I am having is in setting up the ListView so that the players that have already been added to the game get checked initially when the activity opens.

I have tried to do this by iterating over the entire list in the ListView activity but this doesn't work because the Views that are not currently visible can't be accessed to check.

So now I'm trying to do this in my extended SimpleCursorAdapter in bindView but I can't even get this simple code to work:

@Override
public void bindView(View _view, Context _context, Cursor _cursor) {
    String name = c.getString(c.getColumnIndexOrThrow(from[0]));

    this.player = (CheckedTextView)_view.findViewById(to[0]);
    this.player.setText(name);
    this.player.setChecked(true);
}

It correctly sets the player's name with setText(), but I can't get any of the boxes to check in bindView. Is there somewhere else I should be doing this or am I just doing it incorrectly?

Upvotes: 1

Views: 2285

Answers (2)

SurlyDre
SurlyDre

Reputation: 473

I had trouble with this as well and pieced together this solution. I thought about passing the ListView directly into the Adapter, but chose to create an interface instead, to avoid a circular reference between the ListView and the Adapter.

An interface to loosely couple the adapter to the list:

public interface CheckControl {
    public void setChecked(int position, boolean value);
}

Custom Adapter:

private class MyAdapter extends CursorAdapter {
    private CheckControl checkControl;

    public MyAdapter(Context context, Cursor cursor, CheckControl checkControl) {
        super(context, cursor, 0);
        this.checkControl = checkControl;
        ...
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ...
        checkControl.setChecked(cursor.getPosition(), cursor.getInt(COL_ENABLED) == 1);
        ...
    }
}

And here's how I use these two elements. In my case I'm extending ListViewFragment, but the same could done in any other class that contains the ListView.

public class MyListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ....
        CheckControl checkControl = new CheckControl() {
            public void setItemChecked(int position, boolean checked) {
                getListView().setItemChecked(position, checked);
            }
        };
        setListAdapter(new MyAdapter(getActivity(), null, checkControl));

        // Kick off the loader
        getLoaderManager().initLoader(LOADER_1, null, this);
    }
}

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006869

Call setItemChecked() on the ListView for each checked position.

Upvotes: 7

Related Questions