Question

I have a problem: when I press a folder that shown in the ListView I can't open it. I have a Folder class and adapter class. This is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    path = Environment.getExternalStorageDirectory().getPath();
    file = new File(path);
    folders = new ArrayList<Folders>();
    lv= (ListView) findViewById(R.id.listView1);

    adapter= new MyAdapter(getApplicationContext());
    lv.setAdapter(adapter);
    SetList();

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
                long arg3) {        
    String  newPath=    adapter.folders.get(pos).getNames()+folders.get(pos).getFiles();

    file = new File(path+"/"+newPath);
        String[] newdirs = file.list();
        folders.clear();
        for (int i = 0; i < newdirs.length; i++) {
            Folders fol = new Folders(newdirs[i], newdirs[i].length()+"itemes");
            adapter.folders.add(fol);
        }
        }
    }); 
}
public void SetList(){
    String[] dirs = file.list();
    for (int i = 0; i < dirs.length; i++) {
        Folders fol= new Folders(dirs[i], dirs[i].length()+" files");
        adapter.folders.add(fol);
Was it helpful?

Solution 2

try this worked for me:

public void onCreate(Bundle savedInstanceState) 
 {
super.onCreate(savedInstanceState);

myList = new ArrayList<String>();   

String root_sd = Environment.getExternalStorageDirectory().toString();
file = new File( root_sd + "/external_sd" ) ;       
File list[] = file.listFiles();

for( int i=0; i< list.length; i++)
{
    myList.add( list[i].getName() );
}

setListAdapter(new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, myList ));

}

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

File temp_file = new File( file, myList.get( position ) );  

if( !temp_file.isFile())        
{
    file = new File( file, myList.get( position ));
    File list[] = file.listFiles();

    myList.clear();

    for( int i=0; i< list.length; i++)
    {
        myList.add( list[i].getName() );
    }
    Toast.makeText(getApplicationContext(), file.toString(), Toast.LENGTH_LONG).show(); 
    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, myList ));

   }

  }

OTHER TIPS

this May help you:

methode to save files:

 public void saveFile(String data, Context context, String filepath, String filename){

            ContextWrapper contextWrapper = new ContextWrapper(context);
            File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
            File myInternalFile = new File(directory , filename);

            try {
                FileOutputStream fos = new FileOutputStream(myInternalFile);

                fos.write(data.getBytes());
                fos.close();

            } catch (IOException e) {
                e.printStackTrace();
            }

        }

methode to read files:

public String readFile(Context context, String filepath, String filename) throws JSONException{

            ContextWrapper contextWrapper = new ContextWrapper(context);
            File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
            File myInternalFile = new File(directory , filename);

            String myData = "";

            try {
                FileInputStream fis = new FileInputStream(myInternalFile);
                DataInputStream in = new DataInputStream(fis);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));

                String strLine;
                while ((strLine = br.readLine()) != null) {
                    myData = myData + strLine;
                }

                in.close();

            } catch (IOException e) {
                e.printStackTrace();
            }

            return myData;
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top