You are on page 1of 6

Experiment - 10

Multiple value display using SQLite database

XML Code:

<?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=".SqLite.Sq9_ReadMultiple">

<ListView

android:id="@+id/Sq9_lst"

android:layout_width="match_parent"

android:layout_height="wrap_content"></ListView>

</LinearLayout>

Java Code:

package com.e.seeta.SqLite;

import android.content.Context;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;
import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.TextView;

import com.e.seeta.Control2.E43_ListViewMulti;

import com.e.seeta.R;

public class Sq9_ReadMultiple extends AppCompatActivity {

ListView lst;

String[] arName ;

String[] arFName;

String[] arCity;

SQLiteDatabase db;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_sq9__read_multiple);

lst = (ListView) findViewById(R.id.Sq9_lst);

db = openOrCreateDatabase("db_ShriRam", MODE_PRIVATE, null);

db.execSQL("CREATE TABLE IF NOT EXISTS tbl_Student (SId INTEGER PRIMARY KEY


AUTOINCREMENT, SName varchar(200),FName varchar(200),City varchar(200))");

FillList();

private void FillList()


{

String sql;

sql="SELECT * FROM tbl_Student ";

Cursor c=db.rawQuery(sql,null);

c.moveToLast();

int n=c.getCount();

c.moveToFirst();

arName=new String[n];

arFName=new String[n];

arCity=new String[n];

for(int i=0;i<n;i++)

arName[i]=c.getString(c.getColumnIndex("SName"));

arFName[i]=c.getString(c.getColumnIndex("FName"));

arCity[i]=c.getString(c.getColumnIndex("City"));

c.moveToNext();

CustAdpt shri = new CustAdpt(this, arName, arFName,arCity);

lst.setAdapter(shri);

public class CustAdpt extends ArrayAdapter<String> {

String[] mName;

String[] mFName;
String[] mCity;

Context mContext;

public CustAdpt(Context tcontext, String[] tname, String[] tfname, String[] tcity) {

super(tcontext, R.layout.activity_sq9__read_multiple_row, R.id.Sq9_lbl_Name, tname);

mCity = tcity;

mName = tname;

mFName = tfname;

mContext = tcontext;

@Override

public View getView(int position, View convertview, ViewGroup parent) {

View row=convertview;

ViewHolder vHolder=null;

if(row==null)

LayoutInflater
inflater=(LayoutInflater)getApplicationContext().getSystemService(mContext.LAYOUT_INFLA
TER_SERVICE);

row= inflater.inflate(R.layout.activity_sq9__read_multiple_row,parent,false);

vHolder=new ViewHolder(row);

row.setTag(vHolder);

else

{
vHolder=(ViewHolder)row.getTag();

vHolder.txtCity.setText(mCity[position]);

vHolder.txtFName.setText(mFName[position]);

vHolder.txtName.setText(mName[position]);

return row;

public class ViewHolder {

TextView txtCity, txtName, txtFName;

public ViewHolder(View v) {

txtCity = (TextView) v.findViewById(R.id.Sq9_lbl_City);

txtName = (TextView) v.findViewById(R.id.Sq9_lbl_Name);

txtFName = (TextView) v.findViewById(R.id.Sq9_lbl_FName);

Row File: activity_sq9__read_multiple_row

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"

android:orientation="horizontal"

android:layout_height="match_parent">

<TextView

android:id="@+id/Sq9_lbl_Name"

android:layout_weight="1"

android:layout_width="0dp"

android:layout_height="wrap_content" />

<TextView

android:id="@+id/Sq9_lbl_FName"

android:layout_weight="1"

android:layout_width="0dp"

android:layout_height="wrap_content" />

<TextView

android:id="@+id/Sq9_lbl_City"

android:layout_weight="1"

android:layout_width="0dp"

android:layout_height="wrap_content" />

</LinearLayout>

You might also like