package com.AndroidgetFromLocationName;
import java.io.IOException;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class AndroidgetFromLocationNameActivity extends Activity {
EditText searchText;
Button searchButton;
ListView listviewResult;
Geocoder myGeocoder;
final static int MAX_RESULT = 10;
final static String DEFAULT_SEARCH = "谷歌";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
searchText = (EditText)findViewById(R.id.searchtext);
searchText.setText(DEFAULT_SEARCH);
searchButton = (Button)findViewById(R.id.searchbutton);
listviewResult = (ListView)findViewById(R.id.listviewresult);
searchButton.setOnClickListener(searchButtonOnClickListener);
myGeocoder = new Geocoder(this);
//for API Level 9 or higher
if (!Geocoder.isPresent()){
Toast.makeText(AndroidgetFromLocationNameActivity.this,
"Sorry! Geocoder service not Present.",
Toast.LENGTH_LONG).show();
}
}
Button.OnClickListener searchButtonOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String searchString = searchText.getText().toString();
searchFromLocationName(searchString);
}};
private void searchFromLocationName(String name){
try {
List<Address> result
= myGeocoder.getFromLocationName(name, MAX_RESULT);
if ((result == null)||(result.isEmpty())){
Toast.makeText(AndroidgetFromLocationNameActivity.this,
"No matches were found or there is no backend service!",
Toast.LENGTH_LONG).show();
}else{
MyArrayAdapter adapter = new MyArrayAdapter(this,
android.R.layout.simple_list_item_1, result);
listviewResult.setAdapter(adapter);
Toast.makeText(AndroidgetFromLocationNameActivity.this,
"Finished!",
Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(AndroidgetFromLocationNameActivity.this,
"The network is unavailable or any other I/O problem occurs!",
Toast.LENGTH_LONG).show();
}
}
public class MyArrayAdapter extends ArrayAdapter<Address> {
Context mycontext;
public MyArrayAdapter(Context context, int textViewResourceId,
List<Address> objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
mycontext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
int maxAddressLineIndex = getItem(position).getMaxAddressLineIndex();
String addressLine = "";
for (int j = 0; j <= maxAddressLineIndex; j++){
addressLine += getItem(position).getAddressLine(j) + ",";
}
TextView rowAddress = new TextView(mycontext);
rowAddress.setText(addressLine);
return rowAddress;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<EditText
android:id="@+id/searchtext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/searchbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Search"
/>
<ListView
android:id="@+id/listviewresult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
next post:
- get latitude and longitude from android.location.Address
if (!Geocoder.isPresent())
ReplyDeletei am gettin error at this line
i have solved that error by upgrading from 2.2 to 2.3.now in emulator when i am pressing the search button i am getting "The network is unavailable or any other I/O problem occurs......please help me
ReplyDeletekrishna,
ReplyDeleteIf you run on emulator, please make sure the 3G Data icon is on, and it can connect to internet. May be you have to wait longer, or re-start the emulator.
thanks for the reply eric...3g icon is on and it connects to internet.i can say that cause google maps are coming on the emulator....still same problem even after restarting the emulator............
ReplyDeletei think der was some problem in try block.the execution comes to try and it directly prints the statement in catch block......
ReplyDeleteHey there, If I would like to limit the search only within a country, how am I suppose to do ? Anyone, any idea?
ReplyDeleteI think you can direct enter the country name in the search string.
DeleteCan i know GetView is for what?
ReplyDeleteIn your custom adapter, getView() is the place you prepare the view display on the specified position. The system will insert your view in the allocated position of the associated view, listviewResult in this example.
Delete