Advertisement
Guest User

test code

a guest
Apr 21st, 2012
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.92 KB | None | 0 0
  1. //I use a ListActivityy for easy testing
  2. public class TestContextMenu extends ListActivity {
  3.  
  4.     private static final String DATABASE_NAME = "reg_name.db";
  5.     private static final int DATABASE_VERSION = 1;
  6.     private SQLiteDatabase mDb;// our database on which we'll make the querys
  7.  
  8.     @Override
  9.     protected void onCreate(Bundle savedInstanceState) {
  10.         super.onCreate(savedInstanceState);
  11.         DatabaseHelper dbh = new DatabaseHelper(this, DATABASE_NAME, null,
  12.                 DATABASE_VERSION);
  13.         mDb = dbh.getWritableDatabase();
  14.         // I only get the id, type, date and status columns
  15.         String[] columns = { TabRegistry._ID, TabRegistry.TYPE,
  16.                 TabRegistry.DATE, TabRegistry.STATUS };
  17.         Cursor cursor = mDb.query(TabRegistry.TABLE_NAME, columns, null, null,
  18.                 null, null, null);
  19.         setListAdapter(new RegistryAdapter(this, cursor));
  20.         registerForContextMenu(getListView());
  21.     }
  22.  
  23.     @Override
  24.     public boolean onContextItemSelected(MenuItem item) {
  25.         AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
  26.                 .getMenuInfo();
  27.         long rowId = info.id;
  28.         // see which context option was selected
  29.         switch (item.getItemId()) {
  30.         case 1000:
  31.             // I assumed you only want the NUMBER column so I query for just this
  32.             // column
  33.             String[] columnsForResults = { TabRegistry.NUMBER };
  34.             // We have an id so use it so we only get only that row
  35.             String selection = TabRegistry._ID + "= ?";
  36.             // the ? will be replaced with the actual value in the query
  37.             String[] selectionArgs = { Long.toString(rowId) };
  38.             Cursor results = mDb.query(TabRegistry.TABLE_NAME,
  39.                     columnsForResults, selection, selectionArgs, null, null,
  40.                     null);
  41.             // Build the results of the query
  42.             results.moveToFirst();
  43.             String toShow = "For the row with the id: "
  44.                     + rowId
  45.                     + " the NUMBER column is: "
  46.                     + results.getString(results
  47.                             .getColumnIndex(TabRegistry.NUMBER)) + "!";
  48.             Toast.makeText(this, toShow, Toast.LENGTH_LONG).show();
  49.             break;
  50.         case 2000:
  51.             Toast.makeText(this,
  52.                     "You pressed the ContextMenu that does nothing!",
  53.                     Toast.LENGTH_SHORT).show();
  54.             break;
  55.         }
  56.         return super.onContextItemSelected(item);
  57.     }
  58.  
  59.     @Override
  60.     public void onCreateContextMenu(ContextMenu menu, View v,
  61.             ContextMenuInfo menuInfo) {
  62.         menu.add(Menu.NONE, 1000, Menu.NONE, "See details"); //see the details
  63.         menu.add(Menu.NONE, 2000, Menu.NONE, "See nothing"); // does nothing
  64.     }
  65.  
  66.     private class DatabaseHelper extends SQLiteOpenHelper {
  67.  
  68.         public DatabaseHelper(Context context, String name,
  69.                 CursorFactory factory, int version) {
  70.             super(context, name, factory, version);
  71.         }
  72.  
  73.         @Override
  74.         public void onCreate(SQLiteDatabase db) {
  75.             // you don't have to use the format method, just append directly the
  76.             // table/column names to the sql string
  77.             String sql = "CREATE TABLE " + TabRegistry.TABLE_NAME + " ("
  78.                     + TabRegistry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
  79.                     + TabRegistry.TYPE + " TEXT, " + TabRegistry.DATE
  80.                     + " INTEGER, " + TabRegistry.STATUS + " TEXT, "
  81.                     + TabRegistry.NUMBER + "  TEXT, " + TabRegistry.MESSAGE
  82.                     + " TEXT, " + TabRegistry.OTHER + " TEXT);";
  83.             db.execSQL(sql);
  84.             // insert some values in the database(I just started at 1 so the id
  85.             // and Number column match , to easy test the code)
  86.             for (int i = 1; i < 30; i++) {
  87.                 ContentValues cv = new ContentValues();
  88.                 cv.put(TabRegistry.DATE, 1000 + i);
  89.                 cv.put(TabRegistry.MESSAGE, "Message no." + i);
  90.                 cv.put(TabRegistry.NUMBER, "Number no." + i); // this is the number!
  91.                 cv.put(TabRegistry.OTHER, "Other no." + i);
  92.                 cv.put(TabRegistry.STATUS, "Status no." + i);
  93.                 cv.put(TabRegistry.TYPE, "Type no." + i);
  94.                 db.insert(TabRegistry.TABLE_NAME, null, cv);
  95.             }
  96.         }
  97.  
  98.         @Override
  99.         public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
  100.             // just for the interface in this example
  101.         }
  102.  
  103.     }
  104.  
  105.     /**
  106.      * I don't know what is your TabRegistry class so I use a simple class that
  107.      * will hold those fields for the database (It also implements BaseColumns
  108.      * so you have the _ID column)
  109.      */
  110.     class TabRegistry implements BaseColumns {
  111.         public static final String TABLE_NAME = " table_name";
  112.         public static final String TYPE = "col_type";
  113.         public static final String DATE = "col_date";
  114.         public static final String STATUS = "col_status";
  115.         public static final String NUMBER = "col_number";
  116.         public static final String MESSAGE = "col_message";
  117.         public static final String OTHER = "col_other";
  118.     }
  119.  
  120.     class RegistryAdapter extends CursorAdapter {
  121.  
  122.         public RegistryAdapter(Context context, Cursor c) {
  123.             super(context, c);
  124.         }
  125.  
  126.         @Override
  127.         public void bindView(View view, Context context, Cursor cursor) {
  128.             ((TextView) view.findViewById(R.id.tipo)).setText(cursor
  129.                     .getString(cursor.getColumnIndex(TabRegistry.TYPE)));
  130.             ((TextView) view.findViewById(R.id.data)).setText(cursor
  131.                     .getString(cursor.getColumnIndex(TabRegistry.DATE)));
  132.             ((TextView) view.findViewById(R.id.stato)).setText(cursor
  133.                     .getString(cursor.getColumnIndex(TabRegistry.STATUS)));
  134.         }
  135.  
  136.         @Override
  137.         public View newView(Context context, Cursor cursor, ViewGroup parent) {
  138.             return LayoutInflater.from(context).inflate(R.layout.registryvista,
  139.                     null);
  140.         }
  141.  
  142.     }
  143.  
  144. }
  145.  
  146. //and the xml layout for the custom adapter row
  147. <?xml version="1.0" encoding="utf-8"?>
  148. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  149.     android:layout_width="match_parent"
  150.     android:layout_height="match_parent"
  151.     android:orientation="vertical" >
  152.  
  153.     <TextView
  154.         android:id="@+id/tipo"
  155.         android:layout_width="wrap_content"
  156.         android:layout_height="wrap_content" />
  157.  
  158.     <TextView
  159.         android:id="@+id/data"
  160.         android:layout_width="wrap_content"
  161.         android:layout_height="wrap_content" />
  162.  
  163.     <TextView
  164.         android:id="@+id/stato"
  165.         android:layout_width="wrap_content"
  166.         android:layout_height="wrap_content" />
  167.  
  168. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement