package test.simpleAdapter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.example.simpleadapters.R; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleAdapter; public class ListContent extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.list_content); //----------------------------------------- //为simpleAdapter构造数据 String []title = {"排山倒海","葵花点穴手","如来神掌"}; String []tips = {"威力巨大给对手造成巨大内伤,即使对手逃逸也会无疾而终","短暂制瘫对方,瞬间废除对手活动能力,即可为所欲为","有效杀伤力达几百米寸草不生,但灰尘太大不环保"}; int []icon = {R.drawable.psdh,R.drawable.khdxs,R.drawable.rlsz}; String []time ={"14:30","14:20","14:10"}; //--------------------------------------- //构造玩上述数据后,开始构造它们xml布局 //--------------------------------------- //将数据打包成MAP对象 List<Map<String,Object>> map_content = new ArrayList<Map<String,Object>>(); for(int i = 0; i < title.length;i++){ Map map = new HashMap<String, Object>(); //打包方式 key value键值对形式 map.put("title", title[i]); map.put("tips", tips[i]); map.put("icon", icon[i]); map.put("time", time[i]); //------------------------------------ map_content.add(map); } //---------------------------------------- //生成xml布局资源ID引用 int []xmlres = {R.id.avatar,R.id.list_title,R.id.list_tips,R.id.list_time}; //生成键素组 String []mapkey = {"icon","title","tips","time"};//与上面的xmlres数组的资源ID对应 //map对象构造完毕,接下来生成simpleAdapter对象 //构造simpleAdapter所需参数说明:context窗口对象,盛装map的list,列表栏目布局资源ID的引用,list中的map中对应的键名称,键名称对应的控件资源ID; SimpleAdapter sAdapter = new SimpleAdapter(ListContent.this, map_content, R.layout.simple_adapter_list, mapkey, xmlres); ListView list = (ListView)findViewById(R.id.main_list); list.setAdapter(sAdapter);//设置资源 } }
xml:
//list_content <?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/main_list" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </LinearLayout>
//simple_adapter <?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" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <ImageView android:id="@+id/avatar" android:layout_width="match_parent" android:layout_height="70dp" android:layout_weight="0.7"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.3" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/list_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.2"/> <TextView android:id="@+id/list_time" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.8"/> </LinearLayout> <TextView android:id="@+id/list_tips" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout> </LinearLayout>