java - ListView 未从数组填充

标签 java android listview

我需要用目录中的文件填充listview。我可以将文件放入数组中,但似乎无法将它们放入 listview 中。这是我的代码;我哪里出错了?

private void getDir(final String dirPath)
{
    myPath.setText("Location: " + dirPath);
    item = new ArrayList<String>();
    path = new ArrayList<String>();
    File f = new File(String.valueOf(dirPath));
    final File[] files = f.listFiles();
    myList = (ListView)findViewById(android.R.id.list);


    if(!dirPath.equals(root))
    {
        item.add(root);
        path.add(root);
        item.add("../");
        path.add(f.getParent());
    }

    if (files != null) {

        for (int i = 0; i < files.length; i++) {
            File file = files[i];

            // For testing only ---
            System.out.println("Files --  "+files[i]);
            System.out.println("Files --  "+file);
            // End testing ^^^^^


            if (!file.isHidden() && file.canRead()) {
                path.add(file.getPath());
                if (file.isDirectory()) {
                    item.add(file.getName() + "/");
                }
                else {
                    item.add(file.getName());

                }
            }
        }

        ArrayAdapter<String> fileList =
                new ArrayAdapter<String>(this, R.layout.activity_sendsave, android.R.id.list);
        setListAdapter(fileList);


    }
    else {
        Toast.makeText(this,"No Files",Toast.LENGTH_LONG).show();
    }

}

这是我的 xml 文件。

<ListView
    android:id = "@android:id/list"
    android:layout_width = "match_parent"
    android:layout_height = "wrap_content"
    android:layout_alignParentTop = "true"
    android:layout_alignParentLeft = "true"
    android:layout_alignParentStart = "true"
    android:layout_above = "@+id/btnSendSave"
    android:clickable = "true"
    android:focusable = "true"
    android:visibility = "visible" />

我需要让你知道,一切似乎都在做它应该做的事情,但 listview 不会填充。

在代码中,有 system.out.println(files[i]) 显示文件正在放入数组中。

最佳答案

您的ArrayAdapter构造函数有多个问题:

ArrayAdapter<String> fileList =
            new ArrayAdapter<String>(this, R.layout.activity_sendsave, android.R.id.list);

首先,您没有传入 item,因此适配器将为空。

其次,android.R.id.list 不太可能是您用于 ListView 的行布局中 TextView 的 ID .

第三,我怀疑 R.layout.activity_sendsave 实际上并不是您想要的每个 ListView 行的布局。

试试这个:

ArrayAdapter<String> fileList =
            new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, item);

它为您的 ListView 使用内置的简单行布局,并提供您的单词列表。

以下是使用此方法的示例 Activity ,来自 this sample project :

/***
  Copyright (c) 2008-2012 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain   a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    https://commonsware.com/Android
*/

package com.commonsware.android.list;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ListViewDemo extends ListActivity {
  private TextView selection;
  private static final String[] items={"lorem", "ipsum", "dolor",
          "sit", "amet",
          "consectetuer", "adipiscing", "elit", "morbi", "vel",
          "ligula", "vitae", "arcu", "aliquet", "mollis",
          "etiam", "vel", "erat", "placerat", "ante",
          "porttitor", "sodales", "pellentesque", "augue", "purus"};

  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    setListAdapter(new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1,
                        items));
    selection=(TextView)findViewById(R.id.selection);
  }

  @Override
  public void onListItemClick(ListView parent, View v, int position,
                                long id) {
    selection.setText(items[position]);
  }
}

关于java - ListView 未从数组填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35929852/

相关文章:

Java ArrayList<T>.转换

android - EditText.setText() 在 Fragment 中不起作用

java - 将毫秒转换为 24 小时格式?

android - 同步ViewPager中的所有ListView

Android - 如何以编程方式点击 ListView 项目

android - 添加和删​​除此 listView 的标题

java - JTable 单元格日期呈现器

java - Cassandra Query执行时间分析

java - 如何打印对象的实现类

android - 翻译动画在使用 XML 定义时完美运行,并且仅通过代码完美运行一次 - Android