Sometimes we need to set a fixed data for a ListView, the bottom is how to set the static data, before we give you a look at the effect of the picture:
Main page of layout file listview
<?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:o" rientation= "vertical" >
<listview
android:id= "@+id/listview" android:layout_width= "Fill_"
Parent "
android:layout_height=" fill_parent ">
</ListView>
</LinearLayout>
Then a layout file for each ListView Item,listview_item.xml has pictures and text
<?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:o" rientation= "Horizontal" >
<imageview
android:id= "@+id/listitem_iv"
android:layout_width= "74DP "
android:layout_height=" 74DP "
android:src=" @drawable/about_brand "/>
<textview
android : id= "@+id/listitem_tv"
android:layout_width= "match_parent"
android:layout_height= "74DP"
Android : text= "TextView"
android:textalignment= "center"
android:textsize= "55DP"/>
</linearlayout >
Then the key is how to set the static data:
The control class of this interface Listviewuseadapter.java
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.AdapterView;
Import Android.widget.Toast;
Import Android.widget.AdapterView.OnItemClickListener;
Import Android.widget.ListView;
public class Listviewuseadapter extends activity {private ListView ListView;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (r.layout.listview_test);
ListView = (ListView) This.findviewbyid (R.id.listview); Set the image resource for the adapter int[] imageID = new int[] {r.drawable.chat_tool_camera, r.drawable.chat_tool_location, r.drawab
Le.chat_tool_paint, R.drawable.chat_tool_video, R.drawable.chat_tool_voice, R.drawable.about_brand};
Set Title string[] title = new String[] {"Camera", "position", "brush", "video", "Sound", "chat"}; List<map<string, Object>>
ListItem = new arraylist<map<string, object>> (); Converts the above resources to the list collection for (int i = 0; i < title.length i++) {map<string, object> Map = new Hashmap<str
ING, object> ();
Map.put ("image", imageid[i]);
Map.put ("title", Title[i]);
Listitem.add (map);
} listviewadapter adapter = new Listviewadapter (this, listitem);
Listview.setadapter (adapter); Listview.setonitemclicklistener (New Onitemclicklistener () {@Override public void Onitemclick (adapterview< ?> Parent, view view, int position, long id) {Toast.maketext (listviewuseadapter.this, "haha", Toast.)
Length_short). Show ();
}
});
}
}
Then the required adapters are as follows:
Import java.util.List;
Import Java.util.Map;
Import Android.content.Context;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.ImageView;
Import Android.widget.TextView;
public class Listviewadapter extends Baseadapter {private context context;
Private list<map<string, object>> ListItem;
Public Listviewadapter (context, list<map<string, object>> listitem) {this.context = context;
This.listitem = ListItem;
@Override public int GetCount () {return listitem.size ();
@Override public Object getitem (int position) {return listitem.get (position);
@Override public long getitemid (int position) {return position;
@Override public View getview (int position, View Convertview, ViewGroup parent) {if (Convertview = null) { Convertview = Layoutinflater.from (context). Inflate (r.layout.listview_itEM, NULL);
} ImageView ImageView = (imageview) Convertview.findviewbyid (R.ID.LISTITEM_IV);
TextView TextView = (TextView) Convertview.findviewbyid (R.ID.LISTITEM_TV);
map<string, object> map = listitem.get (position);
Imageview.setimageresource ((Integer) map.get ("image");
Textview.settext (Map.get ("title") + "");
return convertview;
}
}
Hopefully this article will help you learn about Android software programming.