This example display all folder of sdcard in listview.see this code..
package com.getallfolder;
import java.io.File;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class GetallfolderActivity extends Activity
{
ArrayList<String> path = new ArrayList<String>();
ArrayList<String> item = new ArrayList<String>();
ListView lst;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lst = (ListView)findViewById(R.id.listView1);
File mfile=new File("/sdcard/");
File[] list=mfile.listFiles();
for(int i=0; i < list.length; i++)
{
File file = list[i];
path.add(file.getPath());
if(file.isDirectory())
item.add(file.getName()+"/");
else
item.add(file.getName());
}
lst.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,item));
}
}
package com.getallfolder;
import java.io.File;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class GetallfolderActivity extends Activity
{
ArrayList<String> path = new ArrayList<String>();
ArrayList<String> item = new ArrayList<String>();
ListView lst;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lst = (ListView)findViewById(R.id.listView1);
File mfile=new File("/sdcard/");
File[] list=mfile.listFiles();
for(int i=0; i < list.length; i++)
{
File file = list[i];
path.add(file.getPath());
if(file.isDirectory())
item.add(file.getName()+"/");
else
item.add(file.getName());
}
lst.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,item));
}
}
No comments:
Post a Comment