jeudi 2 mars 2017

Strange problems with checkboxes in listview

i have some problems with checkboxes in listview. Here is my layout for the list items.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
    android:orientation="vertical"
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="300.0dp"
    android:layout_height="match_parent"
    android:id="@+id/linearLayout2"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="0.0dp">
    <TextView
        android:text="Text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView21"
        android:paddingTop="10dp"
        android:textSize="25dp"
        android:textStyle="bold"
        android:textColor="#000000"
        android:paddingBottom="10dp" />
</LinearLayout>
<LinearLayout
    android:orientation="vertical"
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="70.0dp"
    android:layout_height="match_parent"
    android:id="@+id/linearLayout3"
    android:layout_marginRight="5dp">
    <CheckBox
        android:paddingTop="40dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/checkBox1" />
</LinearLayout>
</LinearLayout>

Here is my listview adapter

 public class CheckBoxAdapter : BaseAdapter<string>
{
    private Activity activity;
    private string[] _names1;

    public CheckBoxAdapter(Activity activity, string[] names1)
    {
        this.activity = activity;
        this._names1 = names1;
    }



    public override int Count
    {
        get { return this._names1.Count(); }
    }

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

    public override string this[int position]
    {
        get
        {
            return this._names1[position];
        }
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View view = convertView;

        if (view == null)
        {
            view = this.activity.LayoutInflater.Inflate(Resource.Layout.listaUczniow, null);
        }



        view.FindViewById<TextView>(Resource.Id.textView21).Text = _names1[position];


        CheckBox chkCaptain = view.FindViewById<CheckBox>(Resource.Id.checkBox1);
        chkCaptain.Tag = _names1[position];

        chkCaptain.SetOnCheckedChangeListener(null);

        chkCaptain.SetOnCheckedChangeListener(new CheckedChangeListener(this.activity));

        return view;
    }

    private class CheckedChangeListener : Java.Lang.Object, CompoundButton.IOnCheckedChangeListener
    {
        private Activity activity;

        public CheckedChangeListener(Activity activity)
        {
            this.activity = activity;
        }

        public void OnCheckedChanged(CompoundButton buttonView, bool isChecked)
        {
            if (isChecked)
            {
                string name = (string)buttonView.Tag;
                string text = string.Format("{0} Checked.", name);
                Toast.MakeText(this.activity, text, ToastLength.Short).Show();
            }
        }
    }
}

Here is another axml code (with listview in it).

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />
<ListView
    android:layout_height="0dip"
    android:layout_width="match_parent"
    android:id="@+id/listViewCZWARTA"
    android:divider="@drawable/dotted"
    android:dividerHeight="10dp"
    android:cacheColorHint="#00000000"
    android:drawSelectorOnTop="false"
    android:layout_weight="1" />
 <Button
    android:text="BUTTON"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/button1"
    android:textColor="#FFFFFF"
    android:textSize="20sp"
    android:background="@drawable/btn"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp" />
  </LinearLayout>

and here is how im calling these

         listUcz4 = FindViewById<ListView>(Resource.Id.listViewCZWARTA);
        namesUcz = new string[12] { "1.", "2.", "3.", "4.", "5.", "6.", "7.", "8.", "9.", "10.", "11.", "12." };
        CheckBoxAdapter adapter = new CheckBoxAdapter(this, namesUcz);
        listUcz4.Adapter = adapter;

When im emulating this program on phone (android API 22) it looks fine at first, when im clicking on the 1st checkbox in first list row the toast message shows the right information that the ,,.1" have been checked. But the strange thing is that with only one click on the 1st checkbox the animation of the 9 checkbox is also checked. Same things are happenning to the 2nd and 10 checkboxes, 3 and 11 etc. This also works in other way (when i click on the 9 checkbox the 1 checkbox get also checked).I also noticed that when the checkbox in 1st row is checked and i will start to scroll listview up and down the animation of it will start to jump around so that the 2nd checkbox is being shown as checked instead of the 1st one and if i will continue to do so the other ones might get checked instead of 2nd but the toast message wont show up (only with the actual click will). .Could someone explain me what is going on?

Aucun commentaire:

Enregistrer un commentaire