hm-lok

Unnamed repository; edit this file 'description' to name the repository.
git clone http://git.code.weiherhei.de/hm-lok.git
Log | Files | Refs

PVOList.java (5695B)


      1 package de.jandankert.hansemerkur;
      2 
      3 import java.io.IOException;
      4 
      5 import android.app.AlertDialog;
      6 import android.app.ListActivity;
      7 import android.content.Context;
      8 import android.content.Intent;
      9 import android.database.Cursor;
     10 import android.database.sqlite.SQLiteDatabase;
     11 import android.location.Location;
     12 import android.location.LocationManager;
     13 import android.os.Bundle;
     14 import android.view.Menu;
     15 import android.view.MenuInflater;
     16 import android.view.MenuItem;
     17 import android.view.View;
     18 import android.widget.AdapterView;
     19 import android.widget.CursorAdapter;
     20 import android.widget.ListView;
     21 import android.widget.TextView;
     22 import android.widget.Toast;
     23 import android.widget.AdapterView.OnItemClickListener;
     24 import android.widget.AdapterView.OnItemLongClickListener;
     25 import de.jandankert.hansemerkur.adapter.PVOCursorAdapter;
     26 import de.jandankert.hansemerkur.db.DataBaseHelper;
     27 
     28 public class PVOList extends ListActivity
     29 {
     30 	private static final int MAX_ENTFERNUNG = 100;
     31 
     32 	/** Called when the activity is first created. */
     33 	@Override
     34 	public void onCreate(Bundle savedInstanceState)
     35 	{
     36 		super.onCreate(savedInstanceState);
     37 		setContentView(R.layout.pvo);
     38 
     39 		/*
     40 		 * 
     41 		 * DataBaseHelper myDbHelper = new DataBaseHelper(); myDbHelper = new
     42 		 * DataBaseHelper(this);
     43 		 * 
     44 		 * try {
     45 		 * 
     46 		 * myDbHelper.createDataBase();
     47 		 * 
     48 		 * } catch (IOException ioe) {
     49 		 * 
     50 		 * throw new Error("Unable to create database");
     51 		 * 
     52 		 * }
     53 		 * 
     54 		 * try {
     55 		 * 
     56 		 * myDbHelper.openDataBase();
     57 		 * 
     58 		 * }catch(SQLException sqle){
     59 		 * 
     60 		 * throw sqle;
     61 		 * 
     62 		 * }
     63 		 */
     64 
     65 		Location loc = getLocation();
     66 
     67 		DataBaseHelper db = new DataBaseHelper(this);
     68 		try
     69 		{
     70 			db.createDataBase();
     71 		} catch (IOException e)
     72 		{
     73 			throw new RuntimeException(e);
     74 		}
     75 		db.openDataBase();
     76 		SQLiteDatabase database = db.getReadableDatabase();
     77 
     78 		double lat = 50.0;
     79 		double lon = 10.3;
     80 		if (loc != null)
     81 		{
     82 
     83 			lat = loc.getLatitude();
     84 			lon = loc.getLongitude();
     85 		} else
     86 		{
     87 			new AlertDialog.Builder(this).setTitle(
     88 					getResources().getString(R.string.no_location_available))
     89 					.show();
     90 			// Toast.makeText(this,
     91 			// getResources().getString(R.string.no_location_available),
     92 			// Toast.LENGTH_LONG).show();
     93 		}
     94 		Toast.makeText(this, "Suche wird gestartet", Toast.LENGTH_SHORT).show();
     95 
     96 		double laengeLaengengrad = 40000 / 360;
     97 		double laengeBreitengrad = laengeLaengengrad
     98 				* Math.cos(Math.cos(Math.toRadians(lat)));
     99 		laengeBreitengrad = 67;
    100 
    101 		final String sql = "SELECT *,"
    102 				+ "(? * (?-vms.lat)* ? * (?-vms.lat)) + (? * (? - vms.lon)*(? * (? - vms.lon))) AS entfernung,"
    103 				// + "vms.strasse,vms.plz,vms.ort,vms.vorname,vms.nachname,"
    104 				+ " vms.plz as _id" + " FROM vms"
    105 				+ " where lon is not null and entfernung < (" + MAX_ENTFERNUNG
    106 				+ "*" + MAX_ENTFERNUNG + ")" + " order by entfernung";
    107 
    108 		String[] param = new String[] { "" + laengeBreitengrad, "" + lat,
    109 				"" + laengeBreitengrad, "" + lat, "" + laengeLaengengrad,
    110 				"" + lon, "" + laengeLaengengrad, "" + lon };
    111 
    112 		Cursor cursor = database.rawQuery(sql, param);
    113 
    114 		startManagingCursor(cursor);
    115 		
    116 		// Now create an array adapter and set it to display using our row
    117 		CursorAdapter pvoAdapter = new PVOCursorAdapter(this, cursor);
    118 
    119 		ListView list = getListView();
    120 		
    121 		list.setOnItemLongClickListener(new OnItemLongClickListener()
    122 		{
    123 
    124 			@Override
    125 			public boolean onItemLongClick(AdapterView<?> parent, View view,
    126 					int position, long id)
    127 			{
    128 				Toast.makeText(PVOList.this,
    129 						"Item in position " + position + " long clicked",
    130 						Toast.LENGTH_LONG).show();
    131 				// Return true to consume the click event. In this case the
    132 				// onListItemClick listener is not called anymore.
    133 				return true;
    134 			}
    135 		});
    136 		list.setOnItemClickListener(new OnItemClickListener()
    137 		{
    138 
    139 			@Override
    140 			public void onItemClick(AdapterView<?> parent, View view,
    141 					int position, long id)
    142 			{
    143 				final Intent i = new Intent(PVOList.this, PVODetail.class);
    144 				i.putExtra("id",id);
    145 				i.putExtra("ort",((TextView)view.findViewById(R.id.pvo_ort)).getText());
    146 				startActivity(i);
    147 			}
    148 		});
    149 
    150 		// list.setItemsCanFocus(false);
    151 		setListAdapter(pvoAdapter);
    152 
    153 	}
    154 
    155 	// @Override
    156 	// protected void onStop()
    157 	// {
    158 	// super.stop();
    159 	// }
    160 
    161 	private Location getLocation()
    162 	{
    163 		LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    164 		Location loc = lm
    165 				.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    166 
    167 		return loc;
    168 	}
    169 
    170 	@Override
    171 	public boolean onSearchRequested()
    172 	{
    173 		// TODO Auto-generated method stub
    174 		return super.onSearchRequested();
    175 	}
    176 
    177 	@Override
    178 	protected void onListItemClick(ListView l, View v, int position, long id)
    179 	{
    180 		super.onListItemClick(l, v, position, id);
    181 		// Get the item that was clicked
    182 		Object o = this.getListAdapter().getItem(position);
    183 		String keyword = o.toString();
    184 		Toast.makeText(this, "You selected with short click: " + keyword,
    185 				Toast.LENGTH_LONG).show();
    186 
    187 		// Intent i = new Intent(Intent.ACTION_VIEW, Uri
    188 		// .parse("google.navigation:q=New+York"));
    189 		// startActivity(i);
    190 
    191 		// Intent callIntent = new Intent(Intent.ACTION_CALL);
    192 		// callIntent.setData(Uri.parse("tel:123456789"));
    193 		// startActivity(callIntent);
    194 
    195 		// String uri = "geo:"+ latitude + "," + longitude;
    196 		// startActivity(new Intent(android.content.Intent.ACTION_VIEW,
    197 		// Uri.parse(uri)));
    198 	}
    199 
    200 	public boolean onCreateOptionsMenu(Menu menu)
    201 	{
    202 		super.onCreateOptionsMenu(menu);
    203 		MenuInflater mi = new MenuInflater(getApplication());
    204 		mi.inflate(R.menu.menu, menu);
    205 
    206 		return true;
    207 	}
    208 
    209 	public boolean onOptionsItemSelected(MenuItem item)
    210 	{
    211 		switch (item.getItemId())
    212 		{
    213 		case R.id.item01:
    214 			super.finish();
    215 			break;
    216 		default:
    217 
    218 		}
    219 		return true;
    220 	}
    221 
    222 }