Thursday, April 11, 2013

Get details of Address returned from Geocoder



Modify MainActivity.java from last exercise "Search address by name with Geocoder, with Search Dialog", implement OnItemClickListener on the search result ListView.
package com.example.androidsearchgeocoder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.SearchManager;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

 ListView listResult;
 
 private ArrayAdapter<String> adapter;
 
 Geocoder geocoder;
 final static int maxResults = 5;
 List<Address> locationList;
 List<String> locationNameList;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  listResult = (ListView)findViewById(R.id.resultlist);
  
  handleIntent(getIntent());
  
  geocoder = new Geocoder(this, Locale.ENGLISH);
  
  locationNameList = new ArrayList<String>(); //empty in start
  adapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_spinner_item, locationNameList);
  listResult.setAdapter(adapter);
  listResult.setOnItemClickListener(listResultOnItemClickListener);
 }

 @Override
 protected void onNewIntent(Intent intent) {
  setIntent(intent);
  handleIntent(intent);
 }
 
    private void handleIntent(Intent intent) {
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            doSearch(query);
        }
    }

    private void doSearch(String query){
     
     try {
      locationList = geocoder.getFromLocationName(query, maxResults);
      
      if(locationList == null){
       Toast.makeText(getApplicationContext(), 
         "locationList == null", 
         Toast.LENGTH_LONG).show(); 
      }else{
       if(locationList.isEmpty()){
        Toast.makeText(getApplicationContext(), 
          "locationList is empty", 
          Toast.LENGTH_LONG).show(); 
       }else{
        Toast.makeText(getApplicationContext(), 
          "number of result: " + locationList.size(), 
          Toast.LENGTH_LONG).show();
      
        locationNameList.clear();
      
        for(Address i : locationList){
         if(i.getFeatureName() == null){
          locationNameList.add("unknown"); 
         }else{
          locationNameList.add(i.getFeatureName());
          
         } 
        }
      
        adapter.notifyDataSetChanged(); 
       } 
      } 
     } catch (IOException e) {
      Toast.makeText(getApplicationContext(), 
        "network unavailable or any other I/O problem occurs", 
        Toast.LENGTH_LONG).show();
      e.printStackTrace(); 
     }
  
     String result ="Finished - " + query;

     //I can't close search dialog, so I call it again to keep it active.
     //Otherwise, user see the search dialog opened, but cannot search in seconf time. 
     onSearchRequested();
     
    }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
        case R.id.search:
            onSearchRequested();
            return true;
        default:
            return false;    
  }
 }
 
 OnItemClickListener listResultOnItemClickListener =
   new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
      int position, long id) {
     openAddressDialog(locationList.get(position));
    }   
 };
 
 private void openAddressDialog(Address address){
  AlertDialog.Builder addressDialog = 
    new AlertDialog.Builder(MainActivity.this);
  addressDialog.setTitle(address.getFeatureName());
  String msg = 
    "Country: " + address.getCountryName() + "\n"
    + "AdminArea: " + address.getAdminArea() + "\n"
    + "Longitude: " + address.getLongitude() + "\n"
    + "Latitude: " + address.getLatitude();
  
  addressDialog.setMessage(msg);
  addressDialog.show();
 }
    
}


download filesDownload the files.

No comments: