如何从SQLite数据库中检索标题和片段?

时间:2015-01-07 16:14:09

标签: android sqlite

我对如何检索已保存在SQLite数据库中的标题和片段感到困惑。我保存了标题和片段,可以在我的数据库中看到它,但我不完全确定如何检索所需的信息。

所以这就是我保存标题和片段的方式:

contentValues.put(LocationsDB.FIELD_TITLE, title.getText().toString());
contentValues.put(LocationsDB.FIELD_SNIPPET, snippet.getText().toString());

因此,我可以看到(访问数据库)标题和片段被保存。现在我的问题是如何检索该信息?

我可以解读latlng值以及缩放级别,但不知道如何获取标题和摘要:

加载latlng值和缩放级别:

Marker marker;

......

@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
    int locationCount = 0;
    double lat=0;
    double lng=0;
    float zoom=0;
    float title = 0; \\Don't know if this part is correct..with warning "The value of the local variable title is not used" 
    float snippet = 0; \\Don't know if this part is correct ..with warning "The value of the local variable title is not used"
    locationCount = arg1.getCount();
    arg1.moveToFirst();

    for(int i=0;i<locationCount;i++){

 lat = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LAT));
 lng = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LNG));
 zoom = arg1.getFloat(arg1.getColumnIndex(LocationsDB.FIELD_ZOOM));
 title = arg1.getFloat(arg1.getColumnIndex(LocationsDB.FIELD_TITLE));
 snippet = arg1.getFloat(arg1.getColumnIndex(LocationsDB.FIELD_SNIPPET));
 point = new LatLng(lat, lng);
 drawMarker(point); 
 arg1.moveToNext();
    }
    if(locationCount>0){
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat,lng)));
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(zoom));
        TextView titleText = (TextView) v.findViewById(R.id.TitleId);
        titleText.setText(marker.getTitle());
        TextView snippetText = (TextView) v.findViewById(R.id.SnippetId);
        snippetText.setText(marker.getSnippet());

这样做应用程序然后崩溃? 我真的希望有人可以帮助我并解释我做错了什么

修改

Stacktrace:

01-07 18:45:07.788: E/AndroidRuntime(17933): FATAL EXCEPTION: main
01-07 18:45:07.788: E/AndroidRuntime(17933): Process: com.example.MapTest, PID: 17933
01-07 18:45:07.788: E/AndroidRuntime(17933): java.lang.NullPointerException
01-07 18:45:07.788: E/AndroidRuntime(17933):    at com.example.MapTest.MainMap.onLoadFinished(MainMap.java:357)
01-07 18:45:07.788: E/AndroidRuntime(17933):    at com.example.MapTest.MainMap.onLoadFinished(MainMap.java:1)
01-07 18:45:07.788: E/AndroidRuntime(17933):    at android.support.v4.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:427)
01-07 18:45:07.788: E/AndroidRuntime(17933):    at android.support.v4.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:395)
01-07 18:45:07.788: E/AndroidRuntime(17933):    at android.support.v4.content.Loader.deliverResult(Loader.java:104)
01-07 18:45:07.788: E/AndroidRuntime(17933):    at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:73)
01-07 18:45:07.788: E/AndroidRuntime(17933):    at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:35)
01-07 18:45:07.788: E/AndroidRuntime(17933):    at android.support.v4.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:223)
01-07 18:45:07.788: E/AndroidRuntime(17933):    at android.support.v4.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:61)
01-07 18:45:07.788: E/AndroidRuntime(17933):    at android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:461)
01-07 18:45:07.788: E/AndroidRuntime(17933):    at android.support.v4.content.ModernAsyncTask.access$500(ModernAsyncTask.java:47)
01-07 18:45:07.788: E/AndroidRuntime(17933):    at android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:474)
01-07 18:45:07.788: E/AndroidRuntime(17933):    at android.os.Handler.dispatchMessage(Handler.java:102)
01-07 18:45:07.788: E/AndroidRuntime(17933):    at android.os.Looper.loop(Looper.java:136)
01-07 18:45:07.788: E/AndroidRuntime(17933):    at android.app.ActivityThread.main(ActivityThread.java:5586)
01-07 18:45:07.788: E/AndroidRuntime(17933):    at java.lang.reflect.Method.invokeNative(Native Method)
01-07 18:45:07.788: E/AndroidRuntime(17933):    at java.lang.reflect.Method.invoke(Method.java:515)
01-07 18:45:07.788: E/AndroidRuntime(17933):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
01-07 18:45:07.788: E/AndroidRuntime(17933):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
01-07 18:45:07.788: E/AndroidRuntime(17933):    at dalvik.system.NativeStart.main(Native Method)

第(357)行:

titleText.setText(marker.getTitle());

0 个答案:

没有答案