Android CheckedTextView未正确检查

时间:2013-09-15 03:27:51

标签: java android listview checkedtextview

我有一个带有复选框的ListView,我需要在单击时切换。出于某种原因,我让它在较旧的Android操作系统上工作,但现在它在最新版本中无法运行。

这是我的XML文件:

<?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" />

相关的源代码:

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());
            }
        });
    }
}

什么可能导致它在构建之间有所不同?或者是否有更简单的方法来实现一个简单的列表,每个项目都有一个复选框?

谢谢!

1 个答案:

答案 0 :(得分:0)

因为ListView将管理项目的检查状态,所以可以删除check.setChecked的代码。