package com.example.GPS_DB02;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.SimpleCursorAdapter.ViewBinder;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


import android.app.ActionBar;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;

public class Listrun extends ListActivity
{

	DB_GPS db;
	private SQLiteDatabase dataBase;
	
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
			super.onCreate(savedInstanceState);
			setContentView(R.layout.listrun_layout);
			
			db = new DB_GPS(getApplicationContext());
			
			ActionBar actionBar = getActionBar();
	        getActionBar().setHomeButtonEnabled(true);
	        getActionBar().setDisplayHomeAsUpEnabled(true);
	        setTitle("Listing des runs");
	}
	
	
	@Override protected void onResume() 
	{
	        super.onResume();
	
	        SimpleCursorAdapter adapter = new SimpleCursorAdapter(
					this, 
					R.layout.row_listrun, 
					createCursor(), 
					new String[]{ "nom" , "date", "start", "stop", "steps" }, 
					new int[]{R.id.msg_tv, R.id.date_tv, R.id.time_tv}, 0);
	        
	    	adapter.setViewBinder(new ViewBinder() 
			{
				public boolean setViewValue(View view, Cursor cursor, int columnIndex) 
				{
					TextView tv = (TextView)view;
					
					switch(view.getId()) 
					{
					
							case R.id.msg_tv:
								String toto  = cursor.getString(columnIndex);
								tv.setText(toto);
								break;
					
							case R.id.date_tv:
								String titi  = cursor.getString(columnIndex);
								tv.setText(titi);
								break;
								
							case R.id.time_tv:
								String lulu  = cursor.getString(columnIndex);
								tv.setText(lulu);
								break;
					}
					return true;
				}
			});
			
			setListAdapter(adapter);
			

	        
	        
	      //  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getList());
	      //  ListView listView = (ListView) findViewById(R.id.run_listview);
	      //  listView.setAdapter(adapter);
	      //  addItemClickListener(listView);
    }

	
	private Cursor createCursor()
	{
		String TAG = " ****************** ";
		
		String requete = "SELECT * FROM TABLE_RUN";
		
		Cursor Cursor = db.DB_query(requete);
		int val = Cursor.getCount();
	    Log.i(TAG, String.valueOf(val));
	    return  Cursor;   
		
	}	
	
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id)
	{
		//String keyword = (String)this.getListAdapter().getItem(position);
		
		//Object o = this.getListAdapter().getItem(position);
		//String keyword = o.toString();
		// Object toto = l.getItemAtPosition(position);
		// String value = toto.toString();
		 
		 
		Cursor c = (Cursor)l.getItemAtPosition(position);
		String run_name = c.getString(c.getColumnIndex(db.COL_NOM_RUN ));
        Intent intent = new Intent(Listrun.this, Display_datas_run.class);
        intent.putExtra("run_name", run_name);
        startActivity(intent);
		
	}
    
    
    private void addItemClickListener(final ListView listView)
    {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
        {
	            @Override 
	            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
	            {
	                String itemValue = (String) listView.getItemAtPosition(position);
	                Toast.makeText(getApplicationContext(), "Position: " + position + " item: " + itemValue, Toast.LENGTH_SHORT).show();
	            }
        });
    }
	
    
    
    
}