zoukankan      html  css  js  c++  java
  • Android SimpleAdapter

    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>


    效果图:


  • 相关阅读:
    mysql获取给定时间段内的所有日期列表
    mysql中的年,月,日统计以及日历表的实现
    MySQL5.7安装配置
    获取ip地址
    Intellij热部署插件JRebel
    IntelliJ IDEA2018版热部署jrebel插件安装使用教程
    idea插件篇之java内存分析工具(JProfiler)
    mysql中的整除,取余
    SIMD.mul (SIMD) – JavaScript 中文开发手册
    Java面试题 : 如何确保N个线程访问N个资源的同时又不导致死锁
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/7098610.html
Copyright © 2011-2022 走看看