Created
December 9, 2011 22:28
-
-
Save onlyangel/1453601 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.onlyangel.sclbits.rosh.activities.veiculos; | |
| import android.app.ListActivity; | |
| import android.content.Intent; | |
| import android.content.SharedPreferences; | |
| import android.database.Cursor; | |
| import android.os.Bundle; | |
| import android.view.ContextMenu; | |
| import android.view.ContextMenu.ContextMenuInfo; | |
| import android.view.Menu; | |
| import android.view.MenuInflater; | |
| import android.view.MenuItem; | |
| import android.view.View; | |
| import android.widget.AdapterView.AdapterContextMenuInfo; | |
| import android.widget.ListView; | |
| import android.widget.SimpleCursorAdapter; | |
| import com.onlyangel.sclbits.rosh.R; | |
| import com.onlyangel.sclbits.rosh.database.adapters.CarrosDbAdapter; | |
| import com.onlyangel.sclbits.rosh.tools.Generales; | |
| public class VeiculosListActivity extends ListActivity { | |
| private CarrosDbAdapter dbHelper; | |
| private static final int ACTIVITY_CREATE = 0; | |
| private static final int ACTIVITY_EDIT = 1; | |
| public static final int ACTIVITY_OPEN = 2; | |
| private static final int SETDEFAULCARR_ID = Menu.FIRST + 1; | |
| private static final int DELETE_ID = Menu.FIRST + 2; | |
| private Cursor cursor; | |
| /** | |
| * @see android.app.Activity#onCreate(Bundle) | |
| */ | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| this.setContentView(R.layout.veiculos_list); | |
| this.getListView().setDividerHeight(2); | |
| dbHelper = new CarrosDbAdapter(this); | |
| dbHelper.open(); | |
| fillData(); | |
| registerForContextMenu(getListView()); | |
| } | |
| private void fillData() { | |
| cursor = dbHelper.fetchAllCarros(); | |
| startManagingCursor(cursor); | |
| String[] from = new String[] { CarrosDbAdapter.KEY_NOMBRE }; | |
| int[] to = new int[] { R.id.veiculos_list_label }; | |
| // Now create an array adapter and set it to display using our row | |
| SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.veiculos_list_row, cursor, from, to); | |
| setListAdapter(notes); | |
| } | |
| // Create the menu based on the XML defintion | |
| @Override | |
| public boolean onCreateOptionsMenu(Menu menu) { | |
| MenuInflater inflater = getMenuInflater(); | |
| inflater.inflate(R.menu.veiculos_list_menu, menu); | |
| return true; | |
| } | |
| // Reaction to the menu selection | |
| @Override | |
| public boolean onMenuItemSelected(int featureId, MenuItem item) { | |
| switch (item.getItemId()) { | |
| case R.id.menu_list_insert: | |
| createCarro(); | |
| return true; | |
| } | |
| return super.onMenuItemSelected(featureId, item); | |
| } | |
| @Override | |
| public boolean onOptionsItemSelected(MenuItem item) { | |
| switch (item.getItemId()) { | |
| case R.id.menu_list_insert: | |
| createCarro(); | |
| return true; | |
| } | |
| return super.onOptionsItemSelected(item); | |
| } | |
| @Override | |
| public boolean onContextItemSelected(MenuItem item) { | |
| AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); | |
| switch (item.getItemId()) { | |
| case DELETE_ID: | |
| dbHelper.deleteTodo(info.id); | |
| fillData(); | |
| return true; | |
| case SETDEFAULCARR_ID: | |
| SharedPreferences settings = getSharedPreferences(Generales.PREFS_FILENAME, 0); | |
| SharedPreferences.Editor editor = settings.edit(); | |
| editor.putLong(Generales.CAR_DEFAULT_ID, info.id); | |
| editor.commit(); | |
| Generales.CAR_DEFAULT = info.id; | |
| return true; | |
| } | |
| return super.onContextItemSelected(item); | |
| } | |
| private void createCarro() { | |
| Intent i = new Intent(this, VeiculosDetalles.class); | |
| startActivityForResult(i, ACTIVITY_CREATE); | |
| } | |
| // ListView and view (row) on which was clicked, position and | |
| @Override | |
| protected void onListItemClick(ListView l, View v, int position, long id) { | |
| super.onListItemClick(l, v, position, id); | |
| Intent i = new Intent(this, VeiculosDetalles.class); | |
| i.putExtra(CarrosDbAdapter.KEY_ROWID, id); | |
| // Activity returns an result if called with startActivityForResult | |
| startActivityForResult(i, ACTIVITY_EDIT); | |
| } | |
| // Called with the result of the other activity | |
| // requestCode was the origin request code send to the activity | |
| // resultCode is the return code, 0 is everything is ok | |
| // intend can be use to get some data from the caller | |
| @Override | |
| protected void onActivityResult(int requestCode, int resultCode, | |
| Intent intent) { | |
| super.onActivityResult(requestCode, resultCode, intent); | |
| fillData(); | |
| } | |
| @Override | |
| public void onCreateContextMenu(ContextMenu menu, View v, | |
| ContextMenuInfo menuInfo) { | |
| super.onCreateContextMenu(menu, v, menuInfo); | |
| menu.add(0, SETDEFAULCARR_ID, 0, R.string.veiculo_list_menu_setdefault); | |
| menu.add(0, DELETE_ID, 0, R.string.veiculo_list_menu_delete); | |
| } | |
| @Override | |
| protected void onDestroy() { | |
| super.onDestroy(); | |
| if (dbHelper != null) { | |
| dbHelper.close(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment