【问题标题】:Detecting an external file in ListView在 ListView 中检测外部文件
【发布时间】:2019-01-22 21:10:15
【问题描述】:

我正在使用 listview 来填充可供下载的讲座列表(368 个讲座)。

它正在显示无法离线使用的讲座的“下载”文本。

对于可用的讲座,我正在进行外部文件检查,然后更新 “下载”文本到“离线”,但在 ListView 中无法正常工作 并且值是重复的。

这是我的 getView 方法代码

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;

        if (convertView == null) {
            view = inflater.inflate(R.layout.customlistview, null);
        } else {
            view = convertView;
        }

        TextView number = view.findViewById(R.id.lec_number);
        TextView title = view.findViewById(R.id.lec_title);
        TextView url = view.findViewById(R.id.url);
        TextView filename = view.findViewById(R.id.filename);
        // Textview  I want to update, contains "Download" text
        TextView download_text = view.findViewById(R.id.download_text);

        HashMap<String, String> mList = new HashMap<>();
        mList = List.get(position);

        number.setText(mList.get("number"));
        title.setText(mList.get("title"));
        url.setText(mList.get("url"));
        filename.setText(mList.get("file"));


        // File check that I'm doing
        File externalFile = new File(Environment.getExternalStorageDirectory() + "/Lectures By Sarfaraz A Shah Sb/" + mList.get("file"));
        if(externalFile.exists()) {
            download_text.setText("Available");
        }


        return view;


    }

这是我的输出:

Screenshot of ListView

ListView (Continued)

【问题讨论】:

  • 如何以及在何处填充 ListView?您如何遍历数据以确定您更改文本的 Listview 项目?您的 Listview 适配器在哪里?您需要提供更多信息如何将数据传递给 Listview 以及如何遍历它。
  • @skryshtafovych 问题已解决,伙计。我切换到recyclerview。

标签: android android-studio listview


【解决方案1】:

Android 回收视图。发生这种情况时,开发人员必须小心,以免回收视图中的垃圾不会继续。

getView() 中的convertView 参数不为空时,它可能具有先前设置的值。在这种情况下,您必须在显示视图之前清除它们,否则将显示垃圾。一般来说,您应该始终为getView() 中的每个视图设置值。

例如,您可以这样做:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    // ...

    if (externalFile.exists()) {
        download_text.setText("Available");
    } else {
        // Set any text you want to show,
        // or just clear the view like this.
        download_text.setText("");
    }

    return view;
}

【讨论】:

  • 感谢您提及垃圾。我切换到 RecyclerView,现在一切正常。
猜你喜欢
  • 1970-01-01
  • 2016-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 2017-02-27
  • 1970-01-01
相关资源
最近更新 更多