package com.eunchul.zipcode;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class ZipcodeFinderActivity extends Activity {
ListView zipcodeListView;
ArrayAdapter zipcodeArrayAdapter;
ArrayList zipcodes;
ProgressDialog progressDialog;
private Handler handler = new Handler();
final private int PROGRESS_DIALOG = 0;
final private int ZIPCODE_DIALOG = 21;
Zipcode selectedZipcode;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//final EditText keyword = (EditText)findViewById(R.id.keyword);
final Button searchButton = (Button)findViewById(R.id.search_button);
zipcodeListView = (ListView)findViewById(R.id.zipcode_list_view);
searchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Thread thread = new Thread(null, getZipcodes);
thread.start();
showDialog(PROGRESS_DIALOG);
}
});
zipcodeListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView _av, View _v, int _index, long arg3) {
selectedZipcode = zipcodes.get(_index);
Log.d(this.getClass().getName(), selectedZipcode.getAddress());
Log.d(this.getClass().getName(), selectedZipcode.getZipcode());
showDialog(ZIPCODE_DIALOG);
}
});
}
private Runnable getZipcodes = new Runnable() {
public void run() {
try {
final EditText keyword = (EditText)findViewById(R.id.keyword);
Log.d("getZipcodes", "executed...");
ZipcodeFactory zf = new ZipcodeFactory();
zipcodes = zf.search(keyword.getText().toString());
handler.post(updateResults);
} catch (Exception e) {
Log.e("getZipcodes", e.toString());
}
}
};
private Runnable updateResults = new Runnable() {
public void run () {
if (zipcodes != null) {
Log.d("updateResults", "executed...");
int layoutId = android.R.layout.simple_list_item_1;
zipcodeArrayAdapter = new ArrayAdapter(ZipcodeFinderActivity.this, layoutId, zipcodes);
zipcodeListView.setAdapter(zipcodeArrayAdapter);
} else {
showNotFoundZipcodes();
}
progressDialog.dismiss();
ZipcodeFinderActivity.this.removeDialog(PROGRESS_DIALOG);
}
};
private void showNotFoundZipcodes() {
Toast toast = Toast.makeText(this, "Not found", Toast.LENGTH_SHORT);
toast.show();
}
@Override
protected Dialog onCreateDialog (int id) {
switch (id) {
case (PROGRESS_DIALOG):
progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage("Now loading..");
return progressDialog;
case (ZIPCODE_DIALOG):
LayoutInflater li = LayoutInflater.from(this);
View zipcodeDialogView = li.inflate(R.layout.zipcode_dialog, null);
AlertDialog.Builder zipcodeDialog = new AlertDialog.Builder(this);
zipcodeDialog.setTitle("Detail");
zipcodeDialog.setView(zipcodeDialogView);
return zipcodeDialog.create();
}
return null;
}
@Override
protected void onPrepareDialog (int id, Dialog dialog) {
switch (id) {
case(ZIPCODE_DIALOG):
AlertDialog zipcodeDialog = (AlertDialog)dialog;
zipcodeDialog.setTitle(selectedZipcode.getZipcode());
TextView tvAddress = (TextView)zipcodeDialog.findViewById(R.id.dialog_address);
TextView tvZipcode = (TextView)zipcodeDialog.findViewById(R.id.dialog_zipcode);
tvAddress.setText(this.getString(R.string.zipcode_address) + " " + selectedZipcode.getAddress());
tvZipcode.setText(this.getString(R.string.zipcode_zipcode) + " " + selectedZipcode.getZipcode());
break;
}
}
}