lundi 25 janvier 2016

simple_list_item_multiple_choice listview adapter check and uncheck

It may be a stupid logic question, but I can't seem to understand why when I click the list item the box checks up, but will not un-check when clicked again, as if the onClickListener is looping. If anyone can point out this otherwise obvious issue, it would be greatly appreciated

public class WeightSelection extends AppCompatActivity {

private Database database;
private ListView list;
private ArrayAdapter<String> weightAdapter;

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

    database = new Database(this);


    if (list == null) {
        list = (ListView) findViewById(R.id.weight_selection);
    }


    weightAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_multiple_choice,
            database.columns);
    list.setAdapter(weightAdapter);
    list.setChoiceMode(list.CHOICE_MODE_MULTIPLE);
    setListAdapter(weightAdapter);



    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            if (list.isItemChecked(position))list.setItemChecked(position, false);
            else
            list.setItemChecked(position, true);


        }
    });
}

protected void setListAdapter(ListAdapter adapter) {
    getListView().setAdapter(adapter);
}

protected ListView getListView() {
    if (list == null) {
        list = (ListView) findViewById(R.id.weight_selection);
    }
    return list;
}

protected ListAdapter getListAdapter() {
    ListAdapter adapter = getListView().getAdapter();
    if (adapter instanceof HeaderViewListAdapter) {
        return ((HeaderViewListAdapter) adapter).getWrappedAdapter();
    } else {
        return adapter;
    }
}




Aucun commentaire:

Enregistrer un commentaire