【问题标题】:How to display .txt file in a TextView from ListView onClickItem?如何从 ListView onClickItem 在 TextView 中显示 .txt 文件?
【发布时间】:2017-06-07 09:50:53
【问题描述】:

我正在处理以下内容,但我发现从 ListView 读取 .txt 文件时遇到问题,将其显示在 TextView 中。

代码是:

    ArrayList<String> AcqList = GetFiles("/sdcard/Muse");

    lv.setAdapter(new ArrayAdapter<>(getContext(), 
android.R.layout.simple_list_item_1, AcqList));
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    public void onItemClick(AdapterView<?> parent, View vr, int position, long id) {
    **????**
    }
    private boolean checkResults(String fileName){

    File file = new File("/sdcard/MuSe/"+fileName);

    if(file.exists()) return true;

    else return false;
}

});
return vr;
}

我需要使用读取文件 .txt 的代码完成 ??? 部分,并将其显示在带有 id AcqText 的 TextView 中。在ListView 中,我显示了特定目录中的 .txt 文件列表。

【问题讨论】:

  • 明确地说,我在一个片段中,在该代码下方,我有显示 .txt 文件列表的 ArrayList。

标签: java android listview textview


【解决方案1】:

您应该将这些文件存储在 assets 或 raw 目录中,然后您可以使用 , 从这些文件中获取输入流,

如果使用资产

AssetManager am = context.getAssets();
InputStream is = am.open("yourfilename.txt");

或者如果你使用原始目录,那么,

InputStream is = getResources().openRawResource(R.raw.yourfilename);

【讨论】:

  • 问题是我不想打开一个特定的文件“myfilename.txt”,但我想打开我点击我的应用程序的文件。这和你邀请我做的过程一样吗?
  • 您可以在列表项单击侦听器中使用该代码 sn-p。首先,您必须获取项目 ID,如果匹配所需的值,则相应地显示 .txt 文件。你明白我的意思了吗?
  • 不,抱歉,没听懂。请问你能把它编成代码吗?
猜你喜欢
  • 2015-11-19
  • 1970-01-01
  • 1970-01-01
  • 2014-06-06
  • 2017-04-26
  • 2016-05-11
  • 1970-01-01
  • 2020-06-14
  • 2020-10-08
相关资源
最近更新 更多