Android's ListView

Source: Internet
Author: User

ListView Fill Data with Setadapter method, must inherit the abstract class ListAdapter, this class to implement more methods, Google engineers to simplify the operation, just implement Baseadapter can.
The main implementation of two methods: GetCount () method and GetView (int position, View arg1, ViewGroup arg2) method;
GetCount returns the number of bars, that is, the ListView displays several settings here, GetView () returns content.
Example:
Layout file
<?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:orientation= "Vertical" >
<listview android:id= "@+id/listviewtest"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content" >
</ListView>
</LinearLayout>
Java code: Populating the ListView with data
public class Mainactivity extends Activity {
ListView listviewtest;
string [] datas=new string []{"Ginger Small A", "a Zhong", "Huangmiao"};
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Listviewtest = (ListView) Findviewbyid (r.id.listviewtest);
Listviewtest.setadapter (New Myadapter ());
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}

Private class Myadapter extends baseadapter{

@Override
public int GetCount () {
TODO auto-generated method stubs
return datas.length;
}

@Override
Public Object getItem (int arg0) {
TODO auto-generated method stubs
return null;
}

@Override
public long getitemid (int arg0) {
TODO auto-generated method stubs
return 0;
}

@Override
Public View GetView (int position, view arg1, ViewGroup arg2) {
TextView TV =new TextView (Getapplicationcontext ());
Tv.settext (Datas[position]);
Tv.settextsize (72);
Tv.settextcolor (color.red);
return TV;
}

}
}

Android's ListView

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.