ListView

Z Android Wiki

Přejít na: navigace, hledání

ListView s vlastními prvky

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" android:layout_height="wrap_content"
	android:orientation="horizontal">

	<ImageView android:id="@+id/icon" android:layout_width="wrap_content"
		android:paddingLeft="2px" android:paddingRight="2px"
		android:paddingTop="2px" android:layout_height="fill_parent"
		android:src="@drawable/icon" />

	<LinearLayout
		android:orientation="vertical" android:id="@+id/LinearLayout01"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:layout_weight="1">
		<TextView android:id="@+id/label" android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:textSize="20sp" />
		<LinearLayout android:orientation="horizontal"
			android:id="@+id/LinearLayout02" android:layout_width="wrap_content"
			android:layout_height="wrap_content">
			<TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
				android:orientation="vertical" android:layout_height="wrap_content"
				android:text="pokus radek"></TextView>
			<TextView android:id="@+id/TextView02" android:layout_width="wrap_content"
				android:layout_height="wrap_content" android:text="adresa firmy"></TextView>
		</LinearLayout>
	</LinearLayout>
	<ImageView android:id="@+id/ImageView01"
		android:layout_width="wrap_content" android:layout_height="fill_parent"
		android:background="@drawable/icon" android:layout_gravity="right"></ImageView>
</LinearLayout>

Java kod:

/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("train", "text text");
		map.put("from", "6:30 AM");
		mylist.add(map);
		map = new HashMap<String, String>();//pro nove pridani musim vytvorit vzdy novy objekt
		map.put("train", "103(x)");
		map.put("from", "6:35 AM");
		mylist.add(map);
		map = new HashMap<String, String>();
		map.put("train", "text text2");
		map.put("from", "pokus");
		mylist.add(map);

setListAdapter(new SimpleAdapter(this, mylist, R.layout.list,
	            new String[] {"train", "from"}, new int[] {R.id.label, R.id.TextView01}));
        }

@Override
	protected void onListItemClick(android.widget.ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Log.w("ListView.onListItemClick", "onListItemClick klik");//reakce na jednotliva kliknuti
	} 

Čerpáno zde.