【问题标题】:Setting checkbox dynamically from sqlite in a Fragment从片段中的sqlite动态设置复选框
【发布时间】:2013-09-14 14:20:10
【问题描述】:

我无法找出在列表视图中存储和显示复选框的最佳方式。

现在我在 getView 方法中有代码:

@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;
        }

db 助手将照片对象作为参数并更新该行(如果存在)。所以现在我将当前的照片对象 isHidden() 更新为 true 或 false,然后将更新的对象传递给 db helper。

物理数据库似乎正在正确更新。但是,在设置复选框状态时会出现问题。复选框似乎被随机设置为选中或未选中。

我也觉得在 getView 中这样做是 cpu 贪婪的,但我不知道该怎么做。

【问题讨论】:

  • 您不必在每次调用 getView 时都设置 OnCheckedChangeListener。每个复选框一个就足够了。 -> 我明白了,你需要一个新的监听器,因为你引用了监听器中的位置。
  • 这个getView 方法属于哪个类?到适配器?很高兴知道您的目标是什么。photos 对象是什么?
  • 它来自自定义列表视图适配器private class GridAdapter extends BaseAdapter
  • 好的,嗯……为什么要在监听器中设置复选框? cb.setChecked(true); 已经设置好了,还是我错过了什么?

标签: android sqlite listview checkbox android-fragments


【解决方案1】:

更改然后记录。 OnCheckedChange 您可以不使用 if 语句而只使用布尔值。您不需要在 checkedchanged 侦听器中设置复选框的值。在 onResume 中初始化 db,这样它就可以不知道用户点击了多少次,并且 db 初始化会减慢速度。

cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                    photos.get(position).setIshidden(isChecked);
                    db.updatePhoto(photos.get(position));
                    Log.d("checkbox", isChecked.toString());
            }
        });



@Override
protected void onResume() {
    if (db==null) {
        db = MySQLiteHelper.getInstance(getActivity());
    }
}

【讨论】:

    【解决方案2】:

    首先移除 cb.setOnCheckedChangeListener...你不需要在每次调用 getView 时都设置 OnCheckedChangeListener。你已经编写了下面的代码来设置复选框状态(选中或未选中) ) 加载列表视图时。

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

    现在在列表视图中设置 onItemClick 监听器如下:

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

    通过这种方式,您可以选中或取消选中复选框,同时复选框状态(选中或未选中)将保存在您的数据库中。

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

    【讨论】:

      【解决方案3】:

      1) 不要在监听器中设置复选框

      2) 仅当 convertView 为空时(即膨胀时),才在复选框中附加侦听器。当您从转换视图获取视图时,它已经设置了侦听器。

      【讨论】:

        【解决方案4】:

        在初始化复选框时,如果图像被隐藏,则将 Check 状态设置为 false。

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

        但在侦听器中,您将图像设置为在选中复选框时隐藏。

        if (isChecked) {
            photos.get(position).setIshidden(true);
        ...
        

        这实际上是相反的。你可能对此感到不安。 并且您不必在侦听器中再次设置选中状态。

        所以您在侦听器中的代码可能如下所示:

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

        【讨论】:

          猜你喜欢
          • 2018-12-31
          • 2015-11-21
          • 1970-01-01
          • 1970-01-01
          • 2011-10-25
          • 1970-01-01
          • 2011-10-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多