Domanda

I really need your help.

I have a database with latitude and longitude data, here the example:

id_____name_____lat_________long

1_____hotel one___6.1234______-106.12345

2____hotel two____6.54321_____-107.23456

3___hotel three___7.12345_____-106.98765

The data display in ListView on Android. And i use onItemClick, so if i click one of the item in ListView it will go to

Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?f=d&saddr=&daddr="+lat_var+","+long_var))

But the problem is if i click one of the item in ListView the data in lat_var and long_var on intent above won't give me the lat and long data from my database...

Here my code, can you edit it, so if i click one of the item in ListView it will give me the lat and long data from my database. For example if i click hotel two the lat_var will change to 6.54321 and the long_var will change to -107.23456 on that intent.

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            SQLiteDatabase dbs = dbHotelHelper.getReadableDatabase();

            String get_lat = "SELECT lat FROM hoteltbl";
            Cursor result_lat = dbs.rawQuery(get_lat, null);
            double lat_var = result_lat.getColumnIndex("lat");
            String get_long = "SELECT lat FROM hoteltbl";
            Cursor result_long = dbs.rawQuery(get_long, null);
            double long_var = result_long.getColumnIndex("long");

            Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?f=d&saddr=&daddr="+lat_var+","+long_var));
            startActivity(intent);

        }

I don't know if the problem is my syntax or what. Please fix my code. I really thanks for anyone who answer this...

È stato utile?

Soluzione

First, only one query is necessary. No need to query your table twice. Second, you need to adapt your query with parameters from your selection:

@Override
protected void onListItemClick(ListView listView, View view, int position, long id) {id) {    
    String selectedItem = (String) getListAdapter().getItem(position);
    String query = "SELECT lat, long FROM hoteltbl WHERE name = '" + selectedItem + "'";
    SQLiteDatabase dbs = dbHotelHelper.getReadableDatabase();
    Cursor result = dbs.rawQuery(query, null);
    result.moveToFirst();

    double lat = result.getDouble(result.getColumnIndex("lat"));
    double long = result.getDouble(result.getColumnIndex("long"));

    ....

This code was written in the StackOverflow editor, it might not work as such, but will give you an idea.

(Also, don't add suffixes "_var", or "_long" to your variables, it's not necessary in a strongly-typed language like Java)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top