The use of Baseadapter
1. Create an array resource class Generalbean
Package com.example.hoyin0211.entry;
public class Generalbean {
private int resid;
private String name;
@Override
Public String toString () {
Return "Generalbean [resid=" + Resid + ", name=" + name + "]";
}
Public Generalbean (int resid, String name) {
Super ();
This.resid = Resid;
THIS.name = name;
}
public int Getresid () {
return resid;
}
public void Setresid (int resid) {
This.resid = Resid;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
}
2. Defining a string array resource String-array
<string-array name= "City" >
<item name= "BJ" > Beijing </item>
<item name= "sh" > Shanghai </item>
<item name= "sz" > Shenzhen </item>
<item name= "GZ" > Guangzhou </item>
<item name= "WH" > Wuhan </item>
<item name= "XA" > XI ' an </item>
<item name= "HB" > Giggle </item>
</string-array>
3. Define the list landscape layout (Imageview,textview)
<?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/ivthumb"
android:contentdescription= "Chenyi"
Android:layout_width= "80DP"
android:layout_height= "80DP"
android:src= "@drawable/chenyi"/>
<textview
Android:id= "@+id/tvname"
Android:layout_width= "Wrap_content"
android:layout_height= "80DP"
android:layout_marginleft= "10DP"
android:text= "Chen Yi"
Android:textsize= "20SP"
android:gravity= "Center_vertical"/>
</LinearLayout>
4. Add a ListView to the main layout
<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/mlvtest"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:divider= "#ccc"
android:dividerheight= "2DP"/>
</LinearLayout>
5. Define ListView, Collection, Generaladapter, picture resource array variables
ListView ListView;
List<generalbean> Mgeneralbeans;
Generaladapter Madapter;
Int[] Resid = {r.drawable.zhude,r.drawable ...};
6. Converting a string array from a resource into a string array in Java
private void InitData () {
String[] Names=getresources (). Getstringarray (r.array.city);
Mgenerals = new arraylist<generalbean> ();
for (int i = 0; i < names.length; i++) {
Generalbean bean = new Generalbean (resid[i],names[i]);
Mgenerals.add (Bean);
}
}
7. Create the Baseadapter adapter
Class Generaladapter extends baseadapter{
public int GetCount () {
Retuen mgenerals.size ();
}
Public Generalbean getItem (int position) {
return Mgenerals.get (position);
}
public long getitemid (int position) {
return position;
}
Public View getView (int position,view convertview,viewgroup parent) {
Get ListViewItem layout, convert to object of view type
View layout = view.inflate (mainactivity.this,r.layout.item_general,null);
Find ImageView
ImageView Ivthube = (ImageView) Layout.findviewbyid (R.ID.IVTHUMB);
TextView tvname = (TextView) Layout.findviewbyid (r.id.tvname);
Get a picture of subscript position
Generalbean bean = mgenerals.get (position);
Show pictures
Ivthumb.setimageresource (Bean.getresid ());
Display Name
Tvname.settext (Bean.getname ());
return layout;
}
}
8. Associated Adapter
ListView = (ListView) Findviewbyid (R.id.mlvtest);
Madapter = new Generaladapter ();
Listview.setadapter (Madapter);
An example of Baseadapter usage for Android development, creating a mixed list item of graphic text