带有复选框的CustomListview会在滚动时自动检查它们

时间:2017-04-04 13:03:21

标签: java android xamarin

这是我的班级代码:

class ExtrasPreviewClass
{
    [PrimaryKey]

    public int id { get; set; }
    public string ExtrasName { get; set; }
    public string ExtrasCheckBox { get; set; }
    public string ExtrasPrice { get; set; }

}

这是我的xml文件

 <?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="fill_parent"
android:minWidth="25px"
android:weightSum="100"
android:minHeight="25px">
<RelativeLayout
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/relativeLayout1">
    <TextView
        android:text="Εξτρα"
        android:layout_width="200dp"
        android:layout_height="30dp"
        android:layout_alignParentLeft="true"
        android:id="@+id/txtExtrasName"
        android:textSize="20sp" />
    <TextView
        android:text="Τιμή"
        android:layout_width="50dp"
        android:layout_height="30dp"
        android:layout_marginLeft="260dp"
        android:id="@+id/txtExtrasPrice"
        android:textSize="20sp" />
    <TextView
        android:text="ID"
        android:layout_width="50dp"
        android:layout_height="30dp"
        android:layout_marginLeft="450dp"
        android:id="@+id/txtExtrasID"
        android:textSize="20sp" />
    <CheckBox
        android:layout_width="50dp"
        android:layout_alignParentRight="true"
        android:layout_height="30dp"
        android:id="@+id/txtExtrasCheckBox"
        android:textSize="20sp"
        android:focusable="false"
        android:focusableInTouchMode="false" />
   </RelativeLayout>
  </LinearLayout>

这是我的自定义列表视图适配器

namespace WiOrderAndroid
{
class MyListViewAdapterExtras : BaseAdapter<ExtrasPreviewClass>
{
    public List<ExtrasPreviewClass> mitems;
    private Context mContext;
    public MyListViewAdapterExtras(Context context, List<ExtrasPreviewClass> items)
    {
        mitems = items;
        mContext = context;

    }
    public override int Count
    {
        get
        {
            return mitems.Count;
        }
    }
    public override long GetItemId(int position)
    {
        return position;
    }
    public override ExtrasPreviewClass this[int position]
    {
        get
        {
            return mitems[position];
        }

    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        string dpPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "student.db3");
        var db = new SQLiteConnection(dpPath);
        db.CreateTable<ExtrasPreviewClass>();



        View row = convertView;
        if (row == null)
        {
            row = LayoutInflater.From(mContext).Inflate(Resource.Layout.ExtrasPreview, null, false);

        }



        TextView txtExtrasName = row.FindViewById<TextView>(Resource.Id.txtExtrasName);
        txtExtrasName.Text = mitems[position].ExtrasName;
        CheckBox txtExtrasCheckBox = row.FindViewById<CheckBox>(Resource.Id.txtExtrasCheckBox);
        txtExtrasCheckBox.Text =mitems[position].ExtrasCheckBox;
        TextView txtExtrasID = row.FindViewById<TextView>(Resource.Id.txtExtrasID);
        txtExtrasID.Text = Convert.ToString(mitems[position].id);
        TextView txtExtrasPrice = row.FindViewById<TextView>(Resource.Id.txtExtrasPrice);
        txtExtrasPrice.Text = Convert.ToString(mitems[position].ExtrasPrice);


        db.DeleteAll<ExtrasPreviewClass>();
        Connection.Extras = "";


        txtExtrasCheckBox.Click += delegate
        {
            if (txtExtrasCheckBox.Checked == true)
            {
                ExtrasPreviewClass tbl = new ExtrasPreviewClass();

                tbl.ExtrasName = txtExtrasName.Text;
                tbl.id = Convert.ToInt32(txtExtrasID.Text);
                db.Insert(tbl);

                double a, b, c;
                a = double.Parse(Connection.RetailPrice);
                b = double.Parse(txtExtrasPrice.Text);
                c = a + b;
                Connection.RetailPrice = c.ToString();


            }
            else
            {

                var data = db.Table<ExtrasPreviewClass>();
                int idvalue = Convert.ToInt32(txtExtrasID.Text);

                var data1 = data.Where(x => x.id == idvalue).FirstOrDefault();

                if (data1.id != null)
                {
                    db.Delete(data1);


                }
                double a, b, c;
                a = double.Parse(Connection.RetailPrice);
                b = double.Parse(txtExtrasPrice.Text);
                c = a - b;
                Connection.RetailPrice = c.ToString();
            }
        };

        return row;

所以问题是,如果我将检查我的任何复选框,我将向下滚动,我会看到复选框,他们已经自动检查。我也在使用sqlite,它在我的表中插入了重复的值。为什么会这样?

1 个答案:

答案 0 :(得分:1)

您需要在boolean类中添加一个ExtrasPreviewClass变量,并根据列表中所选和未选中项目的位置维护其状态。

并在Checkbox中设置该变量的值。并更新特定列表项的bean类中的标志值,而不仅仅更新CheckBox

http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html

见下文getView功能

holder.name.setChecked(country.isSelected());

name是Checkbox Object,标志值取自作为数据列表项的国家/地区对象。

当onClick listenar运行时

  country.setSelected(cb.isChecked());

标志从数据列表中的ChekBox值重置。

@Override
  public View getView(int position, View convertView, ViewGroup parent) {

   ViewHolder holder = null;
   Log.v("ConvertView", String.valueOf(position));

   if (convertView == null) {
   LayoutInflater vi = (LayoutInflater)getSystemService(
     Context.LAYOUT_INFLATER_SERVICE);
   convertView = vi.inflate(R.layout.country_info, null);

   holder = new ViewHolder();
   holder.code = (TextView) convertView.findViewById(R.id.code);
   holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
   convertView.setTag(holder);

    holder.name.setOnClickListener( new View.OnClickListener() {  
     public void onClick(View v) {  
      CheckBox cb = (CheckBox) v ;  
      Country country = (Country) cb.getTag();  
      Toast.makeText(getApplicationContext(),
       "Clicked on Checkbox: " + cb.getText() +
       " is " + cb.isChecked(), 
       Toast.LENGTH_LONG).show();
      country.setSelected(cb.isChecked());
     }  
    });  
   } 
   else {
    holder = (ViewHolder) convertView.getTag();
   }

   Country country = countryList.get(position);
   holder.code.setText(" (" +  country.getCode() + ")");
   holder.name.setText(country.getName());
   holder.name.setChecked(country.isSelected());
   holder.name.setTag(country);

   return convertView;

  }

 }