The content of the adapter has changed but ListView did not receive a notification

-------------------------


由于考虑到数据库的安全性,不被轻易SQL注入,执行查询语句时,一般不使用直接拼接的语句,而是使用参数传递的方法。然后在使用参数传递的方法中时,发现当使用like方式查询数据时,很容易出现一个问题。

错误案例:

复制代码 代码如下:
String myname = "abc";
String sql = "select * from mytable where name like '?%'";
Cursor cursor = db.rawQuery(sql, new String[]{myname};

运行提示如下错误:
复制代码 代码如下:
java.lang.IllegalArgumentException: Cannot bind argument at index 1 because the index is out of range. The statement has 0 parameters.

根据错误提示可知,sql语句中的?号没有被识别出来,从而new String[]{myname}没法替代sql中的?号。?号没有被识别出来的原因估计是?号外有单引号,但是在sql中like语句的值和%号需要用引号围着。

为了解决sql中?号无法识别,必须去掉?号外的引号,那么%号也需要去掉。所以,得在后面代替?号的参数中添加上%号。

所以,正确的案例如下:

复制代码 代码如下:

String myname = "abc";
String sql = "select * from mytable where name like ?";
Cursor cursor = db.rawQuery(sql, new String[]{myname+"%"};

可能有人会问为什么不用添加引号,因为参数代替?号时,自动以字符串的形式代替的。


------------------------------------------


sqlite3中没有top的语法结构,不过相关的语法能实现跟top语法相同的功能,感兴趣的你可以参考下,希望可以帮助到你
其实,在sqlite3中没有top的语法结构,但在sqlite3中有相关的语法能实现跟top语法相同的功能,sqlite3 sql是用limit这样的语法来实现的;

如:
复制代码 代码如下:

select * from table where name='_安静ゝ' order by id limit 0,10;

这个效果就相当于select top 10 * from table where name='_安静ゝ';

如果还有更精确的:
复制代码 代码如下:

select * from table where name='_安静ゝ' order by date desc,id limit 0,10;



---------------------------------------------------------------------------------------

error

The content of the adapter has changed but ListView did not receive a notification




其实我在listview的adapter添加完数据后,使用了handler去调用datper.notifyDataSetChanged();来通知listview显示变化结果;

虽然自己很确定没有多线程操作,但是有人说listview本来就是线程不安全的,这个不关心了,看了国外一个开发者的方法很简单:

  1. ListView.requestLayout();  
  2. Adatper.notifyDataSetChanged();  

在你adpater更新前,调用listview的requestLayout(),这样做无非就是拟补数据数量不一致导致报错,虽然一个解决的好办法。

但是实际上用的时候我发现也会出问题,想了想最彻底的解决办法:

把  listview的adapter数据更新和dapter.notifyDataSetChanged()必须同时放到单独一个线程里,报错基本是都是这个原因,有人把adapter里的数据更新了,但是 dapter.notifyDataSetChanged()     放到一个单独线程去更新,结果出现notifyDataSetChanged更新同步的问题



---------------------------------------------------------------------------------------

android自定义主题

猜你喜欢

转载自blog.csdn.net/u012457196/article/details/40477897