2013-06-13 37 views
-1

如何在Android中使用目录和存储卡中的文件填充ListView。 请推荐一些链接或书籍。使用文件填充Android ListView

+2

你到目前为止尝试过什么? –

+0

这已经被问及和实施了!倍。 (n接近+ inf) – Nima

回答

2

试试这个页面:http://android-er.blogspot.hu/2010/01/implement-simple-file-explorer-in.html

复制的代码,如果该链接会死在未来:

Row.xml

<TextView 

    xmlns:android="http://schemas.android.com/apk/res/android" 

    android:id="@+id/rowtext" 

    android:layout_width="fill_parent" 

    android:layout_height="25px" 

    android:textSize="23sp" /> 

的main.xml:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:orientation="vertical" 

    android:layout_width="fill_parent" 

    android:layout_height="fill_parent" 

    > 

<TextView 

android:id="@+id/path" 

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    /> 

<ListView 

android:id="@android:id/list" 

android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

/> 

<TextView 

android:id="@android:id/empty" 

android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:text="No Data" 

/> 

</LinearLayout> 

AndroidExplorer.java:

package com.AndroidExplorer; 



import java.io.File; 

import java.util.ArrayList; 

import java.util.List; 

import android.app.AlertDialog; 

import android.app.ListActivity; 

import android.content.DialogInterface; 

import android.os.Bundle; 

import android.view.View; 

import android.widget.ArrayAdapter; 

import android.widget.ListView; 

import android.widget.TextView; 



public class AndroidExplorer extends ListActivity { 



private List<String> item = null; 

private List<String> path = null; 

private String root="/"; 

private TextView myPath; 



    /** Called when the activity is first created. */ 

    @Override 

    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     setContentView(R.layout.main); 

     myPath = (TextView)findViewById(R.id.path); 

     getDir(root); 

    } 



    private void getDir(String dirPath) 

    { 

    myPath.setText("Location: " + dirPath); 



    item = new ArrayList<String>(); 

    path = new ArrayList<String>(); 



    File f = new File(dirPath); 

    File[] files = f.listFiles(); 



    if(!dirPath.equals(root)) 

    { 



     item.add(root); 

     path.add(root); 



     item.add("../"); 

     path.add(f.getParent()); 



    } 



    for(int i=0; i < files.length; i++) 

    { 

     File file = files[i]; 

     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.row, item); 

    setListAdapter(fileList); 

    } 



@Override 

protected void onListItemClick(ListView l, View v, int position, long id) { 



    File file = new File(path.get(position)); 



    if (file.isDirectory()) 

    { 

    if(file.canRead()) 

    getDir(path.get(position)); 

    else 

    { 

    new AlertDialog.Builder(this) 

    .setIcon(R.drawable.icon) 

    .setTitle("[" + file.getName() + "] folder can't be read!") 

    .setPositiveButton("OK", 

     new DialogInterface.OnClickListener() { 



     @Override 

     public void onClick(DialogInterface dialog, int which) { 

     // TODO Auto-generated method stub 

     } 

     }).show(); 

    } 

    } 

    else 

    { 

    new AlertDialog.Builder(this) 

    .setIcon(R.drawable.icon) 

    .setTitle("[" + file.getName() + "]") 

    .setPositiveButton("OK", 

     new DialogInterface.OnClickListener() { 



     @Override 

     public void onClick(DialogInterface dialog, int which) { 

     // TODO Auto-generated method stub 

     } 

     }).show(); 

    } 

} 

} 
+1

该死!你用同样的链接打败了我几秒钟! +1用于在链接关闭的情况下复制代码。 – Joel

+0

对不起:P BTW这是一个很好的例子和博客! – viplezer

+0

非常感谢您的帮助:-) –

0

this链接:

使用java.io.File类从目录中读取文件:

String dirPath = "some directory full path goes here"; 
File f = new File(dirPath); 
File[] files = f.listFiles(); 

然后你就可以完成,其中列出了ListView文件相同的链接您的教程。

有关ListView填充的另一个快速链接,您可以尝试Vogella tutorial


编辑:

如果你是新来的Android(从您的文章,你可能也可以),尝试一些tutorialsLars Vogel。他们写得很好,很容易理解。

1

你可以刚刚做了一个快速的谷歌搜索。 Here是一步一步的教程。

+0

一束谢谢.. !!! 这就是我需要的... –

+0

你可以帮助我通过提高投票和标记它正确的答案:) – Saurabh