Android Database ListView Example

               Everyone loves in android development,but many are facing difficulty in android database.Android Database applications are also very difficult to implement.Let me show how it is possible in a simple way.

Refer:-Android Listview Example

Now we are going to combine with listview and sqlite database.Before going to learn you may know the basics of android listview and Sqlite Database

Refer:-Android Database Tutorials


Refer:-How to insert data into database

Lets start learning

1.Create an Android Project

Refer:-How to create Android Project

2.Now are going to design UI for the project.
Here we are going to add name to database and display it in a listview in the same   layout

Lets the main.xml code is show bellow with a textview,edittext and 2 buttons names SAVE and VIEW and a listView

<?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” >

    <EditText
android:id=”@+id/entername”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:hint=”Enter your name?” >

    </EditText>

<LinearLayout
xmlns:android=”
http://schemas.android.com/apk/res/android”
    android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:layout_gravity=”center”
android:orientation=”horizontal” >

    <Button
android:id=”@+id/bsave”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_weight=”0.33″
android:text=”SAVE” />

    <Button
android:id=”@+id/bview”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_weight=”0.30″
android:text=”VIEW” />

</LinearLayout>
<TextView
android:id=”@+id/header”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:hint=”Names in Database”
android:textColor=”#006699″ >

    </TextView>

    <ListView
android:id=”@+id/list”
android:layout_width=”match_parent”
android:layout_height=”wrap_content” >
</ListView>

</LinearLayout>

3.Now its time to do the background works

  Lets the code is given belllow first

package com.androidituts.dblistview;

import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class DatabaselistviewActivity extends Activity
{
SQLiteDatabase mydb;
TextView name,email;
Button save,myview;
ListView lv;
ArrayList data;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

name=(TextView)findViewById(R.id.entername);
save=(Button) findViewById(R.id.bsave);

//=====CODE FOR SHOWING THE DATA IN TOAST  =======

lv=(ListView) findViewById(R.id.list);
lv.setOnItemClickListener(new OnItemClickListener()
{

     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
{
Toast.makeText(getApplicationContext(), data.get(arg2).toString(),3000).show();
}

});

mydb = DatabaselistviewActivity.this.openOrCreateDatabase(“androidituts”, MODE_PRIVATE, null);
mydb.execSQL(“CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY AUTOINCREMENT,name varchar,email varchar);”);

//=== SAVE BUTTON FOR INSERTING DATA TO DATABASE =======

save.setOnClickListener(new View.OnClickListener()
{

public void onClick(View v)
{
mydb.execSQL(“insert into test (name) values(?);”,new String[]{name.getText().toString()});
Toast.makeText(getApplicationContext(), “DATA INSERTED”, 3000).show();

}
});

myview=(Button)findViewById(R.id.bview);
data=new ArrayList();

//=VIEW BUTTON TO SHOW DATA INSERTED IN THE LISTVIEW=======

myview.setOnClickListener(new View.OnClickListener()
{

public void onClick(View v)
{
Toast.makeText(getApplicationContext(), “clicked”, 3000).show();
Cursor cursor2=mydb.rawQuery(“SELECT * FROM test;”, null);

if  (cursor2.moveToFirst())
{
Toast.makeText(getApplicationContext(), “Name’s are:”, 3000).show();
data.clear();
do
{
data.add(cursor2.getString(cursor2.getColumnIndex(“name”)));
}
while (cursor2.moveToNext());


//====CODE FOR SHOWING DATA AS A SIMPLE LIST ITEM=========================================

ArrayAdapter <String> adapter=new ArrayAdapter<String>(DatabaselistviewActivity.this,android.R.layout.simple_list_item_1,data);
lv.setAdapter(adapter);
}
else
{
Toast.makeText(getApplicationContext(), “DATA NOT AVAILABLE”, 3000).show();
}
cursor2.close();
}
});
}
}

 

4.The code is explained shortly in the code itself

5.Run the application

6.Output is shown bellow.

 

datalist1

datalist2

 

Hope you all understood the project

Ask your doubts in the comment box,