Thursday, June 30, 2016

Populating listview with multiple columns from sqlite database in android

Greetings Of The Day!!!

This Blog teaches you how to implement multi column list view in Android. Data of the list view is fetched from the sqlite database or it can be hard coded. We discuss both the scenarios.

Here is the listview with multiple rows and multiple columns as : Maas, Paksha, Tithi, Aradhane and Vrudavan Place.



This information is available in the sqlite database.Table Name is: Aradhane and the structure is as below:



The sample of the data's available is as below:



First Create the Keys for the hashmap to store the strings

private static final String TAG_MAAS = "maas"; private static final String TAG_PAKSHA = "paksha"; private static final String TAG_TITHI = "tithi"; private static final String TAG_ARADHANE = "aradhane"; private static final String TAG_VRD = "vrindavan_place";

Create a Listview


ListView lv;

lv = (ListView) findViewById(R.id.listView);
?
Create an Arraylist which will hold the HashMap


ArrayList<HashMap<String, String>> aradhanelist=
new ArrayList<HashMap<String, String>>();


HashMap<String, String> map = new HashMap<String, String>();

Add data to HashMap in Key-Value form
Here the data is hard coded.
1
2
3
4
5
map.put(TAG_MAAS, "Chaitra");
map.put(TAG_PAKSHA, "Shukla"); map.put(TAG_TITHI, "Navami"); map.put(TAG_ARADHANE, "Kaveendra Teertharu"); map.put(TAG_VRD, "Navavrumdavan Gadde");
Add HashMap to ArrayList
1
aradhanelist.add(map);
Now Create a SimpleAdapter


  • Pass context as first argument.
  • In the second argument you need to pass the layout in which you want your list to appear.
  • Then for each column you need to add a string value which is a key in your hashmap. Keys as defined at start of the program
  • Corresponding to each column you need to pass the view-item id to this constructor as a fourth argument.
  • Each new hashmap is our new row.

SimpleAdapter adapter =
new SimpleAdapter(Aradhane.this,aradhaneList, R.layout.aradhane_list,
new String[]{TAG_MAAS, TAG_PAKSHA, TAG_TITHI,TAG_ARADHANE,TAG_VRD}, new int[]{R.id.maas, R.id.paksha, R.id.tithi,R.id.aradhane,R.id.vrind});
// updating listview lv.setAdapter(adapter);

Instead of hard coding the data, you can get from the sqlite. for that you can use the below code:


DBAdapter dbAdapter=DBAdapter.getDBAdapterInstance(Aradhane.this);
try { dbAdapter.createDataBase(); } catch (IOException e) { Log.i("*** select ",e.getMessage()); } dbAdapter.openDataBase(); String query="SELECT * FROM aradhane"; ArrayList<ArrayList<String>> stringList = dbAdapter.selectRecordsFromDBList(query, null); dbAdapter.close(); int max= stringList.size()-1; for (int i = 0; i < max; i++) { ArrayList<String> list = stringList.get(i); HashMap<String, String> map = new HashMap<String, String>(); String maas=list.get(0); String paksha=list.get(1); String tithi=list.get(2); String aradhane=list.get(3); String vrinda=list.get(4); map.put(TAG_MAAS, maas); map.put(TAG_PAKSHA, paksha); map.put(TAG_TITHI, tithi); map.put(TAG_ARADHANE, aradhane); map.put(TAG_VRD, vrinda); aradhaneList.add(map); } SimpleAdapter adapter =
new SimpleAdapter(Aradhane.this,aradhaneList, R.layout.aradhane_list,
new String[]{TAG_MAAS, TAG_PAKSHA, TAG_TITHI,TAG_ARADHANE,TAG_VRD}, new int[]{R.id.maas, R.id.paksha, R.id.tithi,R.id.aradhane,R.id.vrind}); // updating listview lv.setAdapter(adapter);           


Aradhane.java looks like this:


package com.sarga.simplecal;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Aradhane extends Activity  {
    ListView listView,lv;
    private static final String TAG_MAAS = "maas";
    private static final String TAG_PAKSHA = "paksha";
    private static final String TAG_TITHI = "tithi";
    private static final String TAG_ARADHANE = "aradhane";
    private static final String TAG_VRD = "vrindavan_place";

    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.aradhane);
        
        aradhaneList = new ArrayList<HashMap<String, String>>();
        
        lv = (ListView) findViewById(R.id.list);
        GetAradhaneList();
        
         }
   
    public void GetAradhaneList(){
    
    
      
DBAdapter dbAdapter=DBAdapter.getDBAdapterInstance(Aradhane.this);
  try {
  dbAdapter.createDataBase();
  } catch (IOException e) {
  Log.i("*** select ",e.getMessage());
  }
      dbAdapter.openDataBase();
 
      String query="SELECT * FROM aradhane";
     
      ArrayList<ArrayList<String>> stringList = dbAdapter.selectRecordsFromDBList(query, null);
 
  dbAdapter.close();
 
  int max= stringList.size()-1;
  for (int i = 0; i < max; i++) {
  ArrayList<String> list = stringList.get(i);
  HashMap<String, String> map = new HashMap<String, String>();
  String  maas=list.get(0);
  String  paksha=list.get(1);
  String  tithi=list.get(2);
  String  aradhane=list.get(3);
  String vrinda=list.get(4);
 
  map.put(TAG_MAAS, maas);
                        map.put(TAG_PAKSHA, paksha);
                        map.put(TAG_TITHI, tithi);
                        map.put(TAG_ARADHANE, aradhane);
                        map.put(TAG_VRD, vrinda);
                        aradhaneList.add(map);
  }
 
    
  SimpleAdapter adapter =
 new SimpleAdapter(Aradhane.this,aradhaneList, 
R.layout.aradhane_list, new String[]{TAG_MAAS, TAG_PAKSHA, TAG_TITHI,TAG_ARADHANE,TAG_VRD},
                 new int[]{R.id.maas, R.id.paksha, R.id.tithi,R.id.aradhane,R.id.vrind});
          // updating listview
          lv.setAdapter(adapter);
          
          return;
    
    }
    

      
}



Layouts :
aradhane.xm

<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:shrinkColumns="*" android:stretchColumns="*" android:orientation="vertical"> <TableRow android:layout_height="match_parent" android:gravity="center_horizontal" > <include layout="@layout/aradhaneheader" /> </TableRow> <TableRow android:layout_height="match_parent" android:gravity="center_horizontal" > <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/list" android:layout_gravity="center_horizontal" android:layout_weight="1" /> </TableRow> </TableLayout>
Aradhaneheader.xml

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

    <TableLayout
        android:background="#008000"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal">

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="match_parent">

            <TextView
                android:textColor="#1B0082"
                android:layout_span="10"
                android:text="Aradhane List"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:textStyle="bold"
                android:gravity="top|center"
                />

        </TableRow>

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <TextView
                android:text="Maas"
                 android:textColor="#1B0082"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="15dp"
               />

            <TextView
                android:text="Paksha"
                 android:textColor="#1B0082"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="10dp"
                />



            <TextView
                android:text="Tithi"
                 android:textColor="#1B0082"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_column="7"
                android:layout_marginRight="15dp"/>
         
            <TextView
                android:text="Aradhane"
                android:textColor="#1B0082"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_column="7"
                android:layout_marginRight="10dp"
                android:layout_marginLeft="4dp" />
         
           
            <TextView
                android:text="Vrundavan@"
                 android:textColor="#1B0082"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_column="7"
                android:layout_marginRight="5dp"/>

        </TableRow>


    </TableLayout>


</LinearLayout>

Aradhane_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:shrinkColumns="*"
    android:stretchColumns="*"
    android:orientation="vertical">


    <TableRow
     
        android:layout_margin="1dip"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:layout_width="0dp"
        android:background="#ff9999" >

 
    <TextView
        android:id="@+id/maas"
   
        android:textColor="#1B0082"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="left|center"
        android:layout_margin="1dip"
        android:layout_span="10"/>

        <TextView
            android:id="@+id/paksha"
         
            android:textColor="#1B0082"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="left|center"
            android:layout_margin="1dip"
            android:layout_span="10"/>

    <TextView
        android:id="@+id/tithi"
     
        android:textColor="#1B0082"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="left|center"
        android:layout_margin="1dip"
        android:layout_span="10" />
 
     <TextView
        android:id="@+id/aradhane"
     
        android:textColor="#1B0082"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:layout_margin="1dip"
        android:layout_span="20" />
   
      <TextView
        android:id="@+id/vrind"
     
        android:textColor="#1B0082"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:layout_margin="1dip"
        android:layout_span="10" />

 
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    </TableRow>


</TableLayout>
 

That's it. Happy Coding

1 comment:

Saranya N said...

This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.Android Training institute in chennai with placement | Best Android Training in velachery