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