gpt4 book ai didi

android - sqlite 查询检索所有不为 null android 的行

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:31:20 27 4
gpt4 key购买 nike

你好,我有两个问题:

首先是背景信息。

根据 android 文档,当查询数据库时,我们必须返回名称为“_id”的列才能与游标适配器一起使用。现在在我的实现中,假设我有 3 列“_id”、“A 列”、“B 列”。

因此,如果我的数据库表中有 8 个条目行,其中 A 列中的前 3 行被填充,B 列中接下来的 5 行被填充。如果我在数据库中查询 B 列并在我的绑定(bind) View 中使用下面的代码,游标将返回所有行,这将导致前 3 个条目为空,最后 5 个项目显示在我的图像 GridView 中。

这是我的绑定(bind) View :

@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder holder = (ViewHolder)view.getTag();
int columnIndex = cursor.getColumnIndexOrThrow(columnName);
final String name = cursor.getString(columnIndex);
while(cursor.moveToNext()){

if(name != null){
Log.d(TAG, "string name in gridimageadapter is " + name);

BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(name);

Bitmap newbitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, false);
if(holder.image != null){
holder.image.setImageBitmap(newbitmap);
}
bitmap.recycle();

}
}
}

所以我的问题是:

  1. 我如何编写 sqlite 查询以返回行不为空的列?我目前的实现是:

    public Cursor retrieveTag(字符串标签){

        String[] condition = {KEY_ID,tag};
    Cursor cursor = db.query(DATABASE_TABLE, condition, null, null, null, null, null);
    return cursor;
    }
  2. 从 BindView 代码中,如果我像这样使用这个条件

    if( !name.equals(null) || !name.equals("")){ }

我在上面得到一个 NullPointerException。这是为什么?..这是测试字符串的正确方法吗?..

希望我的问题很清楚,我们将不胜感激任何帮助。谢谢。

最佳答案

通过使用 Sql 查询,您可以管理没有空值的数据,请参见下面的代码。

Cursor c=db.rawQuery("Select columnB from tablename where columnB IS NOT NULL");

关于android - sqlite 查询检索所有不为 null android 的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11914860/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com