如何在Android中为项目制作列表视图?

时间:2010-08-31 07:21:53

标签: android

请帮帮我。我需要制作列表视图,如我的 Android应用示例图片中所示。怎么做?此外,我还需要在示例图片中添加图像。并将其链接到另一个视图以显示更多详细信息。请查看下面显示的链接。

alt text

pic1

2 个答案:

答案 0 :(得分:3)

首先在layout / list_item.xml(或任何名称)中为列表项创建布局。 我只显示了该项目的文字字段,但您可以更改下面的视图,同时包含图片和标签。

  <TextView xmlns:a="http://schemas.android.com/apk/res/android"
       a:layout_width="fill_parent"
       a:layout_height="wrap_content"
       a:textSize="14dp"
       a:paddingBottom="5dp"
       a:paddingTop="5dp">
   </TextView>

在另一个布局中定义listView,例如布局/ list.xml

   <LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
       a:layout_width="fill_parent"
       a:layout_height="fill_parent"
       a:orientation="vertical"
       a:stretchColumns="1">
     <ListView a:id="@+id/paramList" a:layout_width="fill_parent" 
           a:layout_height="fill_parent" />
   </LinearLayout>

在您的活动中,获取列表视图的句柄并向其添加数据。

 // Find the list view component in your layout.
 ListView list = (ListView) findViewById(R.id.paramList);

 // obtain data
 List data = ... //some list data

 // use a list adapter to render your list item in your list view!!
 // The item is rendered using the list_item.xml layout.
 // Here you can have any layout, with image and text as in your pic.
 ListAdapter adapter = new ArrayAdapter(this, R.layout.list_item, data);
 list.setAdapter(adapter);

答案 1 :(得分:0)

您需要实施BaseAdapter。 BaseAdapter定义了一个getView方法,该方法将返回列表项将使用的视图。您可以以图解方式创建视图或从XML加载它。然后在ListActivity中使用setListAdapter方法。

查看示例here

相关问题