在CursorAdapter的ListView的复选框监听器 [英] Checkbox listener in ListView with CursorAdapter

查看:128
本文介绍了在CursorAdapter的ListView的复选框监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我比这个帖子非常类似的一个问题。在我的ListView中的每一行我有一个听众的复选框。监听器更新DATABSE一行。

I have a problem very similar than this post. In every row of my ListView I have a checkbox with a listener. The Listener update databse row.

@Override
public void bindView(View v, Context context, Cursor c) {
    TextView tvA = (TextView) v.findViewById(R.id.adi_tv_activity);
    CheckBox cb = (CheckBox) v.findViewById(R.id.adi_cbox);
    tvA.setText(c.getString(c.getColumnIndex("name")));
    final long id = c.getLong(c.getColumnIndex("_id"));
    final Context ctx = context;
    cb.setOnCheckedChangeListener(new OnCheckedChangeListener(){
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            ObjDBF dbf = new ObjDBF(ctx); //Object thet update db
            if(isChecked) {
                dbf.tbActivitiesUpdateState(id, true);  
            } else {   
                dbf.tbActivitiesUpdateState(id, false);     
            }
        }               
    });
    if (c.getBoolean(c.getColumnIndex("state"))) {
        cb.setChecked(true);
    } else {
        cb.setChecked(false);
    }
}

我有2个问题。


  1. 滚动问题。移动我失去了行的复选框的状态,从屏幕上消失的列表中。

  2. 任何时候,我用setChecked的listenr被称为导致新的数据库更新。

有关risolving滚动的问题,我想用adapter.changeCursor我每次更新数据库的时间,但对于第二个问题,它导致一个循环。

For risolving scrolling problem I wanted to use adapter.changeCursor every time I update db, but for second problem it cause a loop.

我也尝试使用使用数组在上面链接后的第二个答案,但它使用的方法getView OD适配器,我有一个CursorAdapter的和做NewView的和bindView工作

I try also to use a use an Array as in the second answer of above linked post, but it use the method getView od adapter, I have a cursorAdapter and do work in newView and bindView

我该如何解决这个问题呢?

How can I solve the problem?

编辑:auselen解决方案的工作,但它创建和销毁了大量的听众,还有一个其他更有效的解决方案。

edit: auselen solution works but it creates and destroys a lot of listeners, there is an other more efficient solution?

推荐答案

要避免触发听者回调,您应该注销现有的由 cb.setOnCheckedChangeListener(空)然后设置 cb.setChecked(c.getBoolean(c.getColumnIndex(国家)))然后 cb.setOnCheckedChangeListener()试。

To avoid triggering callbacks on listener, you should unregister existing one by cb.setOnCheckedChangeListener(null) then set cb.setChecked(c.getBoolean(c.getColumnIndex("state"))) then cb.setOnCheckedChangeListener() again.

不过你应该考虑使用一个更好的方式, OnCheckedChangeListener ,因为你最终会创造超过所需的听众。

However you should think of a better way of using that OnCheckedChangeListener, since you would end up creating listeners more than needed.

这篇关于在CursorAdapter的ListView的复选框监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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