Getting started with Android (Interface Programming # 3-listview)

Source: Internet
Author: User
In Android, the use of listview is more complex, that is, to configure its Adapter. There are several kinds of adapters, including arrayadapter and simpleadapter. First, you must generate a listview (of course, you can use listactivity, this activity integrates the listview) and uses the adapter to set the display data and layout of the listview, and then responds to the onitemclick event, or rewrite the onlistitemclick RESPONSE event function in the listactivity. The following code demonstrates the use of listactivity:

Import java. util. arraylist;
Import java. util. hashmap;
Import java. util. Map;
Import Android. App. listactivity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. widget. listview;
Import Android. widget. simpleadapter;

Public class test extends listactivity {
@ Override
Protected void onlistitemclick (listview L, view V, int position, long ID ){
// Todo auto-generated method stub
Super. onlistitemclick (L, V, position, ID );
This. settitle (this. mmodeldata. Get (position). Get ("type"). tostring ());
}
Simpleadapter adapter = NULL;
Private arraylist <Map <string, Object> mmodeldata = NULL;
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Initmodeldata ();
Adapter = new simpleadapter (this, mmodeldata, android. r. layout. two_line_list_item, new string [] {"name"}, new int [] {android. r. id. text1 });
This. setlistadapter (adapter );
// Setcontentview (R. layout. Main );
}
Public void initmodeldata ()
{
Mmodeldata = new arraylist <Map <string, Object> ();
Map <string, Object> item = new hashmap <string, Object> ();
Item. Put ("name", "Linux"); item. Put ("type", "OS ");
Mmodeldata. Add (item );
Map <string, Object> item2 = new hashmap <string, Object> ();
Item2.put ("name", "android"); item2.put ("type", "Platform ");
Mmodeldata. Add (item2 );
Map <string, Object> item3 = new hashmap <string, Object> ();
Item3.put ("name", "tomato"); item3.put ("type", "Fruit ");
Mmodeldata. Add (item3 );

}
}

The following code shows how to use listview + activity:

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. widget. adapterview;
Import Android. widget. arrayadapter;
Import Android. widget. listview;
Import Android. widget. adapterview. onitemclicklistener;

Public class teststringlist extends activity implements onitemclicklistener {
@ Override
Public void onitemclick (adapterview <?> Arg0, view arg1, int arg2, long arg3 ){
This. settitle ("You click item:" + String. valueof (arg2 ));
}
Private listview mlistview = NULL;
@ Override
Protected void oncreate (bundle savedinstancestate ){
// Todo auto-generated method stub
Super. oncreate (savedinstancestate );
Mlistview = new listview (this );
Mlistview. setonitemclicklistener (this );
Arrayadapter <string> adapter = new arrayadapter <string> (this, Android. R. layout. simple_list_item_checked,
New String [] {"itema", "itemb", "itemc "});
Mlistview. setadapter (adapter );
This. setcontentview (mlistview );
}

}

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.