Sometimes we need to set up fixed data for a ListView, and below is how to set up static data
Layout file Main page of the 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:orientation="vertical" > <ListViewandroid:id="@+id/listview"android:layout_width="Fill_ Parent "android:layout_height=" Fill_parent " > </ListView></linearlayout>
Then a layout file for each of the 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:orientation =; <imageview android:id
= "@+id/listitem_iv" android:layout_width = "74DP" android:layout_height = "74DP" android:src = "@ Drawable/about_brand "/> <TextViewandroid: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 up static data:
The control class of this interface Listviewuseadapter.java
ImportJava.util.ArrayList;ImportJava.util.HashMap;ImportJava.util.List;ImportJava.util.Map;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.AdapterView;ImportAndroid.widget.Toast;ImportAndroid.widget.AdapterView.OnItemClickListener;ImportAndroid.widget.ListView; Public class listviewuseadapter extends Activity { PrivateListView 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 of the adapter int[] ImageId =New int[] {r.drawable.chat_tool_camera, r.drawable.chat_tool_location, R.drawable.chat_tool_paint, R.drawable.chat_tool_video, R.drawable.chat_tool_voice, R.drawable.about_brand};//Set titleString[] title =NewString[] {"Camera","Positioning","Brushes","Video","Sound","Chat"}; list<map<string, object>> ListItem =NewArraylist<map<string, object>> ();//Convert the above resources into a list collection for(inti =0; i < title.length; i++) {map<string, object> Map =NewHashmap<string, object> (); Map.put ("Image", Imageid[i]); Map.put ("title", Title[i]); Listitem.add (map); } Listviewadapter adapter =NewListviewadapter ( This, ListItem); Listview.setadapter (adapter); Listview.setonitemclicklistener (NewOnitemclicklistener () {@Override Public void Onitemclick(adapterview<?> parent, view view,intPositionLongID) {Toast.maketext (listviewuseadapter. This,"haha", Toast.length_short). Show (); } }); }}
Then you need the adapter as follows:
ImportJava.util.List;ImportJava.util.Map;ImportAndroid.content.Context;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.ImageView;ImportAndroid.widget.TextView; Public class listviewadapter extends baseadapter { PrivateContext context;PrivateList<map<string, object>> ListItem; Public Listviewadapter(context context, list<map<string, object>> ListItem) { This. Context = Context; This. ListItem = ListItem; }@Override Public int GetCount() {returnListitem.size (); }@Override PublicObjectGetItem(intPosition) {returnListitem.get (position); }@Override Public Long Getitemid(intPosition) {returnPosition }@Override PublicViewGetView(intPosition, 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") +"");returnConvertview; }}
The effect is as follows:
How to set up static data for a ListView in Android