android 2方式数据绑定示例不按照

时间:2016-08-11 23:50:08

标签: java android data-binding android-databinding

我阅读this article关于2路android数据绑定

我注意到代码有点模糊,并决定实现可行的示例并将其放在github上,因此其他人将更容易深入研究它。但按照本文提供的说明,我无法使其正常工作。

在我的例子中,我只有切换器,自定义控件和切换器的主要活动。因此,当我检查主切换器时,它会正确刷新所有内容并按预期工作,但是当我选中/取消选中内部切换器时,它不会影响主视图模型和活动中的任何内容 - 因此双向绑定无法正常工作。

请帮助我找出发生这种情况的原因并解决问题。

代码已修复,现在至少在Android Studio 2.2 beta 1中按预期工作。

Link to the code sample on github

1 个答案:

答案 0 :(得分:2)

几乎正确地连接了一切。在CustomSwitcher中,内部切换器的值发生更改时没有通知。您必须侦听该更改并调用onValChanged回调。

这是你的代码:

dev@dev-ThinkPad-X1-Carbon-3rd:~$ ifconfig|grep "inet addr"
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet addr:192.168.170.50  Bcast:192.168.170.255  Mask:255.255.255.0
dev@dev-ThinkPad-X1-Carbon-3rd:~$ mysql -ubeer -pbeer -h192.168.170.50
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.170.50' (111)
dev@dev-ThinkPad-X1-Carbon-3rd:~$ mysql -ubeer -pbeer -h127.0.0.1
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)
dev@dev-ThinkPad-X1-Carbon-3rd:~$ mysql -ubeer -pbeer -hlocalhost
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

膨胀的绑定不会直接通知自定义切换器,因此您必须侦听该事件。然后你必须打电话给听众。您还必须避免通过确保您没有设置与已存在的值相同的值来反复通知相同值的无限循环。

[client]
port        = 3306
socket      = /var/run/mysqld/mysqld.sock

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

# This was formally known as [safe_mysqld]. Both versions are currently parsed.
[mysqld_safe]
socket      = /var/run/mysqld/mysqld.sock
nice        = 0

[mysqld]
#
# * Basic Settings
#
user        = mysql
pid-file    = /var/run/mysqld/mysqld.pid
socket      = /var/run/mysqld/mysqld.sock
port        = 3306
basedir     = /usr
datadir     = /var/lib/mysql
tmpdir      = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address        = 127.0.0.1
#
# * Fine Tuning
#
key_buffer      = 16M
max_allowed_packet  = 16M
thread_stack        = 192K
thread_cache_size       = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover         = BACKUP
#max_connections        = 100
#table_cache            = 64
#thread_concurrency     = 10
#
# * Query Cache Configuration
#
query_cache_limit   = 1M
query_cache_size        = 16M
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file        = /var/log/mysql/mysql.log
#general_log             = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
#log_slow_queries   = /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
#server-id      = 1
#log_bin            = /var/log/mysql/mysql-bin.log
expire_logs_days    = 10
max_binlog_size         = 100M
#binlog_do_db       = include_database_name
#binlog_ignore_db   = include_database_name
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem



[mysqldump]
quick
quote-names
max_allowed_packet  = 16M

[mysql]
#no-auto-rehash # faster start of mysql but no tab completition

[isamchk]
key_buffer      = 16M

#
# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/