public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.actionbar_menu, menu); return true; } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()){ case R.id.SearchTool: AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); //This is where I manually entered in my location names. final String[] locations = {"Blue Water", "Big Fish ", "Beaverdam", "Wake", "Dock Side", "Oceanview", "Beach Front", "Lake Side", "Boaters Choice"}; dialogBuilder.setTitle("Select a Location:"); dialogBuilder.setItems(locations, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Do something when location is selected within the alertdialog. } }); AlertDialog alertDialog = dialogBuilder.create(); alertDialog.show();
import com.esri.android.map.GraphicsLayer; import com.esri.android.map.MapView; import com.esri.core.map.FeatureSet; import com.esri.core.map.Graphic; import com.esri.core.tasks.ags.query.Query; import com.esri.core.tasks.ags.query.QueryTask; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.AsyncTask; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; //MapScreen is a subclass of Activity. public class MapScreen extends Activity { public MapView map; boolean AlrtDialogQeury = true; GraphicsLayer gl; Graphic graphic; String targetServerURL = "MAP SERVICE URL"; /* Called when the Activity is first created. Remember the Activity and its methods are created first. Then the MapScreen class is used. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mapscreen); map = (MapView) findViewById(R.id.map); } public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.actionbar_menu, menu); return true; } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()){ case R.id.SearchTool: String targetLayer = targetServerURL; String[] queryParams = { targetLayer, "Name" }; AsyncQueryTask ayncQuery = new AsyncQueryTask(); ayncQuery.execute(queryParams); AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); String location1 = graphic.getAttributeValue("Name").toString(); String[] location2 = {location1}; dialogBuilder.setTitle("Select a Location"); dialogBuilder.setItems(location2, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Do something when clicked. } }); AlertDialog alertDialog = dialogBuilder.create(); alertDialog.show(); } return true; } private class AsyncQueryTask extends AsyncTask<String, Void, FeatureSet> { protected FeatureSet doInBackground(String... queryParams) { if (queryParams == null || queryParams.length <= 1) return null; //My query may be wrong. String url = queryParams[0]; Query query = new Query(); String whereClause = queryParams[1]; query.setWhere(whereClause); QueryTask qTask = new QueryTask(url); FeatureSet fs = null; try { fs = qTask.execute(query); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return fs; } return fs; } //I think this is where I can't make it work. protected void onPostExecute(FeatureSet fs) { if (fs != null) { Graphic[] grs = fs.getGraphics(); gl.addGraphics(grs); } } } }
Thomas,
Thanks for the information.
I tried implementing your steps into my code, but I when I select the search icon (menu item) my app crashes.
Using graphic arrays and string arrays has me confused.
I think I'm not doing something right with the onPostExecute or it might be that my query is jacked up.
Got any advice?
Thanks for your help.
Here is my code:
import com.esri.android.map.GraphicsLayer; import com.esri.android.map.MapView; import com.esri.core.map.FeatureSet; import com.esri.core.map.Graphic; import com.esri.core.tasks.ags.query.Query; import com.esri.core.tasks.ags.query.QueryTask; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.AsyncTask; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; //MapScreen is a subclass of Activity. public class MapScreen extends Activity { public MapView map; boolean AlrtDialogQeury = true; GraphicsLayer gl; Graphic graphic; String targetServerURL = "MAP SERVICE URL"; /* Called when the Activity is first created. Remember the Activity and its methods are created first. Then the MapScreen class is used. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mapscreen); map = (MapView) findViewById(R.id.map); } public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.actionbar_menu, menu); return true; } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()){ case R.id.SearchTool: String targetLayer = targetServerURL; String[] queryParams = { targetLayer, "Name" }; AsyncQueryTask ayncQuery = new AsyncQueryTask(); ayncQuery.execute(queryParams); AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); String location1 = graphic.getAttributeValue("Name").toString(); String[] location2 = {location1}; dialogBuilder.setTitle("Select a Location"); dialogBuilder.setItems(location2, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Do something when clicked. } }); AlertDialog alertDialog = dialogBuilder.create(); alertDialog.show(); } return true; } private class AsyncQueryTask extends AsyncTask<String, Void, FeatureSet> { protected FeatureSet doInBackground(String... queryParams) { if (queryParams == null || queryParams.length <= 1) return null; //My query may be wrong. String url = queryParams[0]; Query query = new Query(); String whereClause = queryParams[1]; query.setWhere(whereClause); QueryTask qTask = new QueryTask(url); FeatureSet fs = null; try { fs = qTask.execute(query); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return fs; } return fs; } //I think this is where I can't make it work. protected void onPostExecute(FeatureSet fs) { if (fs != null) { Graphic[] grs = fs.getGraphics(); gl.addGraphics(grs); } } } }
package com.dhec.MPOS; import com.esri.android.map.MapView; import com.esri.core.geometry.Envelope; import com.esri.core.geometry.SpatialReference; import com.esri.core.map.FeatureSet; import com.esri.core.map.Graphic; import com.esri.core.tasks.ags.query.Query; import com.esri.core.tasks.ags.query.QueryTask; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.AsyncTask; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.Toast; //MapScreen is a subclass of Activity. public class MapScreen extends Activity { public MapView map; String targetServerURL = "MAP SERVER HERE"; /* * Called when the Activity is first created. Remember the Activity and its * methods are created first. Then the MapScreen class is used. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mapscreen); map = (MapView) findViewById(R.id.map); } public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.actionbar_menu, menu); return true; } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.SearchTool: String targetLayer = targetServerURL; String[] queryParams = { targetLayer, "Name" }; AsyncQueryTask ayncQuery = new AsyncQueryTask(); ayncQuery.execute(queryParams); } return false; } private class AsyncQueryTask extends AsyncTask<String, Void, FeatureSet> { /** * First member in parameter array is the query URL; second member is * the where clause. */ protected FeatureSet doInBackground(String... queryParams) { if (queryParams == null || queryParams.length <= 1) return null; String url = queryParams[0]; Query query = new Query(); String whereClause = queryParams[1]; query.setOutFields(queryParams); SpatialReference sr = SpatialReference.create(4326); query.setGeometry(new Envelope(-9246560.42264577, 3775709.72082467, -8746857.60892366, 4180002.76740144)); query.setOutSpatialReference(sr); query.setReturnGeometry(true); query.setWhere(whereClause); QueryTask qTask = new QueryTask(url); FeatureSet fs = null; try { fs = qTask.execute(query); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return fs; } return fs; } protected void onPostExecute(FeatureSet result) { Object location1 = null; String message = "No result comes back"; Graphic[] grs = result.getGraphics(); if (result != null && grs.length > 0){ for (int i = 0; i <= grs.length; i++) { Graphic g = grs; location1 = g.getAttributeValue("Name"); } AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MapScreen.this); String[] location2 = { location1.toString() }; dialogBuilder.setTitle("Select a Location:"); dialogBuilder.setItems(marinas, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Do something once clicked } }); AlertDialog alertDialog = dialogBuilder.create(); alertDialog.show(); } else{ Toast toast = Toast.makeText(MapScreen.this, message, Toast.LENGTH_LONG); toast.show(); } } } }
04-09 13:36:09.895: W/dalvikvm(15894): threadid=1: thread exiting with uncaught exception (group=0x40ab3210) 04-09 13:36:09.902: E/AndroidRuntime(15894): FATAL EXCEPTION: main 04-09 13:36:09.902: E/AndroidRuntime(15894): java.lang.ArrayIndexOutOfBoundsException: length=0; index=0 04-09 13:36:09.902: E/AndroidRuntime(15894): at com.dhec.MPOS.MapScreen$AsyncQueryTask.onPostExecute(MapScreen.java:98) 04-09 13:36:09.902: E/AndroidRuntime(15894): at com.dhec.MPOS.MapScreen$AsyncQueryTask.onPostExecute(MapScreen.java:1) 04-09 13:36:09.902: E/AndroidRuntime(15894): at android.os.AsyncTask.finish(AsyncTask.java:602) 04-09 13:36:09.902: E/AndroidRuntime(15894): at android.os.AsyncTask.access$600(AsyncTask.java:156) 04-09 13:36:09.902: E/AndroidRuntime(15894): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615) 04-09 13:36:09.902: E/AndroidRuntime(15894): at android.os.Handler.dispatchMessage(Handler.java:99) 04-09 13:36:09.902: E/AndroidRuntime(15894): at android.os.Looper.loop(Looper.java:137) 04-09 13:36:09.902: E/AndroidRuntime(15894): at android.app.ActivityThread.main(ActivityThread.java:4722) 04-09 13:36:09.902: E/AndroidRuntime(15894): at java.lang.reflect.Method.invokeNative(Native Method) 04-09 13:36:09.902: E/AndroidRuntime(15894): at java.lang.reflect.Method.invoke(Method.java:511) 04-09 13:36:09.902: E/AndroidRuntime(15894): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787) 04-09 13:36:09.902: E/AndroidRuntime(15894): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554) 04-09 13:36:09.902: E/AndroidRuntime(15894): at dalvik.system.NativeStart.main(Native Method)
protected void onPostExecute(FeatureSet result) { if (result != null) { Graphic[] graphics = result.getGraphics(); gl.addGraphics(graphics); Graphic g = graphics[0]; String location1 = (String) g.getAttributeValue("Name"); AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MapScreen.this); String[] location2 = {location1}; dialogBuilder.setTitle("Select a Location:"); dialogBuilder.setItems(location2, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Do something once clicked } }); AlertDialog alertDialog = dialogBuilder.create(); alertDialog.show(); } }
I've tried everything. I can't get it to work.
My Alertdialog will not populate with the names of locations, from the Name field, from my layer, within a map service.
What am I doing wrong SPECIFICALLY?
FYI - I've referred to the query example.
Any help is appreciated.
Thanks,
This is my new code:
package com.dhec.MPOS; import com.esri.android.map.MapView; import com.esri.core.geometry.Envelope; import com.esri.core.geometry.SpatialReference; import com.esri.core.map.FeatureSet; import com.esri.core.map.Graphic; import com.esri.core.tasks.ags.query.Query; import com.esri.core.tasks.ags.query.QueryTask; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.AsyncTask; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.Toast; //MapScreen is a subclass of Activity. public class MapScreen extends Activity { public MapView map; String targetServerURL = "MAP SERVER HERE"; /* * Called when the Activity is first created. Remember the Activity and its * methods are created first. Then the MapScreen class is used. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mapscreen); map = (MapView) findViewById(R.id.map); } public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.actionbar_menu, menu); return true; } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.SearchTool: String targetLayer = targetServerURL; String[] queryParams = { targetLayer, "Name" }; AsyncQueryTask ayncQuery = new AsyncQueryTask(); ayncQuery.execute(queryParams); } return false; } private class AsyncQueryTask extends AsyncTask<String, Void, FeatureSet> { /** * First member in parameter array is the query URL; second member is * the where clause. */ protected FeatureSet doInBackground(String... queryParams) { if (queryParams == null || queryParams.length <= 1) return null; String url = queryParams[0]; Query query = new Query(); String whereClause = queryParams[1]; query.setOutFields(queryParams); SpatialReference sr = SpatialReference.create(4326); query.setGeometry(new Envelope(-9246560.42264577, 3775709.72082467, -8746857.60892366, 4180002.76740144)); query.setOutSpatialReference(sr); query.setReturnGeometry(true); query.setWhere(whereClause); QueryTask qTask = new QueryTask(url); FeatureSet fs = null; try { fs = qTask.execute(query); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return fs; } return fs; } protected void onPostExecute(FeatureSet result) { Object location1 = null; String message = "No result comes back"; Graphic[] grs = result.getGraphics(); if (result != null && grs.length > 0){ for (int i = 0; i <= grs.length; i++) { Graphic g = grs; location1 = g.getAttributeValue("Name"); } AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MapScreen.this); String[] location2 = { location1.toString() }; dialogBuilder.setTitle("Select a Location:"); dialogBuilder.setItems(marinas, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Do something once clicked } }); AlertDialog alertDialog = dialogBuilder.create(); alertDialog.show(); } else{ Toast toast = Toast.makeText(MapScreen.this, message, Toast.LENGTH_LONG); toast.show(); } } } }
This is my log cat:
04-09 13:36:09.895: W/dalvikvm(15894): threadid=1: thread exiting with uncaught exception (group=0x40ab3210) 04-09 13:36:09.902: E/AndroidRuntime(15894): FATAL EXCEPTION: main 04-09 13:36:09.902: E/AndroidRuntime(15894): java.lang.ArrayIndexOutOfBoundsException: length=0; index=0 04-09 13:36:09.902: E/AndroidRuntime(15894): at com.dhec.MPOS.MapScreen$AsyncQueryTask.onPostExecute(MapScreen.java:98) 04-09 13:36:09.902: E/AndroidRuntime(15894): at com.dhec.MPOS.MapScreen$AsyncQueryTask.onPostExecute(MapScreen.java:1) 04-09 13:36:09.902: E/AndroidRuntime(15894): at android.os.AsyncTask.finish(AsyncTask.java:602) 04-09 13:36:09.902: E/AndroidRuntime(15894): at android.os.AsyncTask.access$600(AsyncTask.java:156) 04-09 13:36:09.902: E/AndroidRuntime(15894): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615) 04-09 13:36:09.902: E/AndroidRuntime(15894): at android.os.Handler.dispatchMessage(Handler.java:99) 04-09 13:36:09.902: E/AndroidRuntime(15894): at android.os.Looper.loop(Looper.java:137) 04-09 13:36:09.902: E/AndroidRuntime(15894): at android.app.ActivityThread.main(ActivityThread.java:4722) 04-09 13:36:09.902: E/AndroidRuntime(15894): at java.lang.reflect.Method.invokeNative(Native Method) 04-09 13:36:09.902: E/AndroidRuntime(15894): at java.lang.reflect.Method.invoke(Method.java:511) 04-09 13:36:09.902: E/AndroidRuntime(15894): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787) 04-09 13:36:09.902: E/AndroidRuntime(15894): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554) 04-09 13:36:09.902: E/AndroidRuntime(15894): at dalvik.system.NativeStart.main(Native Method)
package com.dhec.MPOS; import com.esri.android.map.MapView; import com.esri.core.map.FeatureSet; import com.esri.core.map.Graphic; import com.esri.core.tasks.ags.query.Query; import com.esri.core.tasks.ags.query.QueryTask; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.AsyncTask; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; //MapScreen is a subclass of Activity. public class MapScreen extends Activity { public MapView map; /* * Called when the Activity is first created. Remember the Activity and its * methods are created first. Then the MapScreen class is used. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mapscreen); map = (MapView) findViewById(R.id.map); } public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.actionbar_menu, menu); return true; } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.SearchTool: Query query = new Query(); query.setWhere("State = 'South Carolina'"); query.setReturnGeometry(true); String[] outfields = new String[] { "Name" }; query.setOutFields(outfields); query.setOutSpatialReference(map.getSpatialReference()); Query[] queryParams = { query }; AsyncQueryTask qt = new AsyncQueryTask(); qt.execute(queryParams); } return false; } private class AsyncQueryTask extends AsyncTask<Query, Void, FeatureSet> { protected FeatureSet doInBackground(Query... params) { if (params.length > 0) { Query query = params[0]; QueryTask queryTask = new QueryTask( "Put MAP SERVER URL HERE"); try { FeatureSet fs = queryTask.execute(query); return fs; } catch (Exception e) { e.printStackTrace(); return null; } } return null; } protected void onPostExecute(FeatureSet result) { if (result != null) { Graphic[] graphics = result.getGraphics(); AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MapScreen.this); dialogBuilder.setTitle("Select a Location:"); String[] arr = new String[graphics.length]; for (int i = 0; i < graphics.length; i++) { Graphic g = graphics; arr = (String) g.getAttributeValue("Name"); } dialogBuilder.setItems(arr, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog,int which) { //This is where you tell the application to do something once you click //the name within the alert dialog. } }); AlertDialog alertDialog = dialogBuilder.create(); alertDialog.show(); } } } }