Android CheckedTextView not checking properly

1.6k views Asked by At

I have a ListView with checkboxes that I need to toggle when clicked. For some reason, I got it working on an older Android OS, but it is now not functioning in the latest build.

Here is my XML file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

<ListView
   android:id="@android:id/list"
   android:layout_width="fill_parent"
   android:layout_height="0dip"
   android:layout_weight="0.22"
   android:fastScrollEnabled="true" />

And the relevant source code:

package com.taptimer;

public class HistoryActivity extends ListActivity {

    final DatabaseHandler db = new DatabaseHandler(this);
    private SimpleAdapter adapter;
    private List<Map<String, String>> data = new ArrayList<Map<String, String>>();
    private List<ScramblePair> scramblePairs;
    private ListView listView;
    private List<Integer> checkedPositions = new ArrayList<Integer>();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        Intent intent = getIntent();
        scramblePairs = db.getAllScramblePairs("puzzle ASC, id DESC");

        for (ScramblePair sn : scramblePairs) {
            if (sn.getPuzzle().equals(puzzleSelected)) {
                Map<String, String> datum = new HashMap<String, String>(3);
                String time = milliesToDuration(sn.getTime());
                datum.put("time", time);
                datum.put("scramble", sn.getScramble());
                data.add(datum);
            }
        }

        adapter = new SimpleAdapter(
                this,
                data,
                android.R.layout.simple_list_item_multiple_choice,
                new String[] {"time", "scramble", "id", "modifier", "timestamp"},
                new int[] {android.R.id.text1,
                           android.R.id.text2});

        listView = getListView();
        listView.setLongClickable(true);
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position,
                    long arg3) {
                CheckedTextView check = (CheckedTextView) view;
                if (!check.isChecked()) checkedPositions.add(position);
                check.setChecked(!check.isChecked());
            }
        });
    }
}

What can be causing it to be different between builds? Or is there an easier way to implement a simple list with each item having a checkbox?

Thanks!

1

There are 1 answers

1
Seungmin Ha On BEST ANSWER

Because ListView will manage check state of items, the codes that is check.setChecked may be removed.