Popular Posts

Jan 28, 2012

Using SQLite in Android

It is very easy to use sqllite in android.i have made a small project to use sqlite.. and try to explain it.. so that you can easily use sqllite in android..you can insert and fetch the data from sqlite.

- First create a project named it SQLite
- Open java file
- Type the code in onCreate Block

        SQLiteDatabase db = openOrCreateDatabase("MyDB", MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS MyTable(LastName VARCHAR(255), FIRSTName VARCHAR(255), Age INT(3))");
       
        db.execSQL("INSERT INTO MyTable VALUES ('Jubayer','Ahmed',25)");
        db.close();

- Now run the project

- To Check the sqllite file Go to DDMS
- open data -> data-> project->databases you can see the sqlite file
- To fetch the data from SQLlite use the code below


    Cursor c = db.rawQuery("SELECT * FROM MyTable", null);
        c.moveToFirst();
        Log.d("Name", c.getString(c.getColumnIndex("LastName")));




Jan 23, 2012

Shared Preferences (ANDROID)


Interface for accessing and modifying preference data
returned by getSharedPreferences(String, int).
For any particular set of preferences, there is a single instance
of this class that all clients share. Modifications to the preferences
must go through an SharedPreferences.Editor object to ensure the preference
values remain in a consistent state and control when they are committed to storage.

We are going to make a project that will store the text what you have type in the editText box.
and when you close the app and open it again it will show the text in the editText box what you
have wrote before.

- Create a project.
- Add a editText box to the xml file.
- open the java file and type the code...

package com.prefe.project;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;

public class SharedPreferencesActivity<et> extends Activity {
   

private EditText et;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        et = (EditText) findViewById(R.id.editText1);
       
        SharedPreferences settings = getSharedPreferences("MYPREFS",0);
        et.setText(settings.getString("tvalue", ""));
       
    }
   
@Override
protected void onStop() {
super.onStop();
SharedPreferences settings = getSharedPreferences("MYPREFS", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("tvalue", et.getText().toString());
editor.commit();
}
}

- now run the project, type some thing in the text box.close the app and open it again.
- you can see the text what you have written.... !!! :)





To know visit here http://developer.android.com/reference/android/content/SharedPreferences.html
                             http://developer.android.com/guide/topics/data/data-storage.html