使用Android中的OnItemClickListener从SQLite数据库中获取项目

时间:2011-11-28 03:06:44

标签: android sqlite listview syntax onitemclicklistener

当我在ListView中使用OnItemClickListener时,我想从我的数据库中获取项目,但我不知道语法如何做到这一点。

另一个说我必须实现onListItemClick方法,我尝试使用它,但我强行关闭,也许我的语法是错误的。

您能否提供代码建议如何做到这一点。因此我感到沮丧。我非常感谢那些回答这个问题的人。

这是我的代码,请给出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());
    }       
}

}

这是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)

1 个答案:

答案 0 :(得分:1)

我希望您只将酒店名称加载到ListView。 我想你应该首先从你的列表视图中获取你的酒店名称(希望你正确获得listview)。在onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)中,先添加此内容。

String selectedItem = (String) numberList.getItemAtPosition(arg2);

然后将其传递给您的选择查询。