ListView中的CheckBox离开屏幕时被重置 [英] CheckBox in ListView being reset when it leaves the screen
问题描述
我已按照此处的教程创建了一个自定义的ListView
,其中显示了带有类别标题.我修改了list_item_entry.xml
以便在项目中放入CheckBox
:
I have followed the tutorial here to create a custom ListView
that shows items with category headers. I have modified the list_item_entry.xml
to put a CheckBox
in the item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize" >
<CheckBox
android:id="@+id/option_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="6dp"
android:focusable="false"
android:clickable="false" />
<TextView
android:id="@+id/list_item_entry_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:ellipsize="marquee"
android:fadingEdge="horizontal" />
</LinearLayout>
我的问题是,如果我检查了某些CheckBoxes
,然后将其滚动到屏幕外,则当它们回来时,它们未被选中.但是,listView.getCheckedItemPositions()
仍显示该项目为checked
.
My problem is that if I check some of the CheckBoxes
then scroll them off the screen, when they come back they are unchecked. However listView.getCheckedItemPositions()
still shows that the item is checked
.
我很确定我的问题出在我的自定义ArrayAdapter
中的getView()
方法上:
I'm pretty sure that my problem is with the getView()
method in my custom ArrayAdapter
:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Item i = items.get(position);
if (i != null) {
if(i.isSection()){
SectionItem si = (SectionItem)i;
convertView = vi.inflate(R.layout.list_item_section, parent, false);
convertView.setOnClickListener(null);
convertView.setOnLongClickListener(null);
convertView.setLongClickable(false);
final TextView sectionView =
(TextView) convertView.findViewById(R.id.list_item_section_text);
sectionView.setText(si.getTitle());
}else{
EntryItem ei = (EntryItem)i;
convertView = vi.inflate(R.layout. list_item_entry, parent, false);
final TextView title =
(TextView) convertView.findViewById(R.id.list_item_entry_title);
if (title != null)
title.setText(ei.getTitle());
}
}
return convertView;
}
我想我这里有两个问题,尽管我也不知道如何解决这两个问题:
I think that I have two issues here, though I have no idea how to solve either:
-
每次使用
vi.inflate
都会导致android不断创建不好的视图(对此不确定).我尝试仅在convertView == null
时将其充气,但有时convertView
的格式错误,即.List_item_section
何时应为List_item_entry
.每次充气都可以吗?
Using
vi.inflate
every time is causing android to constantly create views which is bad (not sure about this). I tried to only inflate it ifconvertView == null
but then sometimesconvertView
would be in the wrong format, ie.List_item_section
when it should beList_item_entry
. Is it fine to inflate it everytime?
我认为每次放大视图都会导致CheckBoxes
重置,尽管对此我可能是错的.
I think that inflating the view each time is causing the CheckBoxes
to be reset, although I may be wrong about this.
那么我该如何做,以便离开时CheckBoxes保持选中状态并返回屏幕?如果列表足够长,此方法是否会用Views填充Android的内存?
So how do I make it so the CheckBoxes will stay checked when the leave and return to the screen? And will this method fill Android's memory with Views if the the list is sufficiently long?
更新:
我喜欢@ user3815165的答案,因为我不需要为没有复选框的sectionItem
存储checked
值.但是正如我在评论中提到的那样,由于items
列表不在Activity的上下文中,因此在销毁视图并创建错误时,是否会检查每个EntryItem
的值仍然存在.
Update:
I liked @user3815165's answer because I didn't need to store the checked
value for a sectionItem
which doesn't have a checkbox. But as I mentioned in a comment, since the items
list is not in the context of the Activity then the values of whether each EntryItem
is checked or not persists when the view is destroyed and creates bugs.
因此,我决定使用@Palash的答案,即使它存储不需要的数据(列表中每个SectionItem
仅一个boolean
值).效果很好.
So I decided to go with @Palash's answer, even though it stored data not needed (only a single boolean
value for each SectionItem
in the list). It works perfectly.
推荐答案
您需要在活动中维护一个boolean类型的状态数组,将该数组传递到列表适配器中,同时设置该位置的复选框检查状态,您还需要在复选框的click事件上更新该状态数组. 尝试此操作,您将获得所需的输出.
you need to maintain a status array of type boolean in your activity, pass that array into your list adapter and while setting the checkbox check status of that position, also you need to update that status array likewise on click event of checkbox. try this you will get the desired output.
//While Setting the checkbox in adapter
if(bStatus[position]==false)
{
itemSet.chSelectItem.setChecked(false);
}else if(bStatus[position]==true)
{
itemSet.chSelectItem.setChecked(true);
}
在您的主要活动中
//initilize Arraylist in main Activity
boolean[] bStatus;
bStatus = new boolean[BeanArray.size()];
Arrays.fill(bStatus, false);
MyAdapter adapter = new MyAdapter(this, BeanArray, bStatus);
listView.setAdapter(adapter);
这篇关于ListView中的CheckBox离开屏幕时被重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!