Custom listivew (android)

Source: Internet
Author: User

Just learned listview, refer to online tutorials

Http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html

Implemented a listview by myself, using SimpleAdapter, slightly changed

The Code is as follows:

 

 

Public class SListViewActivity extends Activity {
/** Called when the activity is first created .*/
Private ListView iLv;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
ILv = (ListView) this. findViewById (R. id. lv );
SimpleAdapter ada = new SimpleAdapter (this, getData (), R. layout. vlist, new String [] {"title", "info", "img "},
New int [] {R. id. title, R.id.info, R. id. img });
ILv. setAdapter (ada );

}
Private List <Map <String, Object> getData ()
{
List <Map <String, Object> list = new ArrayList <Map <String, Object> ();
Map <String, Object> map = new HashMap <String, Object> ();
Map. put ("title", "G1 ");
Map. put ("info", "google ");
Map. put ("img", R. drawable. i1 );
List. add (map );

Map = new HashMap <String, Object> ();
Map. put ("title", "G2 ");
Map. put ("info", "google 2 ");
Map. put ("img", R. drawable. i1 );
List. add (map );

Return list;
}

 

------

Xml Code

Main. 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 = "fill_parent"
Android: orientation = "vertical">

<ListView
Android: id = "@ + id/lv"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
/>

</LinearLayout>

 

Vlist. xml: The layout file of the custom listview.

 

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

<ImageView
Android: id = "@ + id/img"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_margin = "3px"
/>
<LinearLayout
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: orientation = "vertical"
>
<TextView
Android: id = "@ + id/title"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: textColor = "# ffffff"
Android: textSize = "22px"
/>
<TextView
Android: id = "@ + id/info"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: textColor = "# ffffff"
Android: textSize = "12px"
/>
</LinearLayout>
</LinearLayout>

 

After it was just made, there was no way to run it. In the end, it was found that TextView was written incorrectly here. After correct modification, it can be run.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.