usniyo 2012-10-15 03:27 采纳率: 0%
浏览 3624
已采纳

请教一个关于android ListView的问题

在ListView的Item中有一个Switch控件,当Item过多屏幕装不下时,如果上下滑动使未显示的Item显示,这时开始和结尾的几个Item中的Switch控件会自动改变状态,并响应状态改变的事件。如果Item越多,会自动改变的Switch控件就越多。求解决方法,谢谢!

private class MAdapter extends CursorAdapter
{
    private Context context;
    private LayoutInflater mInflater;

    public MAdapter(Context context, Cursor c)
    {
        super(context, c, true);

        this.context = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent)
    {
        View v = mInflater.inflate(R.layout.main_task_list_item, null);
        return v;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor)
    {
        final MZTask task = new MZTask(cursor);

        // ic
        ImageView ic = (ImageView) view
                .findViewById(R.id.main_list_action_ic);
        ic.setImageResource(task.getIcId());

        // action name
        TextView actionName = (TextView) view
                .findViewById(R.id.main_list_action_name);
        actionName.setText(task.getActionName(context));

        // time
        TextView time = (TextView) view
                .findViewById(R.id.main_list_task_time);
        time.setText(task.getTimeString());

        // repeat
        TextView repeat = (TextView) view
                .findViewById(R.id.main_list_task_repeat);
        repeat.setText(task.getDayStringOfWeek(context));

        // switch view
        final Switch stch = (Switch) view
                .findViewById(R.id.main_list_task_switch);
        stch.setOnCheckedChangeListener(getCheckedChangeListener(task));
        stch.setChecked(task.state());

        // list more
        RelativeLayout icLayout = (RelativeLayout) view
                .findViewById(R.id.main_list_more);
        icLayout.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                popUpMenu(task);
            }
        });

        // ..
        RelativeLayout itemLayout = (RelativeLayout) view
                .findViewById(R.id.main_list_layout_switch);
        itemLayout.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                stch.setChecked(!stch.isChecked());
            }
        });

    }

    public OnCheckedChangeListener getCheckedChangeListener(
            final MZTask task)
    {
        return new OnCheckedChangeListener()
        {
            public void onCheckedChanged(CompoundButton button,
                    boolean change)
            {
                if (change)
                    task.startTask(context, dbAdapter);
                else
                    task.cancelTask(context, dbAdapter);
            }
        };
    }

}
  • 写回答

1条回答 默认 最新

  • usniyo 2012-11-09 02:55
    关注

    由于重用导致的问题,只需在对switch进行操作之前重置状态监听器即可:

    setOnCheckedChangeListener(null);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的