Pergunta

I'm using this function to display the ArrayList in a TextView, which will show the all directory and files in SDCard, but I want to use ListView instead. What should I do to show the directory and all file of sdcard in ListView?

Method getfile to read all files and directory and add to ArrayList class object as filelist:

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);

        }
    }

the getfile method to show the all Directory and Files on SD Card:

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;
Foi útil?

Solução

First learn about the Listview and its associated methods, then refer the following thread How to display files on the SD card in a ListView? to answer your question.

Added to that in order to search items in the listview, you need to follow the below steps:

  1. Add a TextWatcher to your EditText.TextView#addTextChangedListener.
  2. ImplementFilterable interface in your listview BaseAdapter

For more refer List View Filter Android. Hope this would help you get started :)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top