Select to view content in your preferred language

Populate AlertDialog from Webservice layer?

3127
7
03-27-2013 02:09 PM
JohnAllen
Deactivated User
I have an actionbar on may mapscreen. (Image 1)

When I select the search icon (or menu item) on my actionbar and alertdialog appears with names that I manually entered into my code (String Array).

How do I populate the alert dialog with location names (names from a layer in the mapservice that has a Names field) from a map service layer? (Image 2)

If you could post some sample code and explain, that would be great.

Thanks!

John

Code:

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();
0 Kudos
7 Replies
by Anonymous User
Not applicable
Original User: tmsbn90

1. Run a query task on the layer. Put the query task methods inside the Async task class to avoid blocking the UI thread.
2. queryTask.execute() method will return a featureset. From the featureset use featureSet.getGraphics() extract an array of graphics.
3.  You can call the getAttributeValue("YOUR_NAME_FIELD") method on each item of the array of graphics to get the names.
4. Put these names in an string array to use in your dialog builder.

Hope this helps. See the query task sample for more info.
0 Kudos
JohnAllen
Deactivated User
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);    
   }  
  }
 }   
} 
0 Kudos
by Anonymous User
Not applicable
Original User: tmsbn90

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);    
   }  
  }
 }   
} 



Check the line
String location1 = graphic.getAttributeValue("Name").toString();
the value of graphic is null here because you are supposed to put this statement as well the alert dialog statements inside the onPostExecute method of the asyncClass task. You have written these statements just after initialising an asyntask object which is wrong because asynctask takes some time run the query method and return a result. Please read on the documentation of asynctask.
0 Kudos
JohnAllen
Deactivated User
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)

0 Kudos
JohnAllen
Deactivated User
Ok, I finally got something to work. (Image3)

I can return only one location name: Anchor Marina

I need to return all the location names not just one.

I've tried looping through the graphic array, but I couldn't get all my name values to return.

If someone could give me some pointers to have all my graphic names to appear I would appreciate it.

Thanks,

Here's my onPostExecute for only one location to appear:


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();
    }
  
   }

0 Kudos
by Anonymous User
Not applicable
Original User: tmsbn90

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)



Check the for loop statement. Its supposed to be grs.length-1(you know.. because arrays go from 0 to length-1 😛 )

for (int i = 0; i <= grs.length-1; i++) {
Graphic g = grs;
location1 = g.getAttributeValue("Name");

}
0 Kudos
JohnAllen
Deactivated User
Follow up....

I figured it out with the help from a friend.
FYI - The declared names of objects are a little different in the code below when compared to the code I previously posted above, but you should be able to follow.

Work flow - Starting from the onPostExecute method:
1. If result is not null..."do something".
2. Create a Graphics Array and put the FeatureSet results in the Array.
3. Create/Initialize Alert Dialog Builder.
4. Set the Title for the Alert Dialog.
5. Create a String Array and declare the length of the Array, which is the length of the Graphics Array in step two above.
6. Create a loop that goes through each column of the graphic array.
7. Declare a Graphic object that is equal to the graphic array loop.
8. Create a String Array and put the graphics attribute values "Name" in the String Array as a string type.
9. Set the Alert Dialog items as that of the String Array, in this case "arr" & create a new Dialog Interface.OnClickListener
10. Create the Alert Dialog.
11. Show the Alert Dialog.

The code below solves my problem stated in opening post:


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();
   }
  }
 }
}




Thomas, thanks for giving me information.

Hope this helps others.
0 Kudos