从资产/原始文件夹中读取多个文本文件(.txt)

时间:2016-04-23 06:07:15

标签: android listview android-studio

我的原始文件夹中有多个文本文件,我想根据列表视图中的所选项目逐个显示它们。

例如,我已经选择了" TwinkleTwinkle"从列表视图中,显示的文本文件必须是TwinkleTwinkle.txt

我已经阅读了原始文件夹文件,其中只有一个文本文件。但是不知道从那里读取多个文件。请帮帮我

这是我读取一个文本文件原始文件夹的代码:

try {
        Resources res = getResources();
        InputStream in_s = res.openRawResource(R.raw.heading);

        byte[] b = new byte[in_s.available()];
        in_s.read(b);
        heading.add(new String(b));

        Log.e("Heading", heading.toString() + "  " + heading.size());

        HeadingAdapter myadapter = new HeadingAdapter(context, heading);
        list.setAdapter(myadapter);
    } catch (Exception e) {
     e.printStackTrace();

    }

heading.txt:

AAAAAAAAA

BBBBBBBBB

CCCCCCCCC

DDDDDDDDD

在这里,我需要在listview中显示它。但是所有数据都被视为此处的单项。我的意思是,我只有一个项目有上述4个数据。

1 个答案:

答案 0 :(得分:0)

您的读取文件的代码是错误的:请检查此代码

try {


    InputStream is = this.getResources().openRawResource(R.raw.simpletext);
    byte[] buffer = new byte[is.available()];
    while (is.read(buffer) != -1);
    String jsontext = new String(buffer);



    tv.setText(jsontext);

} catch (Exception e) {

    Log.e(TAG, ""+e.toString());
}
相关问题