1.MainActivity.java
public class MainActivity extends Activity { private ListView listView; private SimpleAdapter simp_adapter; private List<Map<String, Object>> dataList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* 1.context:上下文对象 2.data:数据源(List<? extents Map<String,? >> data)一个Map所组成的List集合. 每个Map都相应ListView列表中的一行. 每个Map(键-值对)中的键必须包括全部在from中所指定的键 3.resource:列表项的布局文件ID 4.from:Map中的键名 5.to:绑定数据视图中的ID,与FROM成相应关系 */ listView = (ListView)findViewById(R.id.listView); //1.新建适配器 dataList = new ArrayList<Map<String,Object>>(); //2.适配器载入数据源 simp_adapter = new SimpleAdapter(this, getData(),R.layout.item, new String[] {"image","text"}, new int[]{R.id.image,R.id.text}); //视图载入适配器 listView.setAdapter(simp_adapter); } public List<Map<String, Object>> getData(){ for(int i = 0;i<20;i++){ Map<String, Object> map = new HashMap<String, Object>(); map.put("image", R.drawable.ic_launcher); map.put("text", "Just a Demo."+i); dataList.add(map); } return dataList; } }
2.activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </RelativeLayout>
3.item.xml
<?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="horizontal" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Demo" android:textColor="#000000" android:textSize="20sp" /> </LinearLayout>
效果图:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hhb3NoZW55dXRvdQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" >
时间: 2024-11-13 02:30:51