动态地从sqlite的一个片段设置复选框 [英] Setting checkbox dynamically from sqlite in a Fragment

查看:219
本文介绍了动态地从sqlite的一个片段设置复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法搞清楚存储和显示复选框在ListView的最佳方式。

I'm having trouble figuring out the best way to store and display checkboxes in a listview.

现在我有code。在getView方法:

Right now I have the code in the getView method:

@Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view = convertView;
            //final ViewHolder holder;
            if (convertView == null) {
                convertView = getActivity().getLayoutInflater().inflate(R.layout.grid_item, null);
            }
            ImageView img = (ImageView) convertView.findViewById(R.id.grid_item_img);

            cb = (CheckBox) convertView.findViewById(R.id.chk_box_griditem);


            if(photos.get(position).getIshidden()){
                cb.setChecked(false);
            }else{
                cb.setChecked(true);
            }


            cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    MySQLiteHelper db = MySQLiteHelper.getInstance(getActivity());
                    Log.d("checkbox", "status: " + photos.get(position).getIshidden());

                    if (isChecked) {
                        photos.get(position).setIshidden(true);
                        cb.setChecked(true);
                        Log.d("checkbox", "isChecked");
                        db.updatePhoto(photos.get(position));

                    } else {
                        photos.get(position).setIshidden(false);
                        cb.setChecked(false);
                        Log.d("checkbox", "isCheckedelse");
                        db.updatePhoto(photos.get(position));
                    }

                }
            });

            imageLoader.displayImage(imgUrls[position], img, options, animateFirstListener);

            //img.setImageResource(mImgRes);
            return convertView;
        }

该数据库辅助拍照的对象作为参数,并更新该行,如果它存在。所以现在我更新当前照片的对象 isHidden()来是真或假,那么更新的对象传递给分贝帮手。

The db helper takes a photo object as an argument and updates the row if it exists. So right now I update the current photo object isHidden() to be true or false then pass the updated object to db helper.

的物理数据库似乎是正确地更新。然而,当复选框状态被设置出现问题。该复选框似乎是随机设置为选中或未选中。

The physical database seems to be updating correctly. However a problem occurs when the checkboxes state is being set. The checkboxes seem to be randomly set as checked or unchecked.

另外我觉得在getView这样做,这是CPU的贪婪,但我不知道怎么回事,要做到这一点。

Also I feel like doing this in the getView is cpu greedy but am not sure how else to do this.

推荐答案

首先删除cb.setOnCheckedChangeListener ...你不需要每次getView是called.you已经写设置OnCheckedChangeListener下面code这将设置复选框状态(选中或取消选中)时,列表视图负荷。

First remove cb.setOnCheckedChangeListener...You do not need to set the OnCheckedChangeListener every time getView is called.you have already written the below code which will set check-box state(checked or unchecked) when list-view loads.

if(photos.get(position).getIshidden())
cb.setChecked(false);
else
cb.setChecked(true);

现在设置onItemClick监听器下面的列表视图:

Now set onItemClick listener as below on list view :

    mListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) 
        {

            MySQLiteHelper db = MySQLiteHelper.getInstance(getActivity());
            Log.d("checkbox", "status: " + photos.get(position).getIshidden());

            if(photos.get(position).getIshidden())
            {
                ((CheckBox)arg0.getChildAt(position).findViewById(R.id.chk_box_griditem)).setChecked(true);
                photos.get(position).setIshidden(false);
                db.updatePhoto(photos.get(position));

            }   
            else
            {
                ((CheckBox)arg0.getChildAt(position).findViewById(R.id.chk_box_griditem)).setChecked(false);
                photos.get(position).setIshidden(true);
                db.updatePhoto(photos.get(position));

            }

        }


    });

通过这种方式,你可以选中或取消选中该复选框,并在同一时间复选框状态(选中或取消选中)将被保存在数据库中。

by that way you can check or uncheck the check-box and at the same time the check-box state(checked or unchecked) will be saved in your database.

我希望它会工作。如果不让我知道..:)

I hope it will work.If not let me know..:)

这篇关于动态地从sqlite的一个片段设置复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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