Common usage of Android ListView (iii)

Source: Internet
Author: User

Simpleadapter

Simpleadapter is the best extensibility, you can define a variety of layouts, you can put on imageview (picture), you can also put a button (button), CheckBox (check box) and so on. The following code directly inherits the listactivity,listactivity and the normal activity is not much different, the difference is to display the ListView did a lot of optimization, the aspect shows just.

The following program implements a class table with a picture.

First you need to define the XML that good one uses to display the contents of each column

Vlist.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android=http://schemas.android.com/apk/res/ Android android:orientation= "Horizontal"
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 oid:layout_margin= "5px"/>
<linearlayout
android:orientation= "vertical" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" > <textview
Android:id= "@+id/title" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:textcolor= "#FFFFFFFF" android:textsize= "22px"/> <textview
Android:id= "@+id/info" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:textcolor= "#FFFFFFFF" android:textsize= "13px"/> </LinearLayout>
</LinearLayout>

Here is the implementation code:

public class MYLISTVIEW3 extends Listactivity {//private list<string> data = new arraylist<string> ();        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Simpleadapter adapter = new Simpleadapter (This,getdata (), r.layout.vlist, new string[]{"title", "Info", "IMG"} , new int[]{r.id.title,r.id.info,r.id.img});
Setlistadapter (adapter); } private List<map<string, object>> GetData () {list<map<string, object>> List = new ARR Aylist<map<string, object>> (); map<string, object> map = new hashmap<string, object> (); Map.put ("title", "G1"); Map.put ("info", "Google 1"); 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.I2); List.add (map); Map = new hashmap<string, object> (); Map.put ("title", "G3"); Map.put ("info", "Google 3"); Map.put ("img", R.DRAWABLE.I3); List.add (map);
return list; }}

  

Each section of the list,list that uses Simpleadapter data is composed of generally hashmap corresponding to each row of the ListView. Each key-value data for the HashMap is mapped to the corresponding ID component in the layout file. Since the system does not have a corresponding layout file available, we can define a layout vlist.xml ourselves. To do the following, the new one simpleadapter parameter is: this, the layout file (vlist.xml), HashMap title and info,img. The component of the layout file is id,title,info,img. Each component of the layout file is mapped to each element of the HashMap, and the adaptation is done.

Operating effects such as:

ListView with Buttons

But sometimes the list is not only used for display purposes, but we can also add buttons to it. Add a button first to write an XML file with a button, and then naturally think of the above method to define an adapter, and then map the data to the layout file. But this is not the case, because the button is not mapped, even if you successfully use the layout file to show the button can not add the response of the button, then you need to study how the ListView is realistic, and you must rewrite a class to inherit baseadapter. The following example displays a button and a picture with two lines of words if the button is clicked, the row of the button will be deleted. And tell you exactly how the ListView works. The effect is as follows:

Vlist2.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android=http://schemas.android.com/apk/res/ Android android:orientation= "Horizontal" android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" &     Gt <imageview android:id= "@+id/img" android:layout_width= "wrap_content" android:layout_height= "wrap_content "android:layout_margin=" 5px "/> <linearlayout
android:orientation= "vertical" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" > <textview
Android:id= "@+id/title" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:textcolor= "#FFFFFFFF" android:textsize= "22px"/> <textview
Android:id= "@+id/info" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:textcolor= "#FFFFFFFF" android:textsize= "13px"/> </LinearLayout> <button
Android:id= "@+id/view_btn" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" a ndroid:text= "@string/s_view_btn" android:layout_gravity= "Bottom|right"/></linearlayout>

Program code:

public class MyListView4 extends Listactivity {private list<map<string, object>> mdata;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Mdata = GetData (); Myadapter adapter = new Myadapter (this); Setlistadapter (adapter); } private List<map<string, object>> GetData () {list<map<string, object>> List = new Ar Raylist<map<string, object>> (); map<string, object> map = new hashmap<string, object> (); Map.put ("title", "G1"); Map.put ("info", "Google 1"); 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.I2); List.add (map); Map = new hashmap<string, object> (); Map.put ("title", "G3"); Map.put ("info", "Google 3"); Map.put ("img", R.DRAWABLE.I3); List.add (map); return list; }//The logical @Ov of a selected item in the ListViewErride protected void Onlistitemclick (ListView l, View v, int position, long id) {LOG.V ("Mylistview4-click", (S Tring) Mdata.get (position). Get ("title")); }
/** * Click the button in the ListView popup dialog box */public void Showinfo () {new Alertdialog.builder (this). Settitle ("My Li Stview "). Setmessage (" Introduction ... "). Setpositivebutton (" OK ", new Dialoginterface.onclicklistener () {@O Verride public void OnClick (Dialoginterface dialog, int which) {}}). Show ();
Public final class viewholder{public ImageView img; Public TextView title; public TextView info; Public Button viewbtn; } public class Myadapter extends baseadapter{private layoutinflater minflater; Public Myadapter (Context context) {This.minflater = Layoutinflater.from (context); } @Override public int getcount () {//TODO auto-generated method stub return Mdata.si Ze (); } @Override public Object getItem (int arg0) {//TODO auto-generated method stub retur n null; } @Override public long getitemid (int arg0) {//TODO auto-generated method stub retur n 0; }
@Override public View getView (int position, view Convertview, ViewGroup parent) {Viewholder hol Der = null; if (Convertview = = null) {holder=new viewholder (); Convertview = minflater.inflate (r.layout.vlist2, NULL); Holder.img = (ImageView) Convertview.findviewbyid (r.id.img); Holder.title = (TextView) Convertview.findviewbyid (r.id.title); Holder.info = (TextView) Convertview.findviewbyid (r.id.info); HOLDER.VIEWBTN = (Button) Convertview.findviewbyid (R.ID.VIEW_BTN); Convertview.settag (holder); }else {holder = (Viewholder) convertview.gettag (); } holder.img.setBackgroundResource ((Integer) mdata.get (position). Get ("img")); Holder.title.setText (String) mdata.get (position). Get ("title")); Holder.info.setText (String) mdata.get (position). Get ("info")); Holder.viewBtn.setOnClickListener (New View.onclicklistener () {@Override public void on Click (View v) {showinfo (); }}); return convertview; } }}

The following will be a detailed explanation of the above code, the ListView at the beginning of the drawing, the system first called the GetCount () function, according to his return is worth to the length of the ListView (This is why the first figure in the beginning of the special list length), and then according to this length, Call GetView () to draw each row one by one. If your GetCount () return value is 0, the list will not show the same return 1, and only one row will be displayed.

When the system displays the list, first instantiate an adapter (this will instantiate the custom adapter). When you manually complete the adaptation, you must manually map the data, which requires overriding the GetView () method. This method is called when the system draws each row of the list. GetView () has three parameters, position indicates that the first line will be displayed, Covertview is the layout inflate from the layout file. We use the Layoutinflater method to extract the well-defined Vlist2.xml file into a view instance for display. The individual components in the XML file are then instantiated (the Simple Findviewbyid () method). This allows the data to be mapped to individual components. However, in order to respond to a click event, the button needs to be added a click Listener to capture the click event. Now that a custom ListView has been completed, let's go back and look at the process from a new perspective. System to draw the ListView, he first obtains the length of the list to be drawn, and then begins to draw the first line, how to draw it? Call the GetView () function. In this function, we first get a view (actually a viewgroup) and then instantiate and set up each component to show it. Well, finish drawing this line. Then draw the next line until the painting is finished. During the actual run, you will find that each row in the ListView has no focus, because the button robs the ListView of the focus, as long as the button is set to no focus in the layout file.

Operating effects such as:

Common usage of Android ListView (iii)

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.