How to get specific values form columns "Condition" and get the latitude & Longitude.
My SQLITE database columns are:
//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
My MapActivity: I have used One AutoCompleteTextView for my "Condition" = 'Excellent', 'Good','Poor' & 'Fair'. And One Button to click to show Google Map with Markers.
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);
If My AutoCompleteTextView is Empty and Click Button, It Should show all my 10 location, as shown in sqlite db table.
"This is working correctly"
btMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String search = inputSearch.getText().toString();
if(search.equals("")) {
double latitude = 0;
double longitude = 0;
String mark;
// SQLite database handler
db = new TreeData(getApplicationContext());
Cursor locationCursor = db.getLocations();
locationCursor.moveToFirst();
do {
latitude = locationCursor.getDouble(locationCursor
.getColumnIndex(SQLiteHandler.KEY_TLATITUDE));
longitude = locationCursor.getDouble(locationCursor
.getColumnIndex(SQLiteHandler.KEY_TLONGITUDE));
mark = locationCursor.getString(locationCursor
.getColumnIndex(SQLiteHandler.KEY_TreeID));
LatLng location = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(location).title(mark));
} while (locationCursor.moveToNext());
}
ELSE If I select 'Excellent' from my AutoCompleteTextview and Click Button, It should shown only 'Excellent' Row Latitude and Longitude i.e ID 1,3,6 & 10
"This is not working"
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();
}
}
});}
To read my database I have used following code:
//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;
}
ERROR
FATAL EXCEPTION: main Process: com.example.bharat.plantnow, PID: 2124 java.lang.NullPointerException at com.example.bharat.plantnow.MapsActivity$1.onClick(MapsActivity.java:132) at android.view.View.performClick(View.java:4438) at android.view.View$PerformClick.run(View.java:18422) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method)