【问题标题】:How to display arraylist of files in ListView如何在 ListView 中显示文件的数组列表
【发布时间】:2014-04-01 14:52:20
【问题描述】:

我正在使用此函数在TextView 中显示ArrayList,这将显示SDCard 中的所有目录和文件,但我想改用ListView。我应该怎么做才能在ListView中显示sdcard的目录和所有文件?

方法getfile读取所有文件和目录并添加到ArrayList类对象作为文件列表:

private File root;
private ArrayList<File> fileList = new ArrayList<File>();
private LinearLayout view;
private EditText filename;
private Button search;
private TextView allfiles,searchfile;
Spannable txt;
boolean check=false;
ListView l;
String [] strarray;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    l=(ListView)findViewById(R.id.listView1);
    view = (LinearLayout) findViewById(R.id.view);``
    filename=(EditText)findViewById(R.id.filename);
    allfiles=(TextView)findViewById(R.id.allfiles);
    searchfile=(TextView)findViewById(R.id.searchfile);
    search=(Button)findViewById(R.id.search);

    String path=Environment.getExternalStorageDirectory().toString();
    root = new File(path);

    getfile(root);
    for (int i = 0; i < fileList.size(); i++) 
    {
        if (fileList.get(i).isDirectory()) 
        {
            txt=new SpannableString(fileList.get(i).getName());
            txt.setSpan(new ForegroundColorSpan(Color.RED), 0, txt.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            allfiles.append(txt);
            allfiles.append("\n");
        }
        // if file than show in default color
        else
        {
        allfiles.append(fileList.get(i).getName());
        allfiles.append("\n");
        allfiles.setPadding(5, 5, 5, 5);

        }
    }

getfile 方法显示 SD 卡上的所有目录和文件:

public ArrayList<File> getfile(File dir) 
{
    File listFile[] = dir.listFiles();

    if (listFile != null && listFile.length > 0)
    {
        for (int i = 0; i < listFile.length; i++) 
        {

            if (listFile[i].isDirectory())
            {
                fileList.add(listFile[i]);
                getfile(listFile[i]);// recursive calling to scan hole sdcard


            }
            else 
            {
                //condition to check file exists or not if yes add it to searchfile text view written below
                if(listFile[i].getName().toString().substring(0,listFile[i].getName().toString().indexOf(".")).compareToIgnoreCase(filename.getText().toString())==0)
                {
                    searchfile.append(listFile[i].getName());
                    searchfile.append("\n"); 
                    check=true;
                }
                fileList.add(listFile[i]);

            }

        }

return fileList;

【问题讨论】:

    标签: android listview android-listview arraylist android-sdcard


    【解决方案1】:

    首先了解Listview及其关联的methods,然后参考以下线程How to display files on the SD card in a ListView?回答您的问题。

    为了在列表视图中搜索项目,您需要执行以下步骤:

    1. 将 TextWatcher 添加到您的 EditText.TextView#addTextChangedListener
    2. 在您的列表视图 BaseAdapter 中实现Filterable 接口

    更多信息请参考List View Filter Android。希望这可以帮助您入门:)

    【讨论】:

      猜你喜欢
      • 2012-03-08
      • 1970-01-01
      • 1970-01-01
      • 2021-02-13
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      • 2010-10-02
      相关资源
      最近更新 更多