Hello Android Example for Android 4.0 and SQLite
Software Required :
1. Jdk 1.6
2. Eclipse
3. SDK (Android 4.0 API 14)
4. emulator-5554
5.SQLite (Default With SDK)
API 14 |
AVD |
Project Structure :
Files Created :
- AndroidManifest.xml (Configuration )
- contact_list.xml (Layout File)
- Contact.java
- DatabaseHandler.java
- ListContactsActivity.java
1. AndroidManifest.xml
AndroidManifest.xml
is a powerful file in the Android
platform that allows you to describe the functionality and requirements
of your application to Android. However, working with it is not easy.
Mono for Android attempts to fix this by allowing you to add custom
attributes to your classes, which will then be used to automatically
generate the manifest for you. Our goal is that 99% of people will
never need to manually modify the manifest file.AndroidManifest.xml
is generated as part of the build process, and the XML found within Properties\AndroidManifest.xml
is merged with XML generated based on custom attributes.<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidconnect.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="List of Contacts"
android:name="com.androidconnects.android.ListContactsActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
- package represent generated package gen for application
- android:name represent main ListContactsActivity which will executed by default as it associated with Launcher.
2. contact_list.xml
Layout file to create TextView
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="20sp" >
</TextView>
3.Contact.java
Java Bean for to save and mapped Data Base fieldspublic class Contact {
int _id;
String _name;
String _phone_number;
}
4. DatabaseHandler.java
To handle Data Base operation
// Creating TABLES_CONTACTS table at SQLite on create it will execute only one time.
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_PH_NO + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
// Adding new contact in SQLiteDatabase for TABLE_CONTACT
void addContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName()); // Contact Name
values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone
// Inserting Row
db.insert(TABLE_CONTACTS, null, values);
db.close(); // Closing database connection
}
// Getting All Contacts from SQLite
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setPhoneNumber(cursor.getString(2));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
5. ListContactsActivity.java
Code Has Three Section Insert,Read and ItemClickListner1. Inserting Data
Log.d("Insert: ", "Inserting ..");
db.addContact(new Contact("Gyan", "9100000000"));
db.addContact(new Contact("Gunjan", "9199999999"));
db.addContact(new Contact("Chetan", "9522222222"));
db.addContact(new Contact("Vivek", "9533333333"));
2. // Reading all contacts
Log.d("Reading: ", "Reading all contacts..");
List<Contact> contacts = db.getAllContacts();
int conts=contacts.size();
int i=0;
String[] Contacts = new String[conts];
for (Contact cn : contacts) {
String log = "Id: "+cn.getID()+" ,Name: " + cn.getName() + " ,Phone: " + cn.getPhoneNumber();
// Writing Contacts to log
Contacts[i]=cn.getName();
i++;
Log.d("Name: ", log);
}
3. Adding Contacts to ListView
setListAdapter(new ArrayAdapter<String>(this, R.layout.contact_list,
Contacts));
ListView listView = getListView();
listView.setTextFilterEnabled(true);
4. Onclick of Item
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
O/P :
Code Download : https://github.com/androidconnects/AndroidContactList
No comments:
Post a Comment