Create Project
Create new project in Android Studio with steps as below:
Step 1: Input Project Name and Select Project Location
Step 2: Select SDK for Android App
Step 3: Select Default Activity for App
Step 4: Finish create project
Create Entities
Create new package named entities. In this package, create new java class named Contact.java as below:
package android.demo.entities;
import java.io.Serializable;
public class Contact implements Serializable {
private int id;
private String name;
private String phone;
private String address;
private String email;
private String description;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
}
Create DatabaseHelper
Create new package named database. In this package, create new java class named DatabaseHelper.java as below:
package android.demo.database;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.demo.entities.Contact;
import java.util.ArrayList;
import java.util.List;
public class DatabaseHelper extends SQLiteOpenHelper {
private static String dbName = "ContactDB";
private static int dbVersion = 1;
private static String contactTable = "contact";
private static String idColumn = "id";
private static String nameColumn = "name";
private static String phoneColumn = "phone";
private static String addressColumn = "address";
private static String emailColumn = "email";
private static String descriptionColumn = "description";
public DatabaseHelper(Context context) {
super(context, dbName, null, dbVersion);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table " + contactTable + "(" +
idColumn + " integer primary key autoincrement, " +
nameColumn + " tex, " +
phoneColumn + " text, " +
addressColumn + " text, " +
emailColumn + " text, " +
descriptionColumn + " text " +
")"
);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("drop table if exists " + contactTable);
onCreate(sqLiteDatabase);
}
public List<Contact> findAll() {
List<Contact> contacts = null;
try {
SQLiteDatabase sqLiteDatabase = getReadableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery("select * from " + contactTable, null);
if (cursor.moveToFirst()) {
contacts = new ArrayList<Contact>();
do {
Contact contact = new Contact();
contact.setId(cursor.getInt(0));
contact.setName(cursor.getString(1));
contact.setPhone(cursor.getString(2));
contact.setAddress(cursor.getString(3));
contact.setEmail(cursor.getString(4));
contact.setDescription(cursor.getString(5));
contacts.add(contact);
} while (cursor.moveToNext());
}
} catch (Exception e) {
contacts = null;
}
return contacts;
}
}
Create Custom ListView Layout
Select res\layout folder. In this folder, create new layout named contact_list_layout.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textViewName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textViewPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textViewEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textViewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
Create Custom Adapter
Create new package named adapters. In this package, create new java class named ContactListAdapter.java as below:
package android.demo.adapters;
import android.content.Context;
import android.demo.entities.Contact;
import android.demo.learnandroidwithrealapps.R;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;
public class ContactListAdapter extends ArrayAdapter<Contact> {
private Context context;
private List<Contact> contacts;
public ContactListAdapter(Context context, List<Contact> contacts) {
super(context, R.layout.contact_list_layout, contacts);
this.context = context;
this.contacts = contacts;
}
@NonNull
@Override
public View getView(int position, @Nullable View view, @NonNull ViewGroup parent) {
ViewHolder viewHolder;
if (view == null) {
viewHolder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.contact_list_layout, null);
viewHolder.textViewId = view.findViewById(R.id.textViewId);
viewHolder.textViewName = view.findViewById(R.id.textViewName);
viewHolder.textViewPhone = view.findViewById(R.id.textViewPhone);
viewHolder.textViewEmail = view.findViewById(R.id.textViewEmail);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
Contact contact = contacts.get(position);
viewHolder.textViewId.setText(String.valueOf(contact.getId()));
viewHolder.textViewName.setText(contact.getName());
viewHolder.textViewPhone.setText(contact.getPhone());
viewHolder.textViewEmail.setText(contact.getEmail());
return view;
}
private static class ViewHolder {
public static TextView textViewId;
public static TextView textViewName;
public static TextView textViewPhone;
public static TextView textViewEmail;
}
}
Main Activity Layout
Open res\layout\activity_main.xml file and create layout as below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ListView
android:id="@+id/listViewContact"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Main Activity Class
Add code to MainActivity.java in android.demo.learnandroidwithrealapps package as below:
package android.demo.learnandroidwithrealapps;
import android.content.Intent;
import android.demo.adapters.ContactListAdapter;
import android.demo.database.DatabaseHelper;
import android.demo.entities.Contact;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView listViewContact;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
loadData();
}
private void initView() {
listViewContact = findViewById(R.id.listViewContact);
}
private void loadData() {
DatabaseHelper databaseHelper = new DatabaseHelper(getApplicationContext());
List<Contact> contacts = databaseHelper.findAll();
if (contacts != null) {
listViewContact.setAdapter(new ContactListAdapter(getApplicationContext(), contacts));
}
}
}
Structure of Project
Run App
Load Main Activity