简单的listview(非活动)示例?

时间:2011-08-22 11:16:59

标签: android listview layout

我已经这样做了几个月了,我发现几乎不可能找到ListView是ListView而不是ListActivity的例子。在我的程序中,我想要做的就是将一半的屏幕放在ListView上,另一半放在其他的东西上,这样ListView就可以在布局中移动了。有谁知道这样的例子吗?

1 个答案:

答案 0 :(得分:0)

我创建了ListView的实现,而不是ListActivity。

<强> list_produk.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<ListView
    android:id="@+id/lvListProduk"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.98"
    tools:listitem="@layout/list_produk_row" >
</ListView>

<Button
    android:id="@+id/btnListProdukBack"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/back_" />

</LinearLayout>

<强> list_produk_row.xml:

<?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="vertical" >

<TextView
    android:id="@+id/tvProdukName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>

<强> ListProduk.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_produk);

    String[] from = new String[] {"prd_name"};
    int[] to = new int[] {R.id.tvProdukName};

    List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();

    HashMap<String, String> map = new HashMap<String, String>();
    map.put("prd_name", "BlackBerry");
    fillMaps.add(map);

    map = new HashMap<String, String>();
    map.put("prd_name", "Android");
    fillMaps.add(map);

    map = new HashMap<String, String>();
    map.put("prd_name", "iPhone");
    fillMaps.add(map);

    SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.list_produk_row, from, to);
    ListView listView = (ListView) findViewById(R.id.lvListProduk);
    listView.setAdapter(adapter);
}

list_produk_row.xml,包含TextView,是ListView列表项的布局。