Programming Examples
Android app to demonstrate the list view
Android application to Show data on ListView from SQLiIte Database
Output
main_activity.xml
<?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"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="Student Name"/>
<EditText
android:id="@+id/stu_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="15dp"
android:text="Mobile No."/>
<EditText
android:id="@+id/stu_mobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:inputType="textPersonName"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="15dp"
android:text="Email ID"/>
<EditText
android:id="@+id/stu_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:inputType="textPersonName"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:gravity="center">
<Button
android:id="@+id/btn_save"
android:layout_width="120dp"
android:layout_height="60dp"
android:text="SAVE"/>
<Button
android:id="@+id/btn_view"
android:layout_width="120dp"
android:layout_height="60dp"
android:layout_marginLeft="20dp"
android:text="VIEW"/>
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.dbconnection;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText t1,t2,t3;
Button b1,b2;
dbHelper DB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar=getSupportActionBar();
actionBar.setTitle("Add Student");
t1=findViewById(R.id.stu_name);
t2=findViewById(R.id.stu_mobile);
t3=findViewById(R.id.stu_email);
b1=findViewById(R.id.btn_save);
b2=findViewById(R.id.btn_view);
b3=findViewById(R.id.btn_view_all);
DB=new dbHelper(this);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name=t1.getText().toString();
String mobile=t2.getText().toString();
String email=t3.getText().toString();
Boolean chkIns=DB.insertData(name,mobile,email);
if(chkIns==true)
{
Toast.makeText(MainActivity.this,"Data Saved Successfully",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.this,"Some Error in Insert data",Toast.LENGTH_SHORT).show();
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,StudentList.class);
startActivity(intent);
}
});
}
}
dbHelper.java
package com.example.dbconnection;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class dbHelper extends SQLiteOpenHelper {
public dbHelper(@Nullable Context context) {
super(context, "stu.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create Table Userdetails(name TEXT primary key,mobile TEXT,email TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop Table if exists Userdetails");
}
public Boolean insertData(String name,String mobile,String email)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put("name",name);
contentValues.put("mobile",mobile);
contentValues.put("email",email);
long result=db.insert("Userdetails",null,contentValues);
if(result==-1)
{
return false;
}
else
{
return true;
}
}
public Cursor getData()
{
SQLiteDatabase db=this.getWritableDatabase();
Cursor cursor=db.rawQuery("select * from Userdetails",null);
return cursor;
}
}
activity_student_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".StudentList">
<ListView
android:id="@+id/stu_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
StudentList.java
package com.example.dbconnection;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class StudentList extends AppCompatActivity {
private dbHelper DB;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_list);
ActionBar actionBar=getSupportActionBar();
actionBar.setTitle("Student List");
DB=new dbHelper(this);
listView=findViewById(R.id.stu_list);
Cursor res=DB.getData();
ArrayList<String> arrayList=new ArrayList<>();
//StringBuffer buffer=new StringBuffer();
while(res.moveToNext()){
arrayList.add("+"+res.getString(0)+"\n"+res.getString(1));
//buffer.append("Name : "+res.getString(0)+"\t");
//buffer.append("Mobile : "+res.getString(1)+"\t");
// buffer.append("Email : "+res.getString(2)+"\n");
}
ArrayAdapter arrayAdapter=new ArrayAdapter(this, android.R.layout.simple_list_item_1,arrayList);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Toast.makeText(StudentList.this,"Clicked Item"+position+" "+arrayList.get(position).toString(),Toast.LENGTH_LONG).show();
Intent intent=new Intent(StudentList.this,StudentProfile.class);
intent.putExtra("NAME","dkm");
startActivity(intent);
}
});
}
}