gpt4 book ai didi

java - Android CheckedTextView 未正确检查

转载 作者:行者123 更新时间:2023-12-01 04:22:06 26 4
gpt4 key购买 nike

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

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

谢谢!

最佳答案

因为ListView会管理item的check状态,所以check.setChecked的代码可能会被删除。

关于java - Android CheckedTextView 未正确检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18808644/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com