简体   繁体   中英

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

替代文字

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM