与许多复选框的ListView适配器 [英] ListView adapter with many checkboxes

查看:280
本文介绍了与许多复选框的ListView适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的ListView一些小问题。每个项目都有一些信息和一个复选框。这种精细显示出来,我可以选择和取消选中复选框等等等等。

不过,我发现了一些奇怪的行为。可以说,我点击第一个复选框的第一行。如果ListView的小,所以你并不需要向下滚动,这工作正常。但是,如果ListView的大,所以我需要向下滚动才能看到所有的项目,在底部一些随机的项目也成为点击。
同样的行为的其他方式,如果我在列表视图的底部点击复选框,然后滚动起来,一些随机的复选框也点击顶部。如果我点击复选框几个地方,其他地方也有点击复选框相同。我想这发生在GetView(...)被调用,当它更新的意义。然后,它使一些新的复选框,点击,但我不知道为什么。

鸭preciate一些帮助这里! :)

我的适配器SSSCE:

 公共类ListViewAdapterSSCCE:BaseAdapter
{
    私人活动myActivity;
    私人的String [] someData;
    私人诠释numberOfElements;
    私人复选框[]盒;    公共ListViewAdapterSSCCE(活动活动)
    {
        this.myActivity =活动;        的for(int i = 0; I< numberOfElements;我++)
            this.boxes [I] =新的复选框(myActivity);
    }    公共无效的SetData(字符串[] someData)
    {
        this.someData = someData;
        this.numberOfElements = someData.Length;
    }    公共无效CheckAllBoxes(布尔器isChecked)
    {
        的for(int i = 0; I< numberOfElements;我++)
            this.boxes [I] .Checked =器isChecked;
    }    公众覆盖的java.lang.Object的GetItem(INT位置)
    {
        返回null;
    }    公众覆盖长GetItemId(INT位置)
    {
        返回10;
    }    公众覆盖诠释计数
    {
        {返回numberOfElements; }
    }    公众覆盖查看GetView(INT位置,查看convertView,父母的ViewGroup)
    {
        如果(convertView == NULL)
        {
            LayoutInflater吹气=(LayoutInflater)myActivity.GetSystemService(Context.LayoutInflaterService);
            convertView = inflater.Inflate(Resource.Layout.ChildrenList_item,NULL);
        }        VAR itemBox = convertView.FindViewById<&复选框GT;(Resource.Id.checkbox);
        this.boxes [位置] = itemBox;        VAR的TextView = convertView.FindViewById<&TextView的GT;(Resource.Id.item_data);
        textView.Text = this.someData [位置]        返回convertView;
    }
}


解决方案

您可以看到样本code在这里有复选框状态替代修复:

<一个href=\"http://stackoverflow.com/questions/10214271/custom-checkbox-difficulty-in-android/10278905#10278905\">custom复选框困难的android

如果这篇文章可以帮助你,请注明这篇文章作为一个答案。

感谢。

I'm having some minor problems with my listview. Every item has some information and a checkbox. This shows up fine, I can select and deselect checkboxes etc etc.

However I discovered some strange behaviour. Lets say I click the first checkbox at the first row. If the ListView is small so you don't need to scroll down this works fine. But if the ListView is large so I need to scroll down to see all the items, some random item at the bottom also becomes clicked. Same behaviour the other way around, if I click a checkbox at the bottom of the listview and scroll up, some random checkbox is also clicked at top. If i click several checkboxes somewhere, some other place there are the same amount of clicked checkboxes. I figured this happens when GetView(...) is called, meaning when it updates. It then makes some new checkboxes clicked but I don't know why.

Appreciate some help here! :)

SSSCE of my adapter:

public class ListViewAdapterSSCCE : BaseAdapter
{
    private Activity myActivity;
    private string[] someData;
    private int numberOfElements;
    private CheckBox[] boxes;

    public ListViewAdapterSSCCE(Activity activity)
    {
        this.myActivity = activity;

        for (int i = 0; i < numberOfElements; i++)
            this.boxes[i] = new CheckBox(myActivity);
    }

    public void SetData(string[] someData)
    {
        this.someData = someData;
        this.numberOfElements = someData.Length;
    }

    public void CheckAllBoxes(bool isChecked)
    {
        for (int i = 0; i < numberOfElements; i++)
            this.boxes[i].Checked = isChecked;
    }

    public override Java.Lang.Object GetItem(int position)
    {
        return null;
    }

    public override long GetItemId(int position)
    {
        return 10;
    }

    public override int Count
    {
        get { return numberOfElements; }
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        if (convertView == null)
        {
            LayoutInflater inflater = (LayoutInflater)myActivity.GetSystemService(Context.LayoutInflaterService);
            convertView = inflater.Inflate(Resource.Layout.ChildrenList_item, null);
        }

        var itemBox = convertView.FindViewById<CheckBox>(Resource.Id.checkbox);
        this.boxes[position] = itemBox;

        var textView = convertView.FindViewById<TextView>(Resource.Id.item_data);
        textView.Text = this.someData[position];

        return convertView;
    }
}

解决方案

You can see sample code in here to have an alternative fix for the checkbox state:

custom checkbox difficulty in android

If this post helps you, please mark this post as an answer.

Thanks.

这篇关于与许多复选框的ListView适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆