-1

有人可以告诉我为什么奇怪的字符不会出现在 Android listView 中吗?

我有一个文本文件

在此处输入图像描述

我得到了这个

在此处输入图像描述

这是我的代码

    ArrayList<String> list = new ArrayList<String>();
    InputStream inStream = getResources().openRawResource(R.raw.emoticons);
    InputStreamReader inputReader = null;


    if (inStream != null)
    {

        try {
            inputReader = new InputStreamReader(inStream, "UTF-8");
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }

        int c, i=0;
        //int i=0;
        char [] cb = new char[1];
        byte [] buf = new byte[100];
        String line = "";

        int ble = -1;

        try {
            ble = inputReader.read(cb, 0, 1);
        }catch (IOException e){
            e.printStackTrace();
        }

        while (ble > -1)
        {

            if(cb[0] == '\r' || cb[0] == '\n')
            {
                try{
                    line = new String(buf, 0, i, "UTF-8");
                }catch (UnsupportedEncodingException e){
                    e.printStackTrace();
                }

                i=0;
                list.add(line);
            }

            else
            {

                buf[i++] = (byte) cb[0];
            }

            try {
                ble = inputReader.read(cb, 0, 1);
            }catch (IOException e){
                e.printStackTrace();
            }

        }
    }



    String[] emoticons = list.toArray(new String[list.size()]);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, emoticons);
    listview.setAdapter(adapter);
4

1 回答 1

0

看起来您正在尝试逐行读取文件。一种方法是使用BufferedReader类:

ArrayList<String> list = new ArrayList<String>();
InputStream inStream = getResources().openRawResource(R.raw.emoticons);
BufferedReader inputReader = null;

if (inStream != null)
{
    try {
        inputReader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
        String line;
        while ((line = inputReader.readLine()) != null) {
            list.add(line);
        }
    // handle exceptions..
    } finally {
        if (inputReader != null) inputReader.close();
    }
}

String[] emoticons = list.toArray(new String[list.size()]);
于 2013-09-22T14:16:19.550 回答