Android listview layout (inheriting listactivity and using arrayadapter)

Source: Internet
Author: User

 

 

Using listview and arrayadapter layout is the simplest of listview layout. First, we will create a component to display data, such as main. xml

 

<? XML version = "1.0" encoding = "UTF-8"?> <! -- The main interface itself is a display component --> <textview xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: padding = "10dp" Android: textsize = "16sp"> </textview>

  

The activity code is as follows:

 

Package cn.com. android. GRID; import android. app. listactivity; import android. OS. bundle; import android. widget. arrayadapter; public class listviewtest extends listactivity {@ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); string data [] = getdata (); arrayadapter <string> arrayadapter = new arrayadapter <string> (this, R. layout. main, data); this. setlistadapter (arrayadapter); // arrayadapter inherits from baseadapter, baseadapter inherits from listadapter}/*** @ author chenzheng_java * @ description to get an array list */private string [] getdata () {string [] DATA = new string [100]; for (INT I = 0; I <100; I ++) {data [I] = "list item" + I;} return data ;}}

If the activity does not want to inherit the listactivity, we can write it like this.

 

Package cn.com. android. GRID; import android. app. activity; import android. OS. bundle; import android. widget. arrayadapter; import android. widget. listview; public class listviewtest2 extends activity {@ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); listview = new listview (this); listview. setadapter (New arrayadapter <string> (this, R. layout. main, getdata (); setcontentview (listview);}/*** @ author chenzheng_java * @ description get an array list */private string [] getdata () {string [] DATA = new string [100]; for (INT I = 0; I <100; I ++) {data [I] = "list item" + I;} return data ;}}

  

Waste of words:

There is a big gap between the two implementation methods, and many beginners may be dizzy. But it doesn't matter. Let's take a look at how listactivity is implemented.

The Code excerpt is as follows:

 

public class ListActivity extends Activity {      /**      * This field should be made private, so it is hidden from the SDK.      * {@hide}      */      protected ListAdapter mAdapter;      /**      * This field should be made private, so it is hidden from the SDK.      * {@hide}      */      protected ListView mList;  

  

We can see that, in fact, when we inherit the listactivity, there is actually a listview inherited from the listactivity, so do not think that the gap is very large, in essence, the implementation steps are the same. You can think that android provides you with a free tool class. Nothing unusual.

The steps are three major steps:

Step 1: Prepare the layout file main. xml

Step 2: Get the data getdata ()

 

Step 3: bind the data source setlistadapter ();

 

 

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.