好吧,我正在制作一个简单的待办事项列表应用程序,它从一组字符串数组中读取项目,并将它们显示在 ListView 中作为 simple_list_item_checked。一切正常,我已经设置了 onClickListener,所以它会检查我正在点击的内容,但是,当我点击一个项目时,它还会检查项目 8 项,重复。 (即我点击最上面的项目,它得到一个检查,第 8 个项目被检查,然后 8 个被检查,等等)这是主要 Activity 的代码:
package com.disneyland;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class Launch extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] Rides = getResources().getStringArray(R.array.rides);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, Rides));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
CheckedTextView check = (CheckedTextView)view;
check.setChecked(!check.isChecked());
}
});
}
}
显然,我的问题是为什么会发生这种情况,我该如何解决?
最佳答案
我也遇到了这个问题,并设法快速修复。 我点击了 icecreamman 的链接,发现我的 ListView XML 缺少以下行:
android:choiceMode="multipleChoice"
这允许选择多个项目。如果我选择了 singleChoice,那么任何时候都只能选中列表中的一项,就像单选按钮一样。此行解决了我遇到的问题。
关于android - Simple List Item Checked 在点击时检查多个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7095094/