1

我有一个带有复选框的 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());
            }
        });
    }
}

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

谢谢!

4

1 回答 1

0

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

于 2013-09-15T04:41:05.850 回答