简体   繁体   中英

Android - Check custom listView items on the start of the App

I have a custom Listview , each row contains one textview and one checkbox. I am saving the value (or the text) of the selected row's textview in a public list named usercoin . Each time the user opens the app, the list usercoin will contain the text of the his textview selected items, and I am doing that using SQLite . The problem is I want to re-check the items which the usaer have previously selected which are available in the usercoin list. I am not able to do so.

MyActivity.cs

    ListView mListView;
    MyAdapter adapter;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);
        mListView = FindViewById<ListView>(Resource.Id.listview);
        List<TableList> list = new List<TableList>();

        list.Add(new TableList("Germany",false));
        list.Add(new TableList("France", false));
        list.Add(new TableList("Finland", false));
        list.Add(new TableList("Germany", false));
        list.Add(new TableList("France", false));
        list.Add(new TableList("Germany", false));
        list.Add(new TableList("France", false));
        list.Add(new TableList("Finland", false));

        adapter = new MyAdapter(this, list);
        mListView.Adapter = adapter;
        mListView.ItemClick += MListView_ItemClick;
    }

    private void MListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
    {
        var t = list[e.Position];
        string selected = t.name;
        var ll = e.View as LinearLayout;
        var cb = ll.GetChildAt(2) as CheckBox;
        if (cb.Checked)
        {
            cb.Checked = false;
            adapter.changeState((int)cb.Tag, false);
        }
        else
        {
            cb.Checked = true;
            adapter.changeState((int)cb.Tag, true);
        }
    }



    class MyAdapter : BaseAdapter
    {
        Context mContext;
        List<TableList> mitems;
        public MyAdapter(Context context, List<TableList> list)
        {
            this.mContext = context;
            this.mitems = list;

        }
        public override int Count
        {
            get
            {
                return mitems.Count;
            }
        }

        public override Java.Lang.Object GetItem(int position)
        {
            return mitems[position];
        }

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

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            DataViewHolder holder = null;
            if (convertView == null)
            {
                convertView = LayoutInflater.From(mContext).Inflate(Resource.Layout.CoinList, null, false);
                holder = new DataViewHolder();
                holder.tv = convertView.FindViewById<TextView>(Resource.Id.CoinName);
                holder.iv = convertView.FindViewById<ImageView>(Resource.Id.imageView1);
                holder.cb = convertView.FindViewById<CheckBox>(Resource.Id.checkBox1);
                convertView.Tag = holder;
            }
            else
            {
                holder = convertView.Tag as DataViewHolder;

            }
            holder.cb.Tag = position;

            holder.tv.Text = mitems[position].Name;
            holder.cb.Focusable = false;
            holder.cb.Checked = mitems[position].bl;
            holder.iv.SetImageResource(Resource.Drawable.dapao);
            holder.cb.CheckedChange += Cb_CheckedChange;
            return convertView;

        }

        private void Cb_CheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
        {
            var cb = sender as CheckBox;
            if (e.IsChecked && !mitems[(int)cb.Tag].bl)
            {
                mitems[(int)cb.Tag].bl = true;
                this.NotifyDataSetChanged();
            }
            else if (!e.IsChecked && mitems[(int)cb.Tag].bl)
            {
                mitems[(int)cb.Tag].bl = false;
                this.NotifyDataSetChanged();
            }
        }

        internal void changeState(int tag, bool v)
        {
            mitems[tag].bl = v;
            this.NotifyDataSetChanged();
        }
    }

    public class DataViewHolder : Java.Lang.Object
    {
        public ImageView iv { get; set; }
        public TextView tv { get; set; }
        public CheckBox cb { get; set; }

    }
    public class TableList : Java.Lang.Object
    {
        private string v;



        public TableList(string name, bool b)
        {
            this.Name = name;
            this.bl = b;
        }
        public string Name { get; set; }
        public bool bl { get; set; }
    }
}
}

For example, when the user run the app and select France and Germany from the listview , next time he opens the app, the usercoin list will contain France and Germany . Now the question is how can I check the checkboxes corresponding to those values in the listview . I have tried to do so by including this code in MyAdapter : BaseAdapter class:

if (Class1.usercoin.Contains(item.CoinAbr))
            {
                Class1.adapter[(int)holder.cb.Tag].bl = true;
                this.NotifyDataSetChanged();
            }

But when this code get executed, the previously checked items are checked plus some other items which the user haven't checked previously are also checked. So how can I check the previously checked items in the Listview on the app start ? Please help me to find a solution.

I have stored the data( useritems ) in both DataBase and memory.

If your app is killed by system or user, you can restore the data from DataBase.

If your app isn't killed by system or user, but user jump to other activity, when he back to this activity, you can use memory to restore the data.

About DataBase, I am use SQLite.Net . I am using DBHelper to operation the DataBase.

And I have add Application class in the app.

I have update the demo . Here is gif.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM