Container's ListView 1

Source: Internet
Author: User

A ListView is a list of containers that can contain multiple items of data, which can be simply a textview layout or a complex composite layout.

The inheritance relationship is as follows:

Class ListView
Java.lang.Object  android.view.View      android.view.ViewGroup          android.widget.adapterview< listadapter>              android.widget.AbsListView                  android.widget.ListView
There is also a description in the API documentation:


A ListView is a list of views that can be scrolled vertically, with data items coming from ListAdapter that are bound to this view. Then there is a special case where the ListView may not be available, that is, under ScrollView. I accidentally found, the author for the lazy to widgets all the experiments were put in a project in the top floor with a scrollview, in this case add ListView can only display the first data.

One, the simplest of the ListView

Using a method similar to spinner, you need a data source that is the simplest of arrays, a adapter, and then binds the ListView and this adapter through the ListView's Setadapter () method, so that the simplest ListView succeeds.

                ListView lv_01 = (ListView) Findviewbyid (R.ID.LV_LISTVIEW01); String []itmes = new string[]{"Carefree", "Ling Son", "Month like"}; Arrayadapter <string>aadapter = new Arrayadapter<string> (this,android. R.layout.simple_list_item_1,itmes); Lv_01.setadapter (Aadapter);
Operation Result:

Second, custom ListView

Let's take a look at the Setadapter () method of the ListView in the API to explain:

Setadapter (ListAdapter adapter)
This method sets the data for the ListView, which passes the data, which may be assembled by an assembly adapter, depending on the current ListView feature, such as adding a page or footer.


With this explanation we can find that the ListView style is set by adapter, and the following continues to look at the definition of adapter:

Extended that's the Adapter bridge between A and the ListView data that backs the list. Frequently that the data comes from a Cursor, which is not required. The ListView can display any data provided this it is wrapped in a listadapter.

The extended adapter is a bridge between data and views, usually data from a cursor but is usually not required, and the ListView can render any data provided to it, which is assembled by ListAdapter.

Here we know: the data source + Adapter + layout will be able to get a custom ListView.

Data Source: Array or list<map>

Adapter:simpleadapter Extensibility is good, you can put a variety of other controls to combine.

Layout: Layout.xml

About Simpleadapter Parameters I believe many beginners and the author of the same mess, because the parameters too much, but also difficult to remember, first look at the official API explanation:

Simpleadapter
Simpleadapter (Context context,                     list<? Extends map<string,?>> data,                     int resource,                     string[] from,                     int[] to)
Parameters:
context -The context where the View associated
with this simpleadapter is running
data -A List of Maps. Each entry in the list corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in ' from '
resource -Resource identifier of a view layout that defines the "the" to this list item. The layout file should include at least those named views defined in ' to '
from -A List of column names
that would be added to the MAP associated with each item.
to-The should display column in the ' from ' parameter. These should all is textviews. The first n views in this list is given the values of the first n columns in the From Parameter1.context good understanding of the current context.

2.data, first it is a list containing a map of a set (the contents of a list must be a certain map), the back of a bit around, the list of each item (should be the following from this parameter) and list of the same line (no language), Each row in this map contains data that should contain every item in the "from". It's a bit crazy to look at the text, or just look at the example code:

Simpleadapter sa = new Simpleadapter (this, GetData (), R.layout.item_listview, new string[]{"icon", "Content", "Content1" }, new Int[]{r.id.iv_icon,r.id.tv_content,r.id.tv_content1});

Private list<map<string,object>> GetData () {list<map<string,object>> myList = new ArrayList <Map<String,Object>> (); map<string,object> map = new hashmap<string, object> () map.put ("icon", R.drawable.ic_launcher); Map.put (" Content "," Xiaoyao "); Map.put (" Content1 "," actor "); Mylist.add (map); map = new hashmap<string, object> (); Map.put (" Icon ", r.drawable.ic_launcher); Map.put (" Content "," linger "); Map.put (" Content1 "," Heroine "); Mylist.add (map); map = new Hashmap<string, Object> (), Map.put ("icon", R.drawable.ic_launcher), Map.put ("Content", "month as"); Map.put (" Content1 "," Heroine "); Mylist.add (map); map = new hashmap<string, object> (); Map.put (" icon ", R.drawable.ic_launcher); Map.put ("Content", "Tangyu"), Map.put ("Content1", "actor"); Mylist.add (map); return myList;}

Comparison: It should be said that the from parameter should be the key of the map. The author is so understanding, if not, welcome to point out.

3. This parameter defines a layout to describe the style of each item in the view, and the layout should contain at least the views defined by the parameters in to (that is, the ID)

4. This view should show the values corresponding to the columns in the from parameter, they must all be textview and the order of display is determined by the order of the columns in to.

The general meaning is that the ID in Parameter 3 (which describes the layout of each item style) should include at least 4 of the IDs listed, and in the actual rendering order is the order of the column.

The more complete code is as follows: Mainactivity, since Listactivity is inherited, it is not necessary to define the ListView in XML.

<pre name= "code" class= "Java" >public class Mainactivity extends listactivity {@Overrideprotected void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Simpleadapter sa = new Simpleadapter (this, GetData (), R.layout.item_listview, new string[]{"icon", "Content", "Content1" }, new Int[]{r.id.iv_icon,r.id.tv_content,r.id.tv_content1}); This.setlistadapter (SA);} @Overrideprotected void Onlistitemclick (ListView l, View v, int position, long ID) {//TODO auto-generated method Stublog. I ("abc", GetData (). get (position). ToString ());} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} Private list<map<string,object>> GetData () {list<map<string,object>> myList = new ArrayList <Map<String,Object>> (); map<string,object> map = new hashmap<string, object> () map.put ("icon", R.drawable.ic_launcher); Map.put ("Content", "Xiaoyao"), Map.put ("Content1", "actor"), Mylist.add (map), map = new hashmap<string, object> (); Map.put (" Icon ", r.drawable.ic_launcher); Map.put (" Content "," linger "); Map.put (" Content1 "," Heroine "); Mylist.add (map); map = new Hashmap<string, Object> (), Map.put ("icon", R.drawable.ic_launcher), Map.put ("Content", "month as"); Map.put (" Content1 "," Heroine "); Mylist.add (map); map = new hashmap<string, object> (); Map.put (" icon ", R.drawable.ic_launcher); Map.put ("Content", "Tangyu"), Map.put ("Content1", "actor"); Mylist.add (map); return myList;}}


Item_listview.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "Horizontal" >        <imageview         android:id= "@+id/iv_icon"        android:layout_width= "0DP"        android:layout_height= "match_parent"        android:layout_weight= "1"        />        <textview         Android:id= "@+id/tv_content"        android:layout_width= "0DP"        android:layout_weight= "1"        android: layout_height= "Match_parent"        />       <textview         android:id= "@+id/tv_content1"        android: Layout_width= "0DP"        android:layout_weight= "1"        android:layout_height= "match_parent"        />    </LinearLayout>

Operation Result:



Since this is directly inherited from Listactivity, you can directly get the value of the Click event by overriding the Onlistitemclick () callback method directly.

Container's ListView 1

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.