Thursday, July 7, 2011

UrlEncode

public class Extras {

public static String URLencode(String s){
if (s!=null) {
StringBuffer tmp = new StringBuffer();
int i=0;
try {
//s=java.net.URLEncoder.encode(s,"UTF-8");

while (true) {
int b = (int)s.charAt(i++);
if ((b>=0x30 && b<=0x39) || (b>=0x41 && b<=0x5A) || (b>=0x61 && b<=0x7A)) {
tmp.append((char)b);
}
else {
tmp.append("%");
if (b <= 0xf) tmp.append("0");
tmp.append(Integer.toHexString(b));
}
}

}

catch (Exception e) {
}

return tmp.toString();
}

return null;
}


public static String removecommas(String str)
{
try{
if(str.contains(","))
{
str=str.replace(",","");

return str;
}

else
return str;

}catch(Exception e){
return null;
}

}


public static String removeminus(String str)
{
try{
if(str.contains("-"))
{
str=str.replace("-","");
return str;
}
else
return str;


}catch(Exception e){
return null;
}

}
}

Monday, July 4, 2011

Andriod get image from Url

ImageView iv = new ImageView(context);

try{
String url1 = "http:///test/abc.jpg";
URL ulrn = new URL(url1);
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
InputStream is = con.getInputStream();
Bitmap bmp = BitmapFactory.decodeStream(is);
if (null != bmp)
iv.setImageBitmap(bmp);
else
System.out.println("The Bitmap is NULL");

}catch(Exception e){}
}

Friday, July 1, 2011

Db to get ID

db=new DataHelper(EatOut_details.this);
db.open();
Cursor res=db.getFeed(Story_id);

if(res==null || res.getCount()<=0){

btn_favourite.setBackgroundResource(R.drawable.added);
try{
db.insert(Story_id,1,2,3,4,5,sub_6);
}catch (Exception e) {
Log.e("1", "Error while Querying - "+e);
}

Toast.makeText(this, " dded", Toast.LENGTH_SHORT).show();

res.close();

}else{

btn_favourite.setBackgroundResource(R.drawable.icon);

try{
db.deleteFeed(Story_id);
}catch (Exception e) {
Log.e("ram", "Error while Querying - "+e);

}
Toast.makeText(this, " Removed", Toast.LENGTH_SHORT).show();
res.close();
}
db.close();

remove db

DataHelper db=new DataHelper(this);

db.open();

try{
db.deleteFeed(Story_id[select_pos]);
}catch (Exception e) {
Log.e("BookUrTable", "Error while Querying - "+e);

}

db.close();

Get Database Data

private void UpdateDatabaseEatoutDetails() {

try{

DataHelper db=new DataHelper(this);

db.open();

Cursor sqlResult=null;

sqlResult=db.multiValueQuery("select Story_id,1,2,3,4," +
"5,6,sub_location from ram");

db.close();

List 1= new ArrayList();
List 2= new ArrayList();
List 3=new ArrayList();

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


if(sqlResult!=null){

size=offer1.size();

1=new String[size];
Story_id=new int[size];
2=new String[size];


for(int i=0;i {

1[i]=rating1.get(i);
Story_id[i]=Story_id1.get(i);

}
}

mHandlerOrderin.post(mUpdateResultsorderin);

}catch(Exception e){
System.out.println("=====error update db===="+e);
}

}



final Handler mHandlerOrderin = new Handler();

final Runnable mUpdateResultsorderin = new Runnable() {
public void run() {
updateUIwithDataOrderin();
}
};

DataBase android

package com.ram.database;


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 DataCoupon{

private static final String DATABASE_CREATE =
"create table ram1(Story_id integer primary key, "
+ "offer textt,name text not null,date text);";

private final Context context;

private DatabaseHelper DBHelper;
private SQLiteDatabase db;


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

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

public void deleteAll() {
db.delete(DATABASE_TABLE, null, null);
}
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 DataCoupon 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 date
* @param avgMealForTwo */

public long insert(int Story_id, String offer, String name, String date){

ContentValues initialValues = new ContentValues();
initialValues.put("Story_id", Story_id);
initialValues.put("offer", offer);
initialValues.put("name", name);
initialValues.put("date", date);
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",
"name",
"date"
},
"Story_id" + "=" + Story_id,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}


}

Friends Blog all sample code

http://smartandroidians.blogspot.com/2010/05/reading-and-writing-to-file-in-android.html