android - Android CheckedTextView setChecked 无法正常工作
问题描述
将数据填充到适配器类中的列表视图(每个项目)中
// populate the data
item.setText(megaItem.getName());
item.setChecked(megaItem.getStatus());
允许在活动中使用多个复选框选择
listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
问题:
问题 #1。如果,我use above line
在 Activity 中的代码,那么我can check and uncheck all the items
在 a 中list
,但问题是,我can't see some of my items as checked
默认情况下,正如我status true
在代码中为他们提到的那样
问题2。如果,我don't use above line
在 Activity 中的代码,那么我can see some of my items as checked
默认情况下,正如我在代码中提到他们的状态为真,但问题是,我can't check and uncheck any other item
,在list
xml:
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/checkedTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/CustomStyledRadioButton"
android:drawableLeft="?android:attr/listChoiceIndicatorMultiple" />
来自适配器类的 getView()
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
//get the data for the item at this position
MegaItem megaItem = getItem(position);
//check if the view is being reused, otherwise inflate it
if(convertView == null)
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.items_listview_layout,parent,false);
}
//get references to the UI elements to populate them
item = (CheckedTextView) convertView.findViewById(R.id.checkedTextView);
//populate the data
item.setText(megaItem.getItemName());
item.setChecked(megaItem.getStatus());
//return the view to render on the screen
return convertView;
}
解决方案
试试这个。
selectAll = (CheckBox) findViewById(R.id.selectAll);
selectAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleAll(selectAll.isChecked());
}
});
private void toggleAll(Boolean check)
{
int size = yourListView.getCount();
for ( int i=0; i<size ; i++ ) {
View v = yourListView.getAdapter().getView(i, null, null);
CheckedTextView tv = (CheckedTextView) v.findViewById(R.id.eventName);
tv.setChecked(check);
}
}
并在 getView() 方法中。
public View getView(int position, View convertView, ViewGroup parent) {
CheckedTextView name = (CheckedTextView) view.findViewById(R.id.eventName);
String[] result = item.split("/");
name.setText("txt");
name.setChecked(true);
return view;
推荐阅读
- android - ROM刷新后“无法安装内部SD卡”
- javascript - 如何优化 React 编译的.js?
- php - 即使与 index.php 位于同一目录中,CSS 和 JS 也不会加载
- php - php和Axios Node Js没有响应输出
- angular - Angular 6 无法解析使用电子的“fs”和“路径”
- javascript - 将 for 循环中的每个整数分配给输入 id 属性[for 循环工作正常]
- excel - 在 Excel 中扩展行
- java - 在 Java 中,有没有办法在 for 循环中将元素的最后一次运行添加到数组中?
- java - 如何上课
函数的 - vue.js - 验证子组件Vue vee-validate