Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2012
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.95 KB | None | 0 0
  1. package fake.package.namespace.notes;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import android.app.AlertDialog;
  6. import android.app.LoaderManager;
  7. import android.content.CursorLoader;
  8. import android.content.DialogInterface;
  9. import android.content.Loader;
  10. import android.database.Cursor;
  11. import android.graphics.Shader.TileMode;
  12. import android.graphics.drawable.BitmapDrawable;
  13. import android.os.Build;
  14. import android.os.Bundle;
  15. import android.support.v4.app.LoaderManager.LoaderCallbacks;
  16. import android.text.Editable;
  17. import android.view.View;
  18. import android.widget.ArrayAdapter;
  19. import android.widget.EditText;
  20. import android.widget.ListView;
  21. import android.widget.SimpleCursorAdapter;
  22.  
  23. import com.actionbarsherlock.app.SherlockFragmentActivity;
  24. import com.actionbarsherlock.app.SherlockListFragment;
  25. import com.actionbarsherlock.view.ActionMode;
  26. import com.actionbarsherlock.view.Menu;
  27. import com.actionbarsherlock.view.MenuInflater;
  28. import com.actionbarsherlock.view.MenuItem;
  29.  
  30. public class NotesActivity extends SherlockFragmentActivity {
  31.  
  32.     /** Called when the activity is first created. */
  33.     @Override
  34.     public void onCreate(Bundle savedInstanceState) {
  35.         super.onCreate(savedInstanceState);
  36.         setContentView(R.layout.main);
  37.  
  38.         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
  39.             BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.bg_striped);
  40.             bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
  41.             getSupportActionBar().setBackgroundDrawable(bg);
  42.  
  43.             BitmapDrawable bgSplit = (BitmapDrawable)getResources().getDrawable(R.drawable.bg_striped_split_img);
  44.             bgSplit.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
  45.             getSupportActionBar().setSplitBackgroundDrawable(bgSplit);
  46.         }
  47.  
  48.         // Create the list fragment and add it as our sole content.
  49.         if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
  50.             list = new ArrayListFragment();
  51.             getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit();
  52.         }
  53.  
  54.        
  55.     }
  56.        
  57.         public ArrayListFragment list;
  58.    
  59.         @Override
  60.         public boolean onCreateOptionsMenu(Menu menu) {
  61.             MenuInflater inflater = getSupportMenuInflater();
  62.             inflater.inflate(R.menu.menu, menu);
  63.             return true;
  64.         }
  65.        
  66.         @Override
  67.         public boolean onOptionsItemSelected(MenuItem item) {
  68.             // Handle item selection
  69.             switch (item.getItemId()) {
  70.                 case R.id.add_note:
  71.                     addNote();
  72.                     return true;
  73.                 default:
  74.                     return super.onOptionsItemSelected(item);
  75.             }
  76.         }
  77.        
  78.         public void addNote() {
  79.             AlertDialog.Builder alert = new AlertDialog.Builder(this);
  80.  
  81.             alert.setTitle("Add new note");
  82.  
  83.             // Set an EditText view to get user input
  84.             final EditText input = new EditText(this);
  85.             alert.setView(input);
  86.  
  87.             alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
  88.             public void onClick(DialogInterface dialog, int whichButton) {
  89.               Editable value = input.getText();
  90.                   list.notes.add(value.toString());
  91.                   list.myAdapter.notifyDataSetChanged();
  92.                   return;
  93.               }
  94.             });
  95.  
  96.             alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  97.               public void onClick(DialogInterface dialog, int whichButton) {
  98.                   return;
  99.               }
  100.             });
  101.  
  102.             alert.show();
  103.         }
  104.  
  105.    
  106.  
  107.     public class ArrayListFragment extends SherlockListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
  108.         public ActionMode mMode;
  109.         public mActionModeCallback callback;
  110.         private SimpleCursorAdapter adapter;
  111.        
  112.         @Override
  113.         public void onListItemClick(ListView l, View v, int position, long id) {
  114.             // Start the CAB using the ActionMode.Callback defined above
  115.             if (mMode != null) {
  116.                 if (position == callback.position) {
  117.                     v.setSelected(false);
  118.                     mMode.finish();
  119.                     mMode = null;
  120.                 } else {
  121.                     v.setSelected(true);
  122.                     l.setSelection(position);
  123.                     callback = new mActionModeCallback(position);
  124.                     mMode = startActionMode(callback);
  125.                 }
  126.             } else {
  127.                 v.setSelected(true);
  128.                 l.setSelection(position);
  129.                 callback = new mActionModeCallback(position);
  130.                 mMode = startActionMode(callback);
  131.             }
  132.             v.setSelected(true);
  133.         }
  134.        
  135.         final class mActionModeCallback implements ActionMode.Callback {
  136.            
  137.             public int position;
  138.  
  139.             public mActionModeCallback(int position) {
  140.                 this.position = position;
  141.             }
  142.             @Override
  143.             public boolean onCreateActionMode(ActionMode mode, Menu menu) {
  144.             mode.setTitle(notes.get(this.position));
  145.             MenuInflater inflater = getSupportMenuInflater();
  146.             inflater.inflate(R.menu.contextbar, menu);
  147.             return true;
  148.             }
  149.  
  150.             // Called each time the action mode is shown. Always called after onCreateActionMode, but
  151.             // may be called multiple times if the mode is invalidated.
  152.             @Override
  153.             public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
  154.                 return false; // Return false if nothing is done
  155.             }
  156.  
  157.             // Called when the user selects a contextual menu item
  158.             @Override
  159.             public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
  160.                 // Handle item selection
  161.                 switch (item.getItemId()) {
  162.                     case R.id.delete:
  163.                         deleteItem(this.position);
  164.                         mode.finish();
  165.                         return true;
  166.                     case R.id.edit:
  167.                         mode.finish();
  168.                         return true;
  169.                     default:
  170.                         return false;
  171.                 }
  172.             }
  173.  
  174.             // Called when the user exits the action mode
  175.             @Override
  176.             public void onDestroyActionMode(ActionMode mode) {
  177.                 mMode = null;
  178.             }
  179.         };
  180.        
  181.         public void deleteItem(int position) {
  182.             notes.remove(position);
  183.             myAdapter.notifyDataSetChanged();
  184.         }
  185.  
  186.         // Creates a new loader after the initLoader () call
  187.         @Override
  188.         public Loader<Cursor> onCreateLoader(int id, Bundle args) {
  189.             String[] projection = { NotesSQLiteHelper.COLUMN_ID, NotesSQLiteHelper.COLUMN_TITLE };
  190.             CursorLoader cursorLoader = new CursorLoader(getApplicationContext(),
  191.                     NotesContentProvider.CONTENT_URI, projection, null, null, null);
  192.             return cursorLoader;
  193.         }
  194.  
  195.         @Override
  196.         public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
  197.             adapter.swapCursor(data);
  198.         }
  199.  
  200.         @Override
  201.         public void onLoaderReset(Loader<Cursor> loader) {
  202.             // data is not available anymore, delete reference
  203.             adapter.swapCursor(null);
  204.         }
  205.  
  206.        
  207.         ArrayList<String> notes = new ArrayList<String>();
  208.         public ArrayAdapter<String> myAdapter;
  209.        
  210.  
  211.         @Override
  212.         public void onActivityCreated(Bundle savedInstanceState) {
  213.             this.getListView().setDividerHeight(2);
  214.             fillData();
  215.         }
  216.  
  217.         private void fillData() {
  218.  
  219.             // Fields from the database (projection)
  220.             // Must include the _id column for the adapter to work
  221.             String[] from = new String[] { NotesSQLiteHelper.COLUMN_TITLE };
  222.             // Fields on the UI to which we map
  223.             int[] to = new int[] { R.id.label };
  224.  
  225.             getLoaderManager().initLoader(0, null, this); //error
  226.             adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.notes_row, null, from,
  227.                     to, 0);
  228.  
  229.             setListAdapter(adapter);
  230.         }
  231.  
  232.        
  233.     }
  234.  
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement