The ListView of Android data display

Source: Internet
Author: User

in Android applications, it is often necessary to use the ListView to display data, a ListView typically populate the layout file with data that you need to know during the fill connect the data with the ListView adapter. the adapter is a bridge linking data and Adapterview (a ListView is a typical adapterview) that effectively separates the data from the Adapterview settings, Make Adapterview and data binding easier and easier to modify. Common adapters are available in three types of Arrayadapter,simpleadapter and Baseadapter.

Arrayadapter Implementing a ListView

A simple ListView data binding can be implemented with Arrayadapter. By default, Arrayadapter binds the ToString value of each object to the pre-defined TextView control in layout. The use of Arrayadapter is very simple.

Add a listview to the layout file

    <listview        android:id= "@+id/list_test"        android:layout_width= "match_parent"        android:layout_height = "Wrap_content"/>

The implementation of code in activity, get the ListView nothing to say, mainly  Setadapter method, the second parameter is the built-in layout file on Android, if it's Android. R.layout.simple_list_item_1, is directly a line of text, I only list three, others can self-test:

String[] listarrstrings=new string[]{"Monday", "Tuesday", "Wednesday"};listview= (ListView) Findviewbyid (r.id.list_test); Listview.setadapter (New arrayadapter<string> (this,android. R.layout.simple_list_item_single_choice, listarrstrings)); Listview.setchoicemode (ListView.CHOICE_MODE_SINGLE); Listview.setonitemclicklistener (New Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> Parent, View view,int position, long ID) {//TODO auto-generated method stubstring namestring= (String) Parent.getitematpos Ition (position); Settitle ("Today is:" +namestring);

The implementation results are as follows:

Change the code, change the default system layout file, and you can set a single or multiple selection:

Listview.setadapter (New arrayadapter<string> (this,android. r.layout.simple_list_item_checked, listarrstrings)); Listview.setchoicemode (listview.choice_mode_multiple); Listview.setadapter (New arrayadapter<string> (this,android. r.layout.simple_list_item_checked, listarrstrings)); Listview.setchoicemode (Listview.choice_mode_single);

The effect is as follows:

Simpleadapter Implementing a ListView

Let's take a look at the effect we're going to achieve:

each view layout in the ListView is the same, this time you need to create a new layout file simpleitem. 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=" wrap_content "android:orientation=" Horizontal "> <imageview android:id=" @+id/itemimage "android:layout_width=" Wrap_content "android:layou t_height= "Match_parent" android:src= "@drawable/ic_launcher"/> <linearlayout android:layout_width= " Match_parent "android:layout_height=" wrap_content "android:orientation=" vertical "Android:paddinglef            t= "10DP" > <textview android:id= "@+id/itemtitle" android:layout_width= "Match_parent"        android:layout_height= "Wrap_content" android:text= "title" android:textsize= "25sp"/> <textview android:id= "@+id/itemtext" android:layout_width= "Match_parent" android:layou            t_height= "Wrap_content"android:text= "xxxxxxxxx"/> </LinearLayout></LinearLayout> 

 This time the code call is also very simple, similar to Arrayadapter, Simpleadapter, the last two parameters, one is the key in the map, a corresponding simpleitem in the ID:

ListView listview= (ListView) Findviewbyid (R.id.list_simple); Arraylist
Baseadapter Implementing a ListView

Baseadapter extensibility, can be added to the view of some of their own definition of operation events, when used to implement the Baseadapter class, define a class of their own;

Class Mybaseadapter extends Baseadapter {//define the length of the list @overridepublic int GetCount () {//TODO auto-generated method Stubret Urn 100;} @Overridepublic Object getItem (int position) {//TODO auto-generated method Stubreturn null;} @Overridepublic long Getitemid (int position) {//TODO auto-generated method Stubreturn 30;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {//TODO auto-generated method stub Textvie W TextView = new TextView (baseactivity.this); Textview.settext ("test"); Textview.settextcolor (Color.Blue); Textview.settextsize (20); return TextView;}}

 Call code in activity:

ListView ListView = (ListView) Findviewbyid (R.id.list_base); Listview.setadapter (new Mybaseadapter ());

  

The above actually uses Baseadapter realizes the Arrayadapter function, the same can use the Baseadapter realizes the Simpleadapter function:

Or the same call Simplte this layout, rewrite the GetView () method:

View view = null;if (Convertview = = null) {Layoutinflater inflater = BaseActivity.this.getLayoutInflater (); view = Inflater . Inflate (R.layout.simpleitem, null);} Else{view=convertview;} TextView tvtitletextview= (TextView) View.findviewbyid (r.id.itemtitle); TextView tvtextview= (TextView) View.findviewbyid (R.id.itemtext); Tvtitletextview.settextsize (20); Tvtitletextview.settext ("title---" +position); Tvtitletextview.settextcolor (color.red); tvtextview.settextsize (15); Tvtextview.settext ("content----" +position); return view;

 The effect is as follows:

11 o'clock, it's time to go to bed, programmers still don't stay up late is better ~

The ListView of Android data display

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.