在 Android 中使用 OnItemClickListener 从 SQLite 数据库获取项目
当我在 ListView 中使用 OnItemClickListener 时,我想从数据库中获取项目,但我不知道如何执行此操作的语法。
另一个说我必须实现 onListItemClick 方法,我尝试使用它,但我强制关闭,也许我的语法是错误的。
你能给出如何做到这一点的代码建议吗?我因此而感到沮丧。我真的很感谢那些回答这个问题的人。
这是我的代码,请注意 onItemClick 方法:
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());
}
}
}
这是 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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我希望您只将酒店名称加载到您的列表视图中。
我认为您应该首先从列表视图中获取酒店名称(希望您正确获取列表视图)。在您的
onItemClick(AdapterView arg0, View arg1, int arg2, long arg3)
中,首先添加此内容。然后将其传递给您的选择查询。
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.Then pass it to your select query.