BartRunnerAndroid/src/com/dougkeen/bart/AddRouteActivity.java

102 lines
3.0 KiB
Java
Raw Normal View History

2011-05-23 18:59:34 +00:00
package com.dougkeen.bart;
import com.dougkeen.bart.data.RoutesColumns;
import com.dougkeen.bart.model.Constants;
import com.dougkeen.bart.model.Station;
2011-05-23 18:59:34 +00:00
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
2011-05-23 18:59:34 +00:00
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.Toast;
public class AddRouteActivity extends Activity {
2011-05-23 18:59:34 +00:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_favorite);
SpinnerAdapter originSpinnerAdapter = new ArrayAdapter<Station>(this,
R.layout.simple_spinner_item, Station.getStationList());
2011-05-23 18:59:34 +00:00
((Spinner) findViewById(R.id.origin_spinner))
.setAdapter(originSpinnerAdapter);
SpinnerAdapter destinationSpinnerAdapter = new ArrayAdapter<Station>(
this, R.layout.simple_spinner_item, Station.getStationList());
2011-05-23 18:59:34 +00:00
((Spinner) findViewById(R.id.destination_spinner))
.setAdapter(destinationSpinnerAdapter);
Button saveButton = (Button) findViewById(R.id.saveButton);
saveButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onSaveButtonClick();
}
});
Button cancelButton = (Button) findViewById(R.id.cancelButton);
cancelButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setResult(RESULT_CANCELED);
finish();
}
});
}
protected void onSaveButtonClick() {
Station origin = (Station) ((Spinner) findViewById(R.id.origin_spinner))
.getSelectedItem();
Station destination = (Station) ((Spinner) findViewById(R.id.destination_spinner))
.getSelectedItem();
if (origin == null) {
Toast.makeText(this, com.dougkeen.bart.R.string.error_null_origin,
Toast.LENGTH_LONG);
return;
}
if (destination == null) {
Toast.makeText(this,
com.dougkeen.bart.R.string.error_null_destination,
Toast.LENGTH_LONG);
return;
}
if (origin.equals(destination)) {
Toast.makeText(
this,
com.dougkeen.bart.R.string.error_matching_origin_and_destination,
Toast.LENGTH_LONG);
return;
}
ContentValues values = new ContentValues();
values.put(RoutesColumns.FROM_STATION.string, origin.abbreviation);
values.put(RoutesColumns.TO_STATION.string, destination.abbreviation);
2011-05-23 18:59:34 +00:00
Uri newUri = getContentResolver().insert(
Constants.FAVORITE_CONTENT_URI, values);
if (((CheckBox) findViewById(R.id.return_checkbox)).isChecked()) {
values = new ContentValues();
values.put(RoutesColumns.FROM_STATION.string,
destination.abbreviation);
values.put(RoutesColumns.TO_STATION.string, origin.abbreviation);
getContentResolver().insert(Constants.FAVORITE_CONTENT_URI, values);
}
2011-05-23 18:59:34 +00:00
setResult(RESULT_OK, (new Intent()).setAction(newUri.toString()));
finish();
}
}