开发者

Every seventh box checked with CheckBoxes in ListView

I have a ListView that has 649 entries. Each View in the list has two LinearLayouts, an ImageView, a few TextViews, and a CheckBox. I currently have code to go through all the CheckBoxes and count them, and more code to figure out which ones are checked so I can use them. However, whenever I check one of the CheckBoxes, every seventh CheckBox above and below it magically become checked as well. The method to count the checked boxes returns the number I actually checked, but the method to get the indices of all the checked boxes just returns the first x checked, regardless of whether I checked them or the ones 7 away from them. For example, if I check number 21, the method returns index 3. I have included some of the relevant code segments: The layout code for each View in the list:

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/id"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:textSize="20sp"/>

<LinearLayout
    android:layout_width="180dp"
    android:layout_height="50dp"
    android:orientation="vertical">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/name"/>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
开发者_如何学编程        android:textColor="#777777"
        android:id="@+id/type"/>

</LinearLayout>
<CheckBox
    android:layout_gravity="right|center_horizontal"
    android:id="@+id/check"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false"/>

Check counting method:

    public int[] whichAreChecked()  //TODO: this should work.
{
    int listItemCount = ChooserList.getChildCount();
    ArrayList<Integer> ints=new ArrayList<Integer>();
    for(int i=0; i<listItemCount; i++)
    {
        CheckBox cbox = (CheckBox) ((View)ChooserList.getChildAt(i)).findViewById(R.id.check);
        if(cbox.isChecked())
            ints.add(i);
    }
    Log.e("durr", ""+ints.size());
    int[] result=new int[ints.size()];
    for(int i=0; i<result.length; i++)
        result[i]=ints.get(i);
    return result;
}

Check counting method:

    public int countChecks()
{
    int checked=0;
    int listItemCount = ChooserList.getChildCount();
    for(int i=0; i<listItemCount; i++)
    {
        CheckBox cbox = (CheckBox) ((View)ChooserList.getChildAt(i)).findViewById(R.id.check);
        if(cbox.isChecked())
            checked++;
    }
    Log.e("countChecks()", ""+checked);
    return checked;
}

If I can add any extra info, please ask!


Hmmmm I think you misunderstood what the getChildCount() and getChildAt() methods do. Those will actually get only the rendered views. Unless I misread something here, if you have 650ish rows you cannot accomplish what you want with the views that are being rendered. Instead, you should implement an onItemClickListener (or, alternatively, something that fires when the checkboxes are clicked) and after the checkbox is checked, save that state into your model.


I was also facing a similar king of problem, so after lot of reading I solved this problem like this:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.listview, null);
            holder = new ViewHolder();
            holder.nameView = (TextView)convertView.findViewById(R.id.textView1);
            holder.numberView = (TextView)convertView.findViewById(R.id.textView2);
            holder.cb = (CheckBox)convertView.findViewById(R.id.checkBox1);
            convertView.setTag(holder);                
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        holder.nameView.setText(mData.get(position).toString());
        holder.numberView.setText(mNumber.get(position).toString());
        holder.cb.setChecked(false);
        holder.cb.setTag(position);



        if(selected.indexOf(mNumber.get(position).toString()) >= 0)
        {

        holder.cb.setChecked(true);
        }

        return convertView;
    }

}

Here what I am doing that on getView() I am unchecking all the checkboxes and checking again manually those which I need to be checked according to the textview it corresponds. So if the user scroll down after checking the first checkbox, all the checkbox in the view will get unchecked and if he again scrolls up then also all the checkboxes will be unchecked but then the one he clicked before will be again rechecked.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜