RSS

Tag Archives: Android

Database Helper Class for Android

package com.my.first;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Shader.TileMode;
import android.util.Log;

public class DBAdapter {
public static final String KEY_ROWID = "songId";
public static final String KEY_TITLE = "title";
public static final String KEY_TYPE = "type";
public static final String KEY_SIZE = "size";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "music";
private static final String DATABASE_TABLE = "songs";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table songs (songId integer primary key autoincrement, "
+ "title varchar not null, type varchar not null, "
+ "size float not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}

private  static class DatabaseHelper extends SQLiteOpenHelper
{

public DatabaseHelper(Context context) {
super(context,DATABASE_NAME,null,DATABASE_VERSION);

}

@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL(DATABASE_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS songs");
onCreate(db);
}



}

//---opens the database---
public DBAdapter open()
{
try
{
db = DBHelper.getWritableDatabase();

}
catch(SQLException ex)
{

}
return this;
}

//---closes the database---
public void close()
{
DBHelper.close();
}

//---insert a song into the database---
public long insertTitle(String title, String type, String size)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_TYPE, type);
initialValues.put(KEY_SIZE, size);
return db.insert(DATABASE_TABLE, null, initialValues);
}

//---deletes a particular title---
public boolean deleteTitle(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID +
"=" + rowId, null) > 0;
}

//---retrieves all the titles---
public Cursor getAllSongTitles()
{
return db.query(DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_TITLE,
KEY_TYPE,
KEY_SIZE},
null,
null,
null,
null,
null);
}

//---retrieves a particular title---
public Cursor getSongTitle(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_TITLE,
KEY_TYPE,
KEY_SIZE
},
KEY_ROWID + "=" + rowId,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}


//---updates a title---
public boolean updateSongTitle(long rowId, String title,
String type, String size)
{
ContentValues args = new ContentValues();
args.put(KEY_TITLE, title);
args.put(KEY_TYPE, type);
args.put(KEY_SIZE, size);
return db.update(DATABASE_TABLE, args,
KEY_ROWID + "=" + rowId, null) > 0;
}

}


 
Leave a comment

Posted by on February 28, 2012 in Android

 

Tags: ,

Create SQLite Database in Android

package com.my.first;

import android.R.string;
 import android.app.Activity;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.os.Bundle;
 import android.util.Log;

public class DatabaseActivity extends Activity {

@Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

SQLiteDatabase db = openOrCreateDatabase("MyDB", MODE_PRIVATE, null);
 db.execSQL("CREATE TABLE IF NOT EXISTS MyTable (FirstName VARCHAR,LastName VARCHAR,Age INT(3))");
 db.execSQL("INSERT INTO MyTable VALUES('Josh','Fedric',23)");

Cursor c = db.rawQuery("SELECT * FROM MyTable", null);
 c.moveToFirst();
 String msg = c.getString(c.getColumnIndex("FirstName"));
 Log.d("Fname",msg);

}
 }

 
Leave a comment

Posted by on February 28, 2012 in Android

 

Tags: ,

How to add a context menu in Android

  1. Create an Android XML file of Menu type
  2. Add menu options as you preferred. Each menu option should be indicated as <item> XML element.
  3. This is my context_menu.xml file.
    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/item1"   android:title="option1"></item>
    <item android:id="@+id/item2"   android:title="option2"></item>
    </menu>
    
  4. Override onCreateContextMenu and onContextItemSelected in your Activity.java file
    Add a button in main.xml to respond our context menu.

    package com.my.option;
    
    import java.util.zip.Inflater;
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.ContextMenu;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ContextMenu.ContextMenuInfo;
    import android.widget.Button;
    
    public class ContextMenuActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    Button b = (Button) findViewById(R.id.button1);
    registerForContextMenu(b);
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
    ContextMenuInfo menuInfo) {
    
    MenuInflater inflator = getMenuInflater();
    inflator.inflate(R.menu.context_menu, menu);
    
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {
    if(item.getItemId() == R.id.item1)
    Log.d("Edit", "Selected : Edit Option");
    if(item.getItemId() == R.id.item2)
    Log.d("Delete", "Selected : Delete Option");
    return super.onContextItemSelected(item);
    }
    }
    
 
Leave a comment

Posted by on January 23, 2012 in Android

 

Tags:

Simple Options Menu in Android

  1. Create an Android XML file of Menu type
  2. Add menu options as you preferred. Each menu option should be indicated as <item> XML element.
  3. This is my options.xml file.
    <!--?xml version=<em--><?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <item android:id="@+id/item1" android:title="Edit" android:icon="@android:drawable/ic_menu_edit"></item>
    <item android:id="@+id/item2" android:title="Delete" android:icon="@android:drawable/ic_menu_delete"></item>
    <item android:id="@+id/item3" android:title="Help" android:icon="@android:drawable/ic_menu_help"></item>
    <item android:id="@+id/item4" android:title="More" android:icon="@android:drawable/ic_menu_more"></item>
    
    
    </menu>
    
  4. My options menu is not just text-based. Each menu item is displayed with a relevant icon. Here I used the icons found in the Android.jar file of my project. You can find such icons using this path
    <app root > –> Android <Your Android platform>  –> android.jar –> res –> drawable-hdpi(icons for menu prefixed with ic_menu)
  5. Override onCreateOptionsMenu and onOptionsItemSelected in your Activity.java file
    package com.my.option;
    
    import java.io.Console;
    import java.util.zip.Inflater;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    
    public class OptionMenuActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflator = getMenuInflater();
    inflator.inflate(R.menu.options, menu);
    return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId() == R.id.item1)
    Log.d("Edit", "Selected : Edit Option");
    if(item.getItemId() == R.id.item2)
    Log.d("Delete", "Selected : Delete Option");
    return super.onOptionsItemSelected(item);
    }
    }
    
 
Leave a comment

Posted by on January 23, 2012 in Android

 

Tags: