jeudi 24 août 2017

How can I check specific checkboxes on startup?

I am using Android Studio to create an app using java but I am fairly new to it, most of my experience is in Visual Studio with C# and Winforms.

Currently I have a ListView with CheckBoxes inside of it and I'm using an ArrayAdapter to sync the list.

    ArrayList<String> items = new ArrayList<>();
    items.add("Alpha");
    items.add("Bravo");
    items.add("Charlie");
    items.add("Delta");

    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.row_layout, R.id.chkText, items);
    lv.setAdapter(adapter);

    // Test
    Toast.makeText(getApplicationContext(), "Count " + lv.getAdapter().getCount(), Toast.LENGTH_LONG).show();
    for (int j = 1; j < lv.getAdapter().getCount(); j++)
    {
        CheckBox cb = (CheckBox) lv.getAdapter().getItem(j);
        cb.setChecked(true);
    }

The getCount() does return the proper value the app crashes when it tries to check the boxes. All the answers I found online seem way overcomplicated for such a simple task. Is there nice and easy way I can check boxes "Bravo" and "Delta" when the app loads for example?




Aucun commentaire:

Enregistrer un commentaire