Android

How to Display Listview using CursorAdapter and Sqlite Database in Android

How to Display Listview using CursorAdapter and Sqlite Database in Android

The objective of this article is to get data from SQLite database and then attach that to the Listview.

Source for Activity – AndroidListViewCursorAdaptorActivity.java

Source for SQLite DB Adapter – CountriesDbAdapter.java

Source for Country.java

 

Source for Main Screen Layout – landingscreen.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:padding=”10dp”
android:text=”@string/some_text” android:textSize=”20sp” />

<EditText android:id=”@+id/myFilter” android:layout_width=”match_parent”
android:layout_height=”wrap_content” android:ems=”10″
android:hint=”@string/some_hint”>
<requestFocus />
</EditText>

<ListView android:id=”@+id/listView1″ android:layout_width=”fill_parent”
android:layout_height=”fill_parent” />

</LinearLayout>

Source for List Layout – country_info.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:orientation=”vertical”
android:padding=”6dip” >

<TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_alignParentTop=”true”
android:text=”Code: ”
android:textAppearance=”?android:attr/textAppearanceMedium” />

<TextView
android:id=”@+id/textView2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/textView1″
android:layout_below=”@+id/textView1″
android:text=”Name: ”
android:textAppearance=”?android:attr/textAppearanceMedium” />

<TextView
android:id=”@+id/textView3″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/textView2″
android:layout_below=”@+id/textView2″
android:text=”Continent: ”
android:textAppearance=”?android:attr/textAppearanceMedium” />

<TextView
android:id=”@+id/textView4″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/textView3″
android:layout_below=”@+id/textView3″
android:text=”Region: ”
android:textAppearance=”?android:attr/textAppearanceMedium” />

<TextView
android:id=”@+id/continent”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignBaseline=”@+id/textView3″
android:layout_alignBottom=”@+id/textView3″
android:layout_toRightOf=”@+id/textView3″
android:text=”TextView” />

<TextView
android:id=”@+id/region”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignBaseline=”@+id/textView4″
android:layout_alignBottom=”@+id/textView4″
android:layout_alignLeft=”@+id/continent”
android:text=”TextView” />

<TextView
android:id=”@+id/name”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_above=”@+id/textView3″
android:layout_toRightOf=”@+id/textView3″
android:text=”TextView” />

<TextView
android:id=”@+id/code”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_above=”@+id/textView2″
android:layout_alignLeft=”@+id/name”
android:text=”TextView” />

</RelativeLayout>

Source for application variables – strings.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>

<string name=”app_name”>ListView CusrsorAdapter</string>
<string name=”some_text”>
Some country name!
</string>
<string name=”some_hint”>
Type here to filter&#8230;
</string>

</resources>

Source for application manifest – AndroidManifest.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=“com”.mycodetipsandroid android:versionCode=”1″
android:versionName=”1.0″>

<uses-sdk android:minSdkVersion=”15″ />

<application android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name” android:theme=”@android:style/Theme.Light”>
<activity android:name=”.AndroidListViewCursorAdaptorActivity”
android:label=”@string/app_name”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>

</manifest>

mycodetips

Passionate about coding and software development holding 10+ Years of experience including 6+ years of experience on Mobile application Development.

More Posts