Sunday, July 1, 2012

How Do i Work With Android DataBase - Select / Insert / Update / Delete

BeanMatrixDbActivity.java

package beanmatrix.db;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class BeanMatrixDbActivity extends Activity {
       private SQLiteAdapter SQLiteAdapter1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ListView listContent = (ListView)findViewById(R.id.contentlist);         
        SQLiteAdapter1 = new SQLiteAdapter(this);     
        SQLiteAdapter1.openToWrite();     
        SQLiteAdapter1.deleteAll();     
        SQLiteAdapter1.insert("Apple");     
        SQLiteAdapter1.insert("Banana");     
        SQLiteAdapter1.insert("Watermalon");
        SQLiteAdapter1.insert("Mango");
        SQLiteAdapter1.close();           
        SQLiteAdapter1 = new SQLiteAdapter(this);     
        SQLiteAdapter1.openToRead();     
        Cursor cursor = SQLiteAdapter1.queueAll();     
        startManagingCursor(cursor);     
        String[] from = new String[]{SQLiteAdapter.FRUIT_NAME};     
        int[] to = new int[]{R.id.text};     
        SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.main, cursor, from, to);     
        listContent.setAdapter(cursorAdapter);         
        SQLiteAdapter1.close();
    }
}
SQLiteAdapter.java

package beanmatrix.db;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class SQLiteAdapter {
       public static final String DATABASE_NAME = "DATABASE1";
       public static final String TABLE = "FRUITS";
       public static final int MYDATABASE_VERSION = 1;
       public static final String ID = "_id";
       public static final String FRUIT_NAME = "Content";
       private static final String SCRIPT_CREATE_DATABASE ="create table " + TABLE + " ("+ ID + " integer primary key autoincrement, "+ FRUIT_NAME + " text not null);";
       private SQLiteHelper sqLiteHelper;
       private SQLiteDatabase sqLiteDatabase;
       private Context context;
       public SQLiteAdapter(Context c){context = c;}
       public SQLiteAdapter openToRead() throws android.database.SQLException
       {
              sqLiteHelper = new SQLiteHelper(context, DATABASE_NAME, null, MYDATABASE_VERSION);
              sqLiteDatabase = sqLiteHelper.getReadableDatabase();
              return this;
              }
       public SQLiteAdapter openToWrite() throws android.database.SQLException
       {
              sqLiteHelper = new SQLiteHelper(context, DATABASE_NAME, null, MYDATABASE_VERSION);
              sqLiteDatabase = sqLiteHelper.getWritableDatabase();
              return this;
              }
       public void close()
       {
              sqLiteHelper.close();
              }
       public long insert(String content)
       {
              ContentValues contentValues = new ContentValues();
              contentValues.put(FRUIT_NAME, content);
              return sqLiteDatabase.insert(TABLE, null, contentValues);
              }
       public int deleteAll()
       {
              return sqLiteDatabase.delete(TABLE, null, null);
              }
       public Cursor queueAll()
       {
              String[] columns = new String[]{ID, FRUIT_NAME};
              Cursor cursor = sqLiteDatabase.query(TABLE, columns,  null, null, null, null, null);
              return cursor;
              }
       public class SQLiteHelper extends SQLiteOpenHelper
       {
              public SQLiteHelper(Context context, String name,  CursorFactory factory, int version)
              {
                     super(context, name, factory, version);
                     }
              @Override
              public void onCreate(SQLiteDatabase db)
              {
                     db.execSQL(SCRIPT_CREATE_DATABASE);
              }
     }
 }
             
main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

   <ListView android:id="@+id/contentlist"
                           android:layout_width="fill_parent"
                           android:layout_height="fill_parent"/>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/text"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="10dip"/>
</LinearLayout>


Working with web service - call C#.net webservice with Andrioid application

Create Loading Screen

LoadingScreen.java

package beanmatrix.loadingscreen;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;

public class LoadingScreen extends Activity {
    /** Called when the activity is first created. */
       private ProgressDialog pd;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new LoadView().execute();   
      }

    private class LoadView extends AsyncTask<Void, Integer, Void>
    {
              @Override
              protected void onPreExecute()
              {
                     pd = new ProgressDialog(LoadingScreen.this);
                     pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                     pd.setTitle("Loading...");
                     pd.setMessage("please wait,application will be shortly loaded ...");
                     pd.setMax(500);
                     pd.setProgress(0);
                     pd.show();
              }
              @Override
              protected Void doInBackground(Void... params)
              {      try
                     {
                           synchronized (this)
                           {      int count = 0;
                                  while(count <= 5)
                                  {      this.wait(500);
                                         count++;
                                         publishProgress(count*50);
                                  }
                           }
                     }
                     catch (InterruptedException e)
                     {
                           e.printStackTrace();
                     }
                     return null;
              }
              @Override
              protected void onProgressUpdate(Integer... values)
              {
                     pd.setProgress(values[0]);
              }
              @Override
              protected void onPostExecute(Void result)
              {
                     pd.dismiss();
                     setContentView(R.layout.main);
              }
    }
}


main.xml

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
</LinearLayout>



How Do I Use Image With Android Application

BeanMatrixImageDemoActivity.java

package beanmatrix.image.demo;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class BeanMatrixImageDemoActivity extends Activity {
    /** Called when the activity is first created. */
       @Override
       public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
       }
       public void onClickImg(View view)
       {            
              Toast.makeText(this, "You clicked me!", Toast.LENGTH_SHORT).show();
       }
}


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/myimg"
        android:clickable="true"
        android:onClick="onClickImg"
        />
</LinearLayout>