How to use the ListView for Android

Source: Internet
Author: User

In the Internet of things learning I need to use Android to get the table data in the PC-side database and display it on the screen, so I decided to use WebService to connect to the database to get the data and display the data with the ListView component. I will not show you how to get the data when I save the data in the list generic.

A ListView is a common component of Android that displays data in a list and adapts itself to content. The ListView component does not load all the data at once, but rather how many rows of data the screen displays. However, when you drag down, the new row of data is loaded and the memory is recycled when the first line of the screen is moved out of the screen. So don't worry about using a ListView when the amount of data is large.

The ListView uses the Setadapter () method to bind the data adapter, and here I choose to use a class to inherit the Baseadapter,baseadapter class there are four methods that must be implemented, GetCount (), GetView (), GetItem ( ), Getitemid (). Among them, the important methods are GetCount () and GetView ()

 Public classMyadapterextendsbaseadapter{@Override Public intGetCount () {return NULL;//Total row count of returned data} @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {        return NULL;//returns a row of data} @Override PublicObject GetItem (intposition) {        return NULL; } @Override Public LongGetitemid (intposition) {        return0; }}    

A getview (int position, View Convertview, ViewGroup Parent) method is called whenever a new piece of data is loaded, where position is the line number of the data that will be loaded.

In order to make the data display better, we can create a new style sheet under the Res/layout folder first.

  

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Horizontal" >                     <TextViewAndroid:id= "@+id/id"android:gravity= "Center"Android:text= "ID"Android:background= "#22000000"Android:layout_width= "0DP"Android:layout_weight= "1"Android:layout_height= "Wrap_content"/> <!--style for each column -          <ViewAndroid:layout_width= "1DP"Android:background= "#000000"Android:layout_height= "Wrap_content"/> <!--the vertical line after each column -         <TextViewAndroid:id= "@+id/name"android:gravity= "Center"Android:background= "#22000000"Android:layout_width= "0DP"Android:layout_weight= "1"Android:layout_height= "Wrap_content"Android:text= "NAME"/>          <ViewAndroid:layout_width= "1DP"Android:background= "#000000"Android:layout_height= "Wrap_content"/>          <TextViewAndroid:id= "@+id/password"android:gravity= "Center"Android:background= "#22000000"Android:layout_width= "0DP"Android:layout_weight= "1"Android:layout_height= "Wrap_content"Android:text= "Pasword"/></LinearLayout>

After defining the view style for each row we go back to the GetView () Chinese law

 PublicView GetView (intposition, View Convertview, ViewGroup parent) {View View=view.inflate (nextactivity. This, R.layout.list_resoure,NULL);//r.layout.list_resoure for the style you just definedTextView tv_id =(TextView) View.findviewbyid (r.id.id); TextView Tv_name=(TextView) View.findviewbyid (r.id.name); TextView TV_PSD= (TextView) View.findviewbyid (R.id.password);//get to the control in List_resoureTv_id.settext (userinfo.get (position). GetId ());    Tv_name.settext (UserInfo.get (position). GetName ()); Tv_psd.settext (UserInfo.get (position). GetPassword ());//assign a value to a control in List_resoure, userinfo the list generic for all data to be displayed    returnView//returns this row of data displayed on the screen}

Don't forget to return the total number of rows in the GetCount () method

@Override  Public int GetCount () {    return userinfo.size ();  the return value is type int, which represents the total number of rows of data }

The GetItem () and Getitemid () methods can be ignored.

Finally, we use the setadapter of the ListView to bind our defined data adapter Myadpter.

ListView LV = (listview) Findviewbyid (R.id.listview); Lv.setadapter (new myadapter ());

  

How to use the ListView for Android

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.