Site icon ParallelCodes

Android ListView using Database

Android ListView using Database – Android Listview can be filled using SQLite database or using web services. This post is about filling data using SQLite database. This post is same as my previous post about Android ListView using SimpleCursorAdapter.

Edit the activity_main.xml file as below.

activity_main.xml:

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

<ListView
 android:id="@+id/lstView"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:clickable="true"
 android:divider="#000"
 android:dividerHeight="1dp"
 android:listSelector="#006cd9" >
 </ListView>

</LinearLayout>

Make a new layout file in res > layout folder with name list_template.xml and edit it as below.

res > layout > list_template.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/txtListElement"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:textColor="#fff" >

</TextView>

Create a new class in your project with name DBController.java and edit it as below :

src > DBController.java :

package parallelcodes.simplewidget;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBController extends SQLiteOpenHelper {
 private static final String LOGCAT = null;

public DBController(Context applicationcontext) {
 super(applicationcontext, "WeekDaysData.db", null, 1);
 Log.d(LOGCAT, "Created");
 }

@Override
 public void onCreate(SQLiteDatabase database) {
 String query;
 query = "CREATE TABLE IF NOT EXISTS tblWeekDays ( _id INTEGER PRIMARY KEY, DayOfWeek TEXT)";
 database.execSQL(query);

query = "insert into tblWeekDays (DayOfWeek) values ('Sunday')";
 database.execSQL(query);
 
 query = "insert into tblWeekDays (DayOfWeek) values ('Monday')";
 database.execSQL(query);
 
 query = "insert into tblWeekDays (DayOfWeek) values ('Tuesdayy')";
 database.execSQL(query);
 
 query = "insert into tblWeekDays (DayOfWeek) values ('Wednesday')";
 database.execSQL(query);
 
 query = "insert into tblWeekDays (DayOfWeek) values ('Thursday')";
 database.execSQL(query);
 
 query = "insert into tblWeekDays (DayOfWeek) values ('Friday')";
 database.execSQL(query);
 
 query = "insert into tblWeekDays (DayOfWeek) values ('Saturday')";
 database.execSQL(query);
 }

@Override
 public void onUpgrade(SQLiteDatabase database, int version_old,
 int current_version) {
 String query;
 query = "DROP TABLE IF EXISTS tblWeekDays";
 database.execSQL(query);
 onCreate(database);
 }
 public Cursor getDaysOfWeek() {
 
 String selectQuery = "SELECT * FROM tblWeekDays";
 SQLiteDatabase database = this.getWritableDatabase();
 Cursor cursor = database.rawQuery(selectQuery, null);
 
 
 return cursor;
 }

}

This is important. Over here we are creating a new sqlite database with a table in it. And then inserting data in it at the time of creation. Now edit your MainActivity.java file as below :

MainActivity.java:

package parallelcodes.simplewidget;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
 ListView listView;

SQLiteDatabase sqlDb;
 DBController dbController;

@Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 listView = (ListView) findViewById(R.id.lstView);
 int[] id = { R.id.txtListElement };
 String[] DaysOfWeek = new String[] { "DayOfWeek" };
 dbController = new DBController(this);
 sqlDb = dbController.getReadableDatabase();
 Cursor c = dbController.getDaysOfWeek();

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
 R.layout.list_template, c, DaysOfWeek, id, 0);
 listView.setAdapter(adapter);
 
 }

}

This will create a listview with below design :

Android ListView using Database

Furthermore, Android ListView control can be filled using design file via String-array, using arrayAdapter, using SimpleCursorAdapter, with custom layout file,  can be used with Images.

Please also see : Android Beginners Tutorial.


Exit mobile version