Get item from SQLite database with use OnItemClickListener in Android
-
10-03-2021 - |
Question
I want to get item from my database when i use OnItemClickListener in my ListView, but i don't know the syntax how to do that.
The other say i must implement onListItemClick method, i try to use that but i got force close, maybe my syntax is just wrong.
Can you give suggestion with code how to do that. I got frustrated because of this. I really thank you for those who answer this question.
Here's my code, please give intention to onItemClick mehod:
public class HotelList extends ListActivity{
hotelHelper dbHotelHelper;
protected Cursor cursor;
protected ListAdapter adapter;
ListView numberList;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.hotellist);
numberList = (ListView)findViewById(android.R.id.list);
numberList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String query = "SELECT lat, long FROM hoteltbl WHERE name = '" + selectedItem[THIS IS THE PROBLEM: the syntax to get selected item] + "'";
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);
}
});
dbHotelHelper = new hotelHelper(this);
try{
dbHotelHelper.createDataBase();
}
catch (Exception ioe){
Log.e("err","Unable to create database");
}
view();
}
private void view() {
// TODO Auto-generated method stub
SQLiteDatabase db = dbHotelHelper.getReadableDatabase();
try{
cursor = db.rawQuery("SELECT * FROM hoteltbl", null);
adapter = new SimpleCursorAdapter(
this,
R.layout.hotelview,
cursor,
new String[]{"name"},
new int[] {R.id.hotelname}
);
numberList.setAdapter(adapter);
}
catch (Exception e){
Log.e("error",e.toString());
}
}
}
Here's the LogCat:
11-28 20:44:10.449: ERROR/AndroidRuntime(856): FATAL EXCEPTION: main
11-28 20:44:10.449: ERROR/AndroidRuntime(856): java.lang.NullPointerException
11-28 20:44:10.449: ERROR/AndroidRuntime(856): at com.william.placefinder.HotelList$1.onItemClick(HotelList.java:40)
11-28 20:44:10.449: ERROR/AndroidRuntime(856): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
11-28 20:44:10.449: ERROR/AndroidRuntime(856): at android.widget.ListView.performItemClick(ListView.java:3382)
11-28 20:44:10.449: ERROR/AndroidRuntime(856): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
11-28 20:44:10.449: ERROR/AndroidRuntime(856): at android.os.Handler.handleCallback(Handler.java:587)
11-28 20:44:10.449: ERROR/AndroidRuntime(856): at android.os.Handler.dispatchMessage(Handler.java:92)
11-28 20:44:10.449: ERROR/AndroidRuntime(856): at android.os.Looper.loop(Looper.java:123)
11-28 20:44:10.449: ERROR/AndroidRuntime(856): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-28 20:44:10.449: ERROR/AndroidRuntime(856): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 20:44:10.449: ERROR/AndroidRuntime(856): at java.lang.reflect.Method.invoke(Method.java:521)
11-28 20:44:10.449: ERROR/AndroidRuntime(856): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-28 20:44:10.449: ERROR/AndroidRuntime(856): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-28 20:44:10.449: ERROR/AndroidRuntime(856): at dalvik.system.NativeStart.main(Native Method)
Solution
I hope you are loading only the Hotel Names to your ListView.
I think you should get your hotel name form your listview first(hope you are getting the listview correctly). In your onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
, add this first.
String selectedItem = (String) numberList.getItemAtPosition(arg2);
Then pass it to your select query.