Wednesday, May 18, 2011

Monday, May 16, 2011

DataBase

DataHelper:

package com.jeeva.favourite;


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.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;



public class DataHelper {

//DataBase table Create Statement
private static final String DATABASE_CREATE =
"create table ram (Story_id integer primary key, "
+ "name text not null);";

private final Context context;

private DatabaseHelper DBHelper;
private SQLiteDatabase db;


private static final String DATABASE_NAME = "ramdb";
private static final String DATABASE_TABLE = "ram";
private static final int DATABASE_VERSION = 1;
private static final String TAG = "DBAdapter";

public DataHelper(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}


private static class DatabaseHelper extends SQLiteOpenHelper
{
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 "+DATABASE_TABLE);
onCreate(db);
}

}

/**---opens the database---*/
public DataHelper open() throws SQLException
{

db = DBHelper.getWritableDatabase();
return this;
}

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

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

/**---insert a title into the database---
* @param avgMealForTwo */

public long insert(int Story_id, String offer){

ContentValues initialValues = new ContentValues();
initialValues.put("Story_id", Story_id);
initialValues.put("offer", offer);

return db.insert(DATABASE_TABLE, null, initialValues);

}

/**---Get multiValue Result of user Query--g*/
public Cursor multiValueQuery(String userSQL){
Cursor cursorResult=null;
try{
cursorResult=db.rawQuery(userSQL, null);


}catch(Exception e){
System.out.println("Error in select all--"+e);
return null;
}
if (cursorResult != null) {
cursorResult.moveToFirst();
}
return cursorResult;
}



/**-- Give a single value as result for our give query in compileStatement--g*/
public SQLiteStatement singleValueQuery(String userSQL){
SQLiteStatement result=null;
try{

result=db.compileStatement(userSQL);

return result;

}catch(Exception e){
System.out.println("User Query Error"+e);
return null;
}
}

//---retrieves a particular title---
public Cursor getFeed(Integer Story_id) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {
"Story_id",
"offer",

},
"Story_id" + "=" + Story_id,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}


}




OPenDb:

DataHelper db;

private void UpdateDatabaseDetails() {
try{
db=new DataHelper(this);
db.open();
Cursor sqlResult=null;

sqlResult=db.multiValueQuery("select Story_id,offer from BookUrTable");
db.close();

}
}
List name1 = new ArrayList();

if (sqlResult.moveToFirst()) {
do {
Story_id1.add(sqlResult.getInt(0));
} while (sqlResult.moveToNext());
sqlResult.close();

if(sqlResult!=null){

size=offer1.size();

rating=new String[size];

Connect Db:

DataHelper db;
long res=-1;
db=new DataHelper(getParent());
db.open();
try{
db.insert(Story_id,offer);
res=db.singleValueQuery("select count(*) from BookUrTable where Story_id="+Story_id).simpleQueryForLong();
}catch (Exception e) {
Log.e("Dosa", "Error while Querying - "+e);
res=-1;
}
db.close();
Toast.makeText(getParent(), "Added", Toast.LENGTH_SHORT).show();
}

Sunday, May 8, 2011

ActivityGroup

package com.example.myandroid;

import java.util.ArrayList;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

public class FirstGroup extends ActivityGroup {

// Keep this in a static variable to make it accessible for all the nested activities, lets them manipulate the view
public static FirstGroup group;

// Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory.
private ArrayList history;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.history = new ArrayList();
group = this;

// Start the root activity withing the group and get its view
View view = getLocalActivityManager().startActivity("CitiesActivity", new
Intent(this,CitiesActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();

// Replace the view of this ActivityGroup
replaceView(view);

}

public void replaceView(View v) {
// Adds the old one to history
history.add(v);
// Changes this Groups View to the new View.
setContentView(v);
}

public void back() {
if(history.size() > 0) {
history.remove(history.size()-1);
setContentView(history.get(history.size()-1));
}else {
finish();
}
}

// public void onBackPressed() {
// FirstGroup.group.back();
// return;
// }
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode==KeyEvent.KEYCODE_BACK){
back();
return true;
}else

return super.onKeyDown(keyCode, event);
}
}






Add Code:
View view = FirstGroup.group.getLocalActivityManager()
.startActivity("show_city", i
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();

// Again, replace the view
FirstGroup.group.replaceView(view);