ListView control is relatively common, but have always felt a bit tedious steps to create a ListView, so to sum up what, for easy access.

The program's performance is to achieve a ListView, ListView inside title, content and images, and to join click and long press response.

First, the definition in the xml inside a ListView

1
  <? xml version = "1.0" encoding = "utf-8"?> <LinearLayout android: id = "@ + id/LinearLayout01" android: layout_width = "fill_parent" android: layout_height = "fill_parent" xmlns: android = "http : / / schemas.android.com / apk / res / android "> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ListView01" /> </ LinearLayout>

ListView each entry defines the Layout, with RelativeLayout achieve:

1
  <? xml version = "1.0" encoding = "utf-8"?> <RelativeLayout android: id = "@ + id/RelativeLayout01" android: layout_width = "fill_parent" xmlns: android = "http://schemas.android. com / apk / res / android "android: layout_height =" wrap_content "android: paddingBottom =" 4dip "android: paddingLeft =" 12dip "android: paddingRight =" 12dip "> <ImageView android: paddingTop =" 12dip "android: layout_alignParentRight = "true" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id / ItemImage" /> <TextView android: text = "TextView01" android: layout_height = "wrap_content" android: textSize = "20dip" android: layout_width = "fill_parent" android: id = "@ + id / ItemTitle" /> <TextView android: text = "TextView02" android: layout_height = "wrap_content" android: layout_width = "fill_parent" android: layout_below = "@ + id / ItemTitle" android: id = "@ + id / ItemText "/></ RelativeLayout>

Finally, in Activity inside the call and join the Listener, see in particular Note:

1
  package com.ray.test; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.os.Bundle; import android.view.ContextMenu; import android.view.MenuItem; import android.view.View; import android.view.ContextMenu.ContextMenuInfo; import android.view.View.OnCreateContextMenuListener; import android.widget.AdapterView; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget. AdapterView.OnItemClickListener; public class TestListView extends Activity (@ Override public void onCreate (Bundle savedInstanceState) (super.onCreate (savedInstanceState); setContentView (R.layout.main); / / Bind Layout inside the ListView ListView list = (ListView) findViewById (R.id.ListView01); / / generate dynamic array, adding the data ArrayList <HashMap <String, Object>> listItem = new ArrayList <HashMap <String, Object>> (); for (int i = 0; i < 10; i + +) (HashMap <String, Object> map = new HashMap <String, Object> (); map.put ( "ItemImage", R.drawable.checked); / / Image resource ID map.put ( "ItemTitle "," Level "+ i); map.put (" ItemText "," Finished in 1 Min 54 Secs, 70 Moves! "); listItem.add (map);) / / build adapter Item and dynamic arrays corresponding , "ItemText"), / / ImageItem of the XML files inside a ImageView, two TextView ID new int [] (R.id.ItemImage, R.id.ItemTitle, R.id.ItemText)); / / add and Show list.setAdapter (listItemAdapter); / / add the click list.setOnItemClickListener (new OnItemClickListener () (@ Overridepublic void onItemClick (AdapterView <?> arg0, View arg1, int arg2, long arg3) (setTitle ( "Click on the first" + arg2 + "project ");}}); / / Add a long-per-click list.setOnCreateContextMenuListener (new OnCreateContextMenuListener () (@ Overridepublic void onCreateContextMenu (ContextMenu menu, View v, ContextMenuInfo menuInfo) (menu.setHeaderTitle (" long-press menu-ContextMenu "); menu.add (0, 0, 0," pop-up menu, long press 0 "); menu.add (0, 1, 0," pop-up menu, a long press ");)));) / / long press Menu response function @ Overridepublic boolean onContextItemSelected (MenuItem item) (setTitle ( "Click a long press the menu inside the first" + item.getItemId () + "items"); return super.onContextItemSelected (item);))