开发者

How to make List view for items in Android?

Please help me out here. I need to make a list view as shown in the sample pic for my android app. Ho开发者_开发知识库w to do it? Also I need to add image along it as seen in the sample pic. and link it to another view to show some more details.please check links shown below.

How to make List view for items in Android?

pic1


First create a layout for your list item in layout/list_item.xml (or any name). I've shown only text field for the item, but you can change the view below to include both image and a label.

  <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>

Define a listView in another layout e.g. layout/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>

In your activity get the handle to list view and add data to it.

 // 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);


You need to implement a BaseAdapter. BaseAdapter defines a getView method, which will return the view that will be used by the list items. You can create the view diagrammatically or load it from XML. Then in your ListActivity you use the setListAdapter method.

See an example here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜