Android List Component ListView
A list component is a component that is often used in development and is used to provide an adapter for it, provided by the adapter to determine the display style and display data.
Let's look at an example:
Create a new project lesson8_listviewtest,activity name is mainlistviewtest.
The code for Mainlistviewtest.java is:
Package android.basic.lesson8;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemClickListener;
Import Android.widget.AdapterView.OnItemLongClickListener;
Import Android.widget.ArrayAdapter;
Import Android.widget.ListView;
Import Android.widget.TextView;
Import Android.widget.Toast; The public class Mainlistviewtest extends activity {/** called the ' when ' is the ' The activity ' is a-a-created
IC void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Find ListView ListView lv= (ListView) Findviewbyid (R.ID.LISTVIEW01); Define array string[] Data ={"Android Development Basics", "the second of Android development Basics", "the third of Android development Basics" , "Android Development Basics", "The Android Development Basics", "The sixth of Android development Basics", "Android Development Basics The seventh lecture, "The Android Development Basics Chapter Eighth",
"Android Development Basic article Nineth"}; Provides the array adapter Lv.setadapter (new Arrayadapter (this,android) for ListView.
R.layout.simple_list_item_1,data)); Set list items for ListView Click Listener Lv.setonitemclicklistener (New Onitemclicklistener () {@Override Pub
LIC void Onitemclick (adapterview<?> parent, view view, int position, long ID) { Toast.maketext (Getapplicationcontext (), "you clicked:" + (TextView) view). GetText (), Toast.length_short).
Show ();
}
});
Set long press listener Lv.setonitemlongclicklistener for ListView (New Onitemlongclicklistener () {@Override
public Boolean Onitemlongclick (adapterview<?> parent, view view, int position, long ID) { Toast.maketext (Getapplicationcontext (), "You Are long pressed:" + (TextView) view). GetText (), Toast.len
Gth_short). Show ();
return false;
} }); }
}
which
1, Android. R.layout.simple_list_item_1 is a ListView layout that is built into the Android system; we can customize the way ListView is arranged.
2, Onitemclicklistener is an interface, the use of new to create an interface is not a bit strange? In fact, an anonymous class was created to satisfy the parameter requirements of the Setonitemclicklistener () method.
3, Toast.maketext (). Show () The way you can remember, you can create a bubble hint effect; Toast's specific usage can be seen here.
The code for Main.xml is:
< XML version= "1.0" encoding= "Utf-8"?>
Okay, now look at the effect:
Clicking on one of the list items triggers the item's OnClick event, pops up a bubble tip box, and the long press on one option triggers the Longclick event, which pops up another bubble balloon.
The example above uses Android, which is brought by the system itself. R.layout.simple_list_item_1 layout, using an array in Java code to do the data source; We use XML as a data source in the next example, and then use the system's own Android. R.layout.simple_list_item_2 layout, let's see what their performance is different.
The above is the introduction of Android ListView, follow-up to continue to supplement the relevant knowledge, thank you for your support for this site!