Android ListView SimpleAdapter

In this Android tip, I am going to show you how to use SimpleAdapter to bind ListView to a data source. The SimpleAdapter is convenient class to map static data to views that defines in an xml layout file of the ListView. You can specify data in an ArrayList of HashMaps. Each item of the ArrayList corresponds to one row of the ListView. A HashMap object of every item stores data values for all columns of a row. For example, if a ListView has two columns, pname and pprice, the HashMap object must have two entries, one for the first column and another one for the second column. The keys of the entries must be mapped to specific views in the xml layout file. The views that you can define in xml layout files are CheckBox, TextView, and ImageView.
Now to have an example application on using SimpleAdapter with ListView, you create a new Android project named AndExample. In the activity_main.xml file, you have to add one ListView, two EditTexts, and one Button as shown below.


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="com.example.andexample.MainActivity"
   android:orientation="vertical"
   >
  
<LinearLayout 
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     >
<EditText
   android:id="@+id/txtname"
   android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:hint="Enter product name"
     android:layout_weight="1.5"
   />
<EditText
   android:id="@+id/txtprice"
   android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:hint="Enter unit price"
     android:layout_weight="1.5"
   />

<Button
   android:id="@+id/btadd"
   android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Add"
     android:layout_weight="0.5"
     android:onClick="addRow"
   />
</LinearLayout>

   <ListView
     android:id="@+id/lst"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
   />

</LinearLayout>


Then in the res/layout file, create a row layout file (row.xml) of the ListView. Every row of the ListView has two columns so two EditTexts are needed.

<?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" >
  
   <TextView
   android:id="@+id/pname"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:padding="10sp"
   android:textStyle="bold" >
</TextView>
<TextView
   android:id="@+id/pprice"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:padding="10sp"
       >
</TextView>

</LinearLayout>



Here is the content of the MainActivity class that hosts the ListView.

 package com.example.andexample;
import java.util.ArrayList;
import java.util.HashMap;
import android.support.v7.app.ActionBarActivity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;


public class MainActivity extends ActionBarActivity {

SimpleAdapter adapter;
HashMap<String, String> map;
EditText txtname;
EditText txtprice;
ArrayList<HashMap<String, String>> alist;
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
     ListView lv=(ListView)findViewById(R.id.lst);
     txtname=(EditText)findViewById(R.id.txtname);
     txtprice=(EditText)findViewById(R.id.txtprice);
     // ArrayList for data row
     alist = new ArrayList<HashMap<String, String>>();
     // SimpleAdapter mapping static data to views in xml file
     adapter = new SimpleAdapter(this, alist, R.layout.row, new String[] {"pname", "pprice"}, new int[] {R.id.pname, R.id.pprice});
     lv.setAdapter(adapter);
   }

   public void addRow(View view){
   // HashMap for data values of each row
   map = new HashMap<String, String>();    
   map.put("pname", txtname.getText().toString());
   map.put("pprice", txtprice.getText().toString());
   alist.add(map);
   // Notify the ListView of data changed
   adapter.notifyDataSetChanged();
  
  
   }
  
}

Android-ListView-SimpleAdapter

Posted by: Dara | post date: 11-01-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.