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

8 comments:

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

john jersy said...

Well somehow I got to read lots of articles on your blog. It’s amazing how interesting it is for me to visit you very often.
digital marketing training in marathahalli

digital marketing training in rajajinagar

gowsalya said...

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
full stack developer training in annanagar

full stack developer training in tambaram

full stack developer training in velachery

sai said...

Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
python training institute in chennai
python training in Bangalore
python training in pune

isai 14 said...

This is a nice article here with some useful tips for those who are not used-to comment that frequently. Thanks for this helpful information I agree with all points you have given to us. I will follow all of them.
Blueprism training in velachery

Blueprism training in marathahalli

Blueprism training in btm

lenin christ said...

I’ve desired to post about something similar to this on one of my blogs and this has given me an idea. Cool Mat.
Data Science training in rajaji nagar
Data Science training in chennai
Data Science training in electronic city
Data Science training in USA
Data science training in pune
Data science training in kalyan nagar

Afiah B said...

This is such a great post, and was thinking much the same myself. Another great update.

java training in chennai | java training in bangalore

java online training | java training in pune

Revathy A said...

Very good brief and this post helped me alot. Say thank you I searching for your facts. Thanks for sharing with us!
angularjs-Training in pune

angularjs-Training in chennai

angularjs Training in chennai

angularjs-Training in tambaram

angularjs-Training in sholinganallur