package com.example.geo_api16;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CustomTrajetListview extends Activity 
{
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.trajet02_listview_layout);
        
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();

        if(bundle != null)
        {
           String infos_trajet = bundle.getString("infos_trajet");
           TextView txt = (TextView) findViewById(R.id.infos_trajet);
           txt.setText(infos_trajet);
        }
        
        ArrayList<Elements_trajet> toto = GetSearchResults();
       
        final ListView lv1 = (ListView) findViewById(R.id.ListView01);
        
       
        
        lv1.setAdapter(new TrajetAdapter (this, toto ));
       
        lv1.setOnItemClickListener(new OnItemClickListener() 
        {
        	@Override
        	public void onItemClick(AdapterView<?> a, View v, int position, long id)
        	{
        		Object obj = lv1.getItemAtPosition(position);
        		Elements_trajet fullObject = (Elements_trajet) obj;
        		
        		Toast.makeText(CustomTrajetListview.this, "You have chosen: " + " " + fullObject.getName(), Toast.LENGTH_LONG).show();
        	}

			
        });
        
   
    }
    
    private ArrayList<Elements_trajet> GetSearchResults()
    {
        ArrayList<Elements_trajet> results = new ArrayList<Elements_trajet>();
                
        String filenamex = "infos_filename.txt";
      	File filex = new File(Environment.getExternalStorageDirectory(), filenamex);
      	FileInputStream fis;
      	    
   	    //String [] codes = new String[200];
      	    
   		try 
   		{
   			fis = new FileInputStream(filex);
       		BufferedReader br = new BufferedReader(new InputStreamReader(fis));
       		String line = null;
          		
       		while ((line = br.readLine()) != null)
       		{
       			//System.out.println(line);
       			
       		    line = line.trim();
       		    String match = "*";
        		int index = line.indexOf(match);
       		    
       		    // un
       		    int start = 0;
       		    int stop = index ;
       		    String s1 = line.substring(start,stop);
       		    
       		    // deux
       		    start = index + 1;
       		    index = line.indexOf(match, index + 1);
       		    stop = index;
       		    String s2 = line.substring(start,stop);

       		    
       		    // trois
       		    start = index + 1;
       		    stop =  line.length();
       		    String s3 = line.substring(start,stop);
       		    
       		    Elements_trajet item = new Elements_trajet();
       		    item.setName(s1);
       		    item.setCityState(s2);
       		    item.setPhone(s3);
       		    results.add(item);
       		}
          	 
       		br.close();
      			
	   	} 
	   	catch (FileNotFoundException e) 
	   	{
	   		e.printStackTrace();
	   	} 
	   	catch (IOException e)
	   	{
      		e.printStackTrace();
      	}
	        
   		return results;

    }
    
}    