package com.example.utils01;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.ArrayAdapter;
import android.widget.ListView;


import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;


public class files_activity extends Activity {
 
	  ListView lisView1;
	  private File file;
	  private List<String> myList = new ArrayList<String>();   
	
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.files);
        
        lisView1 = (ListView)findViewById(R.id.listView1); 

        String root_sd = Environment.getExternalStorageDirectory().toString();
        file = new File( root_sd  ) ;       
        File liste[] = file.listFiles();

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

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myList);
        
        lisView1.setAdapter(adapter);
        lisView1.setOnItemClickListener(new ListClickHandler());
        
        //setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myList ));
        
        
        
        //Intent returnIntent = new Intent();
        //returnIntent.putExtra("result",result);
       // setResult(RESULT_OK,returnIntent);     
       // finish();
 
        
    }
    
    public class ListClickHandler implements OnItemClickListener{
    	 
        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int position, long arg3)
        {
            // TODO Auto-generated method stub
        	// String filename = (String) getListAdapter().getItem(position);
        	
        	// String text = lisView1.getTextFilter() .getText().toString();
        	 
        	// Toast.makeText(files_activity.this, myList.get(position), Toast.LENGTH_SHORT).show();
        	 
        	 
        	// Intent iData = new Intent();
           //  iData.putExtra(      EXTRA_STRING_NAME,   myList.get(position) );
           //  setResult(          android.app.Activity.RESULT_OK,    iData );
           //  finish();
        	 
             
             Intent data = new Intent();
             data.putExtra("Order", myList.get(position));
             // Activity finished ok, return the data
             setResult(RESULT_OK, data);
             finish();
        	 
            //TextView listText = (TextView) view.findViewById(R.id.listText);
            //String text = listText.getText().toString();
            //Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            //intent.putExtra("selected-item", text);
            //startActivity(intent);  
             
        }
         
    }
    
    /*
    @Override
	protected void onListItemClick(ListView l, View v, int position, long id)
	{
	    String filename = (String) getListAdapter().getItem(position);
	    Toast.makeText(this, filename , Toast.LENGTH_LONG).show();
	    Intent intent = new Intent(this, SecondActivity.class);
        intent.putExtra("selected-item", filename);
        startActivity(intent);  
	}*/
}
