Basic ListView With HashMap
Using Android Studio 1.4
Application Name: MyListViewHashMap1
Select API 14:Android 4.0 (IceCreamSandwich)
Follow the wizard to create New Empty Activity.
Select “Generate Layout File”
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:divider="@null" android:dividerHeight="0dip" /> </LinearLayout> </RelativeLayout> |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/logo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="8dp" > </ImageView> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:orientation="vertical" android:paddingLeft="0px" android:paddingRight="5dp" > <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:textColor="#FFF38585" android:textSize="15sp" > </TextView> <TextView android:id="@+id/region" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="4dp" android:textColor="#FF2CE4A4" android:textSize="13sp" > </TextView> </LinearLayout> </LinearLayout> |
Download MyListViewHashMap1-drawables.zip place the images into res/drawable folder.
package com.notarazi.mylistviewhashmap1; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.ListView; import android.widget.SimpleAdapter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MainActivity extends AppCompatActivity { private ListView mListView; @Override protected void onCreate (Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (ListView) findViewById(R.id.list); List<Map<String, java.io.Serializable>> data = GetSampleData(); SimpleAdapter adapter; adapter = new SimpleAdapter(this, data, R.layout.row_layout, new String[] { "logo", "name", "region" }, new int[] { R.id.logo, R.id.name, R.id.region }); mListView.setAdapter(adapter); } List<Map<String, java.io.Serializable>> GetSampleData(){ List<Map<String, java.io.Serializable>> list = new ArrayList<Map<String, java.io.Serializable>>(); Map<String, java.io.Serializable> map = new HashMap<String, java.io.Serializable>(); map.put("logo", R.drawable.car_honda); map.put("name", "Honda"); map.put("region", "Asia"); list.add(map); map = new HashMap<String, java.io.Serializable>(); map.put("logo", R.drawable.car_hyundai); map.put("name", "Hyundai"); map.put("region", "Asia"); list.add(map); map = new HashMap<String, java.io.Serializable>(); map.put("logo", R.drawable.car_toyota); map.put("name", "Toyota"); map.put("region", "Asia"); list.add(map); map = new HashMap<String, java.io.Serializable>(); map.put("logo", R.drawable.car_kia); map.put("name", "Kia"); map.put("region", "Asia"); list.add(map); map = new HashMap<String, java.io.Serializable>(); map.put("logo", R.drawable.car_proton); map.put("name", "Proton"); map.put("region", "Asia"); list.add(map); return list; } } |
OUTCOME.
https://shenhengbin.wordpress.com/2012/03/17/listview-simpleadapter/