Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //I use a ListActivityy for easy testing
- public class TestContextMenu extends ListActivity {
- private static final String DATABASE_NAME = "reg_name.db";
- private static final int DATABASE_VERSION = 1;
- private SQLiteDatabase mDb;// our database on which we'll make the querys
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- DatabaseHelper dbh = new DatabaseHelper(this, DATABASE_NAME, null,
- DATABASE_VERSION);
- mDb = dbh.getWritableDatabase();
- // I only get the id, type, date and status columns
- String[] columns = { TabRegistry._ID, TabRegistry.TYPE,
- TabRegistry.DATE, TabRegistry.STATUS };
- Cursor cursor = mDb.query(TabRegistry.TABLE_NAME, columns, null, null,
- null, null, null);
- setListAdapter(new RegistryAdapter(this, cursor));
- registerForContextMenu(getListView());
- }
- @Override
- public boolean onContextItemSelected(MenuItem item) {
- AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
- .getMenuInfo();
- long rowId = info.id;
- // see which context option was selected
- switch (item.getItemId()) {
- case 1000:
- // I assumed you only want the NUMBER column so I query for just this
- // column
- String[] columnsForResults = { TabRegistry.NUMBER };
- // We have an id so use it so we only get only that row
- String selection = TabRegistry._ID + "= ?";
- // the ? will be replaced with the actual value in the query
- String[] selectionArgs = { Long.toString(rowId) };
- Cursor results = mDb.query(TabRegistry.TABLE_NAME,
- columnsForResults, selection, selectionArgs, null, null,
- null);
- // Build the results of the query
- results.moveToFirst();
- String toShow = "For the row with the id: "
- + rowId
- + " the NUMBER column is: "
- + results.getString(results
- .getColumnIndex(TabRegistry.NUMBER)) + "!";
- Toast.makeText(this, toShow, Toast.LENGTH_LONG).show();
- break;
- case 2000:
- Toast.makeText(this,
- "You pressed the ContextMenu that does nothing!",
- Toast.LENGTH_SHORT).show();
- break;
- }
- return super.onContextItemSelected(item);
- }
- @Override
- public void onCreateContextMenu(ContextMenu menu, View v,
- ContextMenuInfo menuInfo) {
- menu.add(Menu.NONE, 1000, Menu.NONE, "See details"); //see the details
- menu.add(Menu.NONE, 2000, Menu.NONE, "See nothing"); // does nothing
- }
- private class DatabaseHelper extends SQLiteOpenHelper {
- public DatabaseHelper(Context context, String name,
- CursorFactory factory, int version) {
- super(context, name, factory, version);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- // you don't have to use the format method, just append directly the
- // table/column names to the sql string
- String sql = "CREATE TABLE " + TabRegistry.TABLE_NAME + " ("
- + TabRegistry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
- + TabRegistry.TYPE + " TEXT, " + TabRegistry.DATE
- + " INTEGER, " + TabRegistry.STATUS + " TEXT, "
- + TabRegistry.NUMBER + " TEXT, " + TabRegistry.MESSAGE
- + " TEXT, " + TabRegistry.OTHER + " TEXT);";
- db.execSQL(sql);
- // insert some values in the database(I just started at 1 so the id
- // and Number column match , to easy test the code)
- for (int i = 1; i < 30; i++) {
- ContentValues cv = new ContentValues();
- cv.put(TabRegistry.DATE, 1000 + i);
- cv.put(TabRegistry.MESSAGE, "Message no." + i);
- cv.put(TabRegistry.NUMBER, "Number no." + i); // this is the number!
- cv.put(TabRegistry.OTHER, "Other no." + i);
- cv.put(TabRegistry.STATUS, "Status no." + i);
- cv.put(TabRegistry.TYPE, "Type no." + i);
- db.insert(TabRegistry.TABLE_NAME, null, cv);
- }
- }
- @Override
- public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
- // just for the interface in this example
- }
- }
- /**
- * I don't know what is your TabRegistry class so I use a simple class that
- * will hold those fields for the database (It also implements BaseColumns
- * so you have the _ID column)
- */
- class TabRegistry implements BaseColumns {
- public static final String TABLE_NAME = " table_name";
- public static final String TYPE = "col_type";
- public static final String DATE = "col_date";
- public static final String STATUS = "col_status";
- public static final String NUMBER = "col_number";
- public static final String MESSAGE = "col_message";
- public static final String OTHER = "col_other";
- }
- class RegistryAdapter extends CursorAdapter {
- public RegistryAdapter(Context context, Cursor c) {
- super(context, c);
- }
- @Override
- public void bindView(View view, Context context, Cursor cursor) {
- ((TextView) view.findViewById(R.id.tipo)).setText(cursor
- .getString(cursor.getColumnIndex(TabRegistry.TYPE)));
- ((TextView) view.findViewById(R.id.data)).setText(cursor
- .getString(cursor.getColumnIndex(TabRegistry.DATE)));
- ((TextView) view.findViewById(R.id.stato)).setText(cursor
- .getString(cursor.getColumnIndex(TabRegistry.STATUS)));
- }
- @Override
- public View newView(Context context, Cursor cursor, ViewGroup parent) {
- return LayoutInflater.from(context).inflate(R.layout.registryvista,
- null);
- }
- }
- }
- //and the xml layout for the custom adapter row
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/tipo"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <TextView
- android:id="@+id/data"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <TextView
- android:id="@+id/stato"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement