【问题标题】:Displaying a unicode character from SQLite database显示 SQLite 数据库中的 unicode 字符
【发布时间】:2012-12-18 17:54:10
【问题描述】:

我有一个由 SQLite 数据库中的数据填充的 listView。我的一个字段是一个带有完整圆圈的 unicode 字符的标记:\u25cf 或空心圆圈:\u25cb。当我在文本字段中使用硬编码字符串时,这两个字符都能正确显示。但是,在我的 listView 中,我看到的是文本编码而不是字符。

有谁知道这是为什么...以及如何显示 unicode 字符?

谢谢。

更新:

插入代码是

private void loadRecords() throws IOException {
        final Resources resources = mContext.getResources();
        InputStream inputStream = resources.openRawResource(R.raw.records);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        try {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] strings = TextUtils.split(line, ",");
                if (strings.length <2) continue;
                long id = addRecord(strings[0].trim(),strings[1].trim(),strings[2].trim(),strings[3].trim(),
                        strings[4].trim(),strings[5].trim(),strings[6].trim());
            }
        } finally {
            reader.close();
        }
    }

资源是一个 csv 文件,其中一行是例如

primaryKey,name,surname,address,phone,email,\u25CB

【问题讨论】:

  • 因为你没有正确插入到数据库中。
  • 您“看到的是文本编码而不是字符”是什么意思?具体是如何显示的?
  • 我的意思是lisView中的元素显示的是“\u25CB”,而不是显示一个开放的圆圈。
  • @njzk2 ... 那么将其插入数据库的正确方法是什么?
  • 取决于。发布您的插入代码

标签: android sqlite unicode


【解决方案1】:

在您的文本文件中,\u25CB 是六个字符。

要正确使用 Unicode 字符,请将它们直接放入文件中:

primaryKey,name,surname,address,phone,email,●

但是,您必须确保 .csv 文件与您的InputStreamReader 使用的编码相同(设置其构造函数的第二个参数)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多