1

I want to get latitude and longitude data from sqlite database.

So i use this code below, but there is a problem in getListAdapter(). It say that i must create 'getListAdapter()' method. I'm searching on the net, but there is no example for getListAdapter() method.

Can you fix my code. So if i click the item it will give me the lat and long data from my database.

public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
//TODO Auto-generated method stub
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 longi = result.getDouble(result.getColumnIndex("long"));

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

}

By the way, this is the problem from my previous question

Community
  • 1
  • 1
forgiven24
  • 49
  • 2
  • 10
  • In `ListActivity`, you can simply use `getListAdapter()`; In `ListView`, you can use `your_listview_name.getAdapter()` – Joanne Aug 08 '12 at 05:31

2 Answers2

6

You do not need to extend ListActivity. You simply need to find the listview and call its getAdapter() method.

Here is your updated code.

public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long arg3) {
//TODO Auto-generated method stub
ListView lv = (ListView) findViewById(R.id.YOURLISTVIEW);
String selectedItem = (String) lv.getAdapter().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 longi = result.getDouble(result.getColumnIndex("long"));

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

}
Mike L.
  • 589
  • 6
  • 16
0

Your activity should extends ListActivity to have getListAdapter method. Take a look at this tutorial.

If you want to get item, you should use getItem(position) method from adapter which was set to ListView

Vladimir
  • 3,774
  • 3
  • 23
  • 27