ANDROID – SIMPLE LISTVIEW USING SIMPLEADAPTER
2014. 11. 5. 13:03ㆍPrograming/Android / Java
In Android Applications, ListView helps you to display the contents of an array with flexible size. The following example shows you how to create a simple ListView.
1. First create a new Android project
2. Create the following Java class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleAdapter; public class ListViewA extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); ListView lv= (ListView)findViewById(R.id.listview); // create the grid item mapping String[] from = new String[] { "rowid" , "col_1" , "col_2" , "col_3" }; int [] to = new int [] { R.id.item1, R.id.item2, R.id.item3, R.id.item4 }; // prepare the list of all records List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>(); for ( int i = 0 ; i < 10 ; i++){ HashMap<String, String> map = new HashMap<String, String>(); map.put( from[0], "" + i); map.put( from[1], "col_1_item_" + i); map.put( from[2], "col_2_item_" + i); map.put( from[3], "col_3_item_" + i); fillMaps.add(map); } // fill in the grid_item layout SimpleAdapter adapter = new SimpleAdapter( this , fillMaps, R.layout.grid_item, from, to); lv.setAdapter(adapter); } } |
3. Create the following 2 layouts
main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | <? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout android:id = "@+id/main" android:orientation = "vertical" android:layout_height = "fill_parent" android:layout_width = "fill_parent" > <!-- Header --> < LinearLayout android:id = "@+id/header" android:background = "#ff347c12" android:layout_height = "wrap_content" android:layout_width = "fill_parent" > < TextView android:id = "@+id/item1" android:layout_height = "fill_parent" android:layout_width = "wrap_content" android:width = "20dip" android:height = "30dip" /> < TextView android:id = "@+id/item2" android:layout_height = "fill_parent" android:layout_width = "wrap_content" android:text = "col_1_h" android:width = "100dip" android:height = "30dip" /> < TextView android:id = "@+id/item3" android:layout_height = "fill_parent" android:layout_width = "wrap_content" android:text = "col_2_h" android:width = "100dip" android:height = "30dip" /> < TextView android:id = "@+id/item4" android:layout_height = "fill_parent" android:layout_width = "wrap_content" android:text = "col_3_h" android:width = "100dip" android:height = "30dip" /> </ LinearLayout > <!-- List Divider --> < View android:layout_width = "fill_parent" android:layout_height = "1dip" android:background = "?android:attr/listDivider" /> <!-- ListView (grid_items) --> < LinearLayout android:id = "@+id/layout" android:layout_width = "wrap_content" android:layout_height = "fill_parent" > < ListView android:id = "@+id/listview" android:layout_height = "fill_parent" android:layout_width = "fill_parent" > </ ListView > </ LinearLayout > </ LinearLayout > |
grid_item.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <? xml version = "1.0" encoding = "utf-8" ?> android:orientation = "horizontal" android:layout_width = "fill_parent" android:layout_height = "fill_parent" > < TextView android:id = "@+id/item1" android:text = "row_id" android:layout_height = "fill_parent" android:layout_width = "wrap_content" android:width = "20dip" /> < TextView android:id = "@+id/item2" android:text = "col_1" android:layout_height = "fill_parent" android:layout_width = "wrap_content" android:width = "100dip" /> < TextView android:id = "@+id/item3" android:text = "col_2" android:layout_height = "fill_parent" android:layout_width = "wrap_content" android:width = "100dip" /> < TextView android:id = "@+id/item4" android:text = "col_3" android:layout_height = "fill_parent" android:layout_width = "wrap_content" android:width = "100dip" /> </ LinearLayout > |
4. Try it!
http://eureka.ykyuen.info/2010/01/03/android-simple-listview-using-simpleadapter/
자료를 가지고 와서 수정한 내용입니다.
'Programing > Android / Java' 카테고리의 다른 글
Android JSON parsing using Volley (0) | 2014.11.05 |
---|---|
Android - How to Create Google Glass Options Menu? (0) | 2014.11.05 |
[Android] LIST OF HCE (HOST CARD EMULATION) APPS (0) | 2014.09.23 |
[Java Card - SmartCard] Developing a Java Card Applet (0) | 2014.09.22 |
[Android] Bitmap - Caused by: java.lang.OutOfMemoryError (0) | 2014.09.19 |