Android在AutocompleteTextview中选择特定列值,并使用sqlite db显示位置?
如何从“条件”列中获取特定值并获取纬度和经度 我的SQLITE数据库列包括:Android在AutocompleteTextview中选择特定列值,并使用sqlite db显示位置?,android,google-maps,android-sqlite,autocompletetextview,Android,Google Maps,Android Sqlite,Autocompletetextview,如何从“条件”列中获取特定值并获取纬度和经度 我的SQLITE数据库列包括: //Table Columns names private static final String KEY_TID = "id"; public static final String KEY_TreeID = "treeid"; public static final String KEY_TCONDITION = "condition"; public static final String KEY_TLA
//Table Columns names
private static final String KEY_TID = "id";
public static final String KEY_TreeID = "treeid";
public static final String KEY_TCONDITION = "condition";
public static final String KEY_TLATITUDE = "latitude";
public static final String KEY_TLONGITUDE = "longitude";
private static final String CREATE_TABLE = "CREATE TABLE if not exists " + TABLE_TREE + "("
+ KEY_TID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_TreeID + " , "
+ KEY_TCONDITION + " , "
+ KEY_TLATITUDE + " double, "
+ KEY_TLONGITUDE + " double "
+");";
ID Condition Latitude Longitude
1 Excellent 12.897485 15.425974
2 Good 12.897485 15.425974
3 Excellent 12.897485 15.425974
4 Poor 12.897485 15.425974
5 Poor 12.897485 15.425974
6 Excellent 12.897485 15.425974
7 Fair 12.897485 15.425974
8 Good 12.897485 15.425974
9 Good 12.897485 15.425974
10 Excellent 12.897485 15.425974
我的地图活动:
我使用了一个AutoCompleteTextView来表示我的“条件”=“优秀”、“良好”、“较差”和“一般”。点击一个按钮可以显示带有标记的谷歌地图
inputSearch = (AutoCompleteTextView) findViewById(R.id.et_search);
ArrayAdapter adapter1 = new ArrayAdapter(this, android.R.layout.select_dialog_item, conditions);
inputSearch.setThreshold(1);
inputSearch.setAdapter(adapter1);
btMap = (Button) findViewById(R.id.MapButton);
若我的AutoCompleteTextView为空并单击按钮,它将显示我的所有10个位置,如sqlite db表所示
“这工作正常”
否则,如果我从AutoCompleteTextview中选择“优秀”并单击按钮,它应该只显示“优秀”行纬度和经度,即ID 1、3、6和10
“这不起作用”
为了读取我的数据库,我使用了以下代码:
//Getting all the 10 Location information in MapsActivity
public Cursor getLocations(){
db = SQLiteHandler.getWritableDatabase();
String[] selecteditems = {SQLiteHandler.KEY_TLATITUDE,SQLiteHandler.KEY_TLONGITUDE, SQLiteHandler.KEY_TCONDITION,SQLiteHandler.KEY_TreeID};
Cursor cursor = db.query(SQLiteHandler.TABLE_TREE,selecteditems,null,null,null,null,null);
return cursor;
}
//Get only selected row in AutoCompleteTextView information in MapsActivity
public Cursor getSelectedLocations(String search){
db = SQLiteHandler.getWritableDatabase();
String[] selecteditems = {SQLiteHandler.KEY_TLATITUDE,SQLiteHandler.KEY_TLONGITUDE, SQLiteHandler.KEY_TCONDITION,SQLiteHandler.KEY_TreeID};
String where = SQLiteHandler.KEY_TCONDITION + "=?";
String[] whereArgs = new String[]{search+""};
Cursor cursor = db.query(SQLiteHandler.TABLE_TREE,selecteditems,where,whereArgs,null,null,null,null);
return cursor;
}
错误
致命异常:主
进程:com.example.bharat.plantnow,PID:2124
java.lang.NullPointerException
在com.example.bharat.plantnow.MapsActivity$1.onClick(MapsActivity.java:132)上
在android.view.view.performClick上(view.java:4438)
在android.view.view$PerformClick.run(view.java:18422)
位于android.os.Handler.handleCallback(Handler.java:733)
位于android.os.Handler.dispatchMessage(Handler.java:95)
位于android.os.Looper.loop(Looper.java:136)
位于android.app.ActivityThread.main(ActivityThread.java:5017)
位于java.lang.reflect.Method.Invokenactive(本机方法)
位于java.lang.reflect.Method.invoke(Method.java:515)
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)上
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
在dalvik.system.NativeStart.main(本机方法)在Toast中获得
一切都很好
?是。在If语句之后。但它在Else语句首先执行时显示错误。
if(search.length() > 0){
Cursor c = db.getSelectedLocations(search);
c.moveToFirst();
do {
double latitude1 = c.getDouble(c
.getColumnIndex(SQLiteHandler.KEY_TLATITUDE));
double longitude1 = c.getDouble(c
.getColumnIndex(SQLiteHandler.KEY_TLONGITUDE));
String marker = c.getString(c
.getColumnIndex(SQLiteHandler.KEY_TreeID));
LatLng location = new LatLng(latitude1, longitude1);
mMap.addMarker(new MarkerOptions().position(location).title(marker));
} while (c.moveToNext());
Toast.makeText(getApplicationContext(), "All is Well", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getApplicationContext(), "Invalid", Toast.LENGTH_LONG).show();
}
}
});}
//Getting all the 10 Location information in MapsActivity
public Cursor getLocations(){
db = SQLiteHandler.getWritableDatabase();
String[] selecteditems = {SQLiteHandler.KEY_TLATITUDE,SQLiteHandler.KEY_TLONGITUDE, SQLiteHandler.KEY_TCONDITION,SQLiteHandler.KEY_TreeID};
Cursor cursor = db.query(SQLiteHandler.TABLE_TREE,selecteditems,null,null,null,null,null);
return cursor;
}
//Get only selected row in AutoCompleteTextView information in MapsActivity
public Cursor getSelectedLocations(String search){
db = SQLiteHandler.getWritableDatabase();
String[] selecteditems = {SQLiteHandler.KEY_TLATITUDE,SQLiteHandler.KEY_TLONGITUDE, SQLiteHandler.KEY_TCONDITION,SQLiteHandler.KEY_TreeID};
String where = SQLiteHandler.KEY_TCONDITION + "=?";
String[] whereArgs = new String[]{search+""};
Cursor cursor = db.query(SQLiteHandler.TABLE_TREE,selecteditems,where,whereArgs,null,null,null,null);
return cursor;
}