Android 带有ListView的复选框和不带using Array的自定义适配器
这个问题很简单,因为这里已经广泛讨论过了,关于Listview,复选框在scrooling后处于松动状态或无法保持其状态。 在我的例子中,当滚动出视图时,选中的框将被解锁。在这篇文章中:,我几乎实现了渴望的行为;但是,它使用的是ArrayList,我已经了解到,这个实现所做的重画数量,当您有一个很长的列表时,它是值得的。。。 我在这里使用了20多个可用的实现。不幸的是,我无法找出它为什么不工作。 所以,请对我有耐心。 我只发布我认为对我的问题至关重要的东西。 下面的显示了分类活动的选择。 注意:我正在传递dbAdapter和SimpleCursorAdapter。Android 带有ListView的复选框和不带using Array的自定义适配器,android,listview,checkbox,Android,Listview,Checkbox,这个问题很简单,因为这里已经广泛讨论过了,关于Listview,复选框在scrooling后处于松动状态或无法保持其状态。 在我的例子中,当滚动出视图时,选中的框将被解锁。在这篇文章中:,我几乎实现了渴望的行为;但是,它使用的是ArrayList,我已经了解到,这个实现所做的重画数量,当您有一个很长的列表时,它是值得的。。。 我在这里使用了20多个可用的实现。不幸的是,我无法找出它为什么不工作。 所以,请对我有耐心。 我只发布我认为对我的问题至关重要的东西。 下面的显示了分类活动的选择。 注意:
public class ShowChosenItensFromCategoriaActivity1 extends AppCompatActivity
{
现在,显示来自所选分类.xml的所有产品,方法从分类(分类ID)获取所选产品,它是。显示来自所选分类行的所有产品。xml
代码为:显示所选目录中的所有产品。\u categoria.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<ListView
android:id="@+id/produtos_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
android:layout_marginBottom="@dimen/list_padding"
android:layout_marginTop="@dimen/list_padding"
android:background="@color/lime_100"
android:padding="@dimen/list_padding"
android:scrollbarStyle="outsideOverlay"
tools:listitem="@layout/show_all_produtos_from_chosen_categoria_row"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/activatedBackgroundIndicator"
android:orientation="horizontal"
tools:context=".ShowChosenItensFromCategoriaActivity">
<TextView
android:id="@+id/idlistitem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elegantTextHeight="true"
android:ellipsize="end"
android:scrollHorizontally="false"
android:textColor="@color/blue"
android:textSize="20sp"/>
<CheckBox
android:id="@+id/idcheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginEnd="10dip"
android:layout_marginStart="4dip"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center">
</CheckBox>
</RelativeLayout>
代码为:显示所选类别行中的所有产品。xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<ListView
android:id="@+id/produtos_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
android:layout_marginBottom="@dimen/list_padding"
android:layout_marginTop="@dimen/list_padding"
android:background="@color/lime_100"
android:padding="@dimen/list_padding"
android:scrollbarStyle="outsideOverlay"
tools:listitem="@layout/show_all_produtos_from_chosen_categoria_row"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/activatedBackgroundIndicator"
android:orientation="horizontal"
tools:context=".ShowChosenItensFromCategoriaActivity">
<TextView
android:id="@+id/idlistitem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elegantTextHeight="true"
android:ellipsize="end"
android:scrollHorizontally="false"
android:textColor="@color/blue"
android:textSize="20sp"/>
<CheckBox
android:id="@+id/idcheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginEnd="10dip"
android:layout_marginStart="4dip"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center">
</CheckBox>
</RelativeLayout>
}
我很抱歉格式化了。
如果你们有时间和耐心,我会感谢你们的帮助
注意。要确定是否应检查视图,请使用数据库中列的值:
viewHolder.checkBox.setChecked(mCursor.getInt(mCursor.getColumnIndex("checked")) == 1);
但要将视图标记为选中,您使用的是DBAdapter类:
dbAdapter.setCheckBox(auxProdutoID, isChecked ? 1 : 0);
如果要控制DBAdapter类中的状态,则应使用相同的代码控制复选框状态,以决定是否应检查复选框,是否应检查DBAdapter类,或者是否更喜欢使用数据库,在onClickListener中,您应该更新数据库中的数据。有时,由于列表视图中使用的自定义项布局,会出现维护问题。 你试过这个吗
mCursorAdapter = new ShowChosenItensFromCategoriaAdapter1(this,
android.R.layout.simple_list_item_checked,
mCursor, From, To, 0, dbAdapter);
mListView = (ListView) findViewById(R.id.produtos_listview);
mListView.setAdapter(mCursorAdapter);
mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
我把适配器的名字弄错了:请考虑它没有数字1(一)。盖伊斯,我相信我找到了它的错误所在,但是我不知道如何修复它。我调试并意识到(duh!)当列表上下滚动时,位置是正确的,但“produtoID”不是。因此,当使用“produtoID”检查viewholder.checkbox.ischecked()时,它会在数据库中得到错误的记录。但我不知道如何“标记”它,因为我的ViewHolder如下所示:私有静态类ViewHolder{int produtoID;TextView textviewProduto;CheckBox;}它可能就是它,因为当我在调试过程中手动更改时,它在那里会正确。我忘了显示dbAdapter.setCheckBox的代码。这是:public void setCheckBox(int-nID,int-checked){ContentValues=new ContentValues();if(!String.valueOf(nID.isEmpty()){values.put(DBContract.LISTA.COLUMN_)CHECKBOX,checked);String where=“listaprodutoid=?”;String[]args={String.valueOf(nID)};mDB.update(DBContract.LISTA.TABLE_NAME_LISTA,values,where,args);}}Hi here。谢谢你的回答。方法dbAdapter.setCheckBox(…)正是这样做的:我将数据库列标记为checked,然后它进行更新。事实上,当我离开活动并重新开始时,所有选中的复选框都在那里。这个问题依赖于Listview的滚动和Recyclerview特性,我对此无法完全理解。谢谢你的提示。你好。我使用ListView.CHOICE\u MODE\u MULTIPLE\u model在用户单击菜单时显示菜单。复选框仅在单击时选中/取消选中。见相应图片:1-)列表视图选中:;2-)未选中列表视图:;3-)离开并进入活动:。4-)取消选中已选中的项:;5-)菜单点击:谢谢。这是Google Play上的链接:我想做的是在列表中的这些人前面添加复选框:问候。
mCursorAdapter = new ShowChosenItensFromCategoriaAdapter1(this,
android.R.layout.simple_list_item_checked,
mCursor, From, To, 0, dbAdapter);
mListView = (ListView) findViewById(R.id.produtos_listview);
mListView.setAdapter(mCursorAdapter);
mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);