Populating listview with multiple columns from sqlite database in android
new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
|
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”); |
|
1 |
aradhanelist.add(map); |
- 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.
DBAdapter dbAdapter=DBAdapter.getDBAdapterInstance(Aradhane.this);
try {new SimpleAdapter(Aradhane.this,aradhaneList, R.layout.aradhane_list, new String[]{TAG_MAAS, TAG_PAKSHA, TAG_TITHI,TAG_ARADHANE,TAG_VRD}, |
|
|
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”> android:layout_height=”match_parent” <include layout=”@layout/aradhaneheader” /> <TableRow android:layout_height=”match_parent” </TableLayout> |


