Android-在应用程序启动时检查自定义listView项 [英] Android - Check custom listView items on the start of the App

查看:45
本文介绍了Android-在应用程序启动时检查自定义listView项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义Listview,每行包含一个textview和一个复选框.我将所选行的textview的值(或文本)保存在名为usercoin的公共列表中.每次用户打开应用程序时,列表usercoin都会包含他的textview所选项目的文本,而我正在使用SQLite进行操作.问题是我想重新检查usaer先前选择的项目,这些项目在usercoin列表中可用.我无法这样做.

I have a custom Listview, each row contains one textviewand 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; }
    }
}
}

例如,当用户运行应用程序并从listview中选择FranceGermany时,下次他打开应用程序时,usercoin列表将包含FranceGermany.现在的问题是,如何检查与listview中的那些值相对应的复选框.我试图通过在MyAdapter : BaseAdapter类中包含以下代码来做到这一点:

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();
            }

但是,当执行此代码时,将检查先前检查过的项目,还会检查用户以前未检查过的其他一些项目.那么,如何在应用程序启动时检查Listview中先前检查的项目?请帮助我找到解决方案.

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.

推荐答案

我已经将数据(useritems)存储在数据库和内存中.

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.

我在应用程序中添加了Application类.

And I have add Application class in the app.

我已经更新了演示. 此处是gif.

I have update the demo. Here is gif.

这篇关于Android-在应用程序启动时检查自定义listView项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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