Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
These are my requirements for an Android app:
- There is a file with lines of text and "x" as a "done" marker
- The text lines shall be shown in a list on the scren when the app starts
- Each listbox line shall have a checkbox
- Checkbox shall be initially marked if the "x" is found in the corresponding text line
- User may click the checkbox to change the "done" marker
- When the app exits, the current "done" states shall be written to the file

Or in short: A simple "todo list" which remembers the "done" state of each entry.

The problem is that after reading and inserting the text lines into the ListView, I call "setItemChecked" to set the initial "Done" state. This has no effect on the screen. All items are still marked "not done". If I call "isItemChecked" directly after setting it to "true", the answer is "false". So the "setItemChecked" call is ignored.

This is the code:

protected void onCreate (Bundle savedInstanceState)
{
  super.onCreate (savedInstanceState);
  setContentView (R.layout.activity_main);

  lv = (ListView) findViewById (R.id.List);
}

protected void onResume ()
{
  super.onResume ();
  ReadEntriesFromFile (); // this fills the "Entries" and "Checks" arrays
  adap = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, Entries);
  lv.setAdapter (adap); // the texts get copied to the list

  int i;
  for (i=0; i<Entries.length; i+=1)
  {
    lv.setItemChecked (i, Checks[i]); // set the "Done" marker
  }
  lv.invalidate ();
  lv.invalidateViews ();
  adap.notifyDataSetChanged ();
}


What am I doing wrong?

What I have tried:

If have found on the net that calling "invalidate", "invalidateViews" or "notifyDataSetChanged" should help, but they have no effect.
Posted
Updated 22-Jul-18 7:23am
Comments
David Crow 17-Jul-18 15:41pm    
Does R.id.List have the android:choiceMode set to multipleChoice?

Another way is to create your own custom row layout that contains a CheckBox and a TextView.

1 solution

Thank you, David, this is the solution:

Either
- set the ListView's attribute "choiceMode" to "multipleChoice" in the activity layout editor

or
- set the ListView's attribute in the activity's java code:
lv = (ListView) findViewById (R.id.List);
lv.setChoiceMode (ListView.CHOICE_MODE_MULTIPLE);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900