SimpleAdapter学习

来源:互联网 发布:wifi自动切换软件 编辑:程序博客网 时间:2024/05/21 18:35
这里我先创建一个Item布局文件<?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"><ImageView    android:id="@+id/image"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_margin="5px"    />    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="vertical"        >        <TextView            android:id="@+id/title"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="22px"            android:text="清风徐来"            />        <TextView            android:id="@+id/info"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="15px"            android:text="水波不兴"/>    </LinearLayout></LinearLayout>然后在Main布局添加ListView<?xml version="1.0" encoding="utf-8"?><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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.user.android2lesson_02_simpleadapter.MainActivity"><ListView    android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/listview"/></RelativeLayout>最后写MainActivitypackage com.user.android2lesson_02_simpleadapter;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.ListView;import android.widget.SimpleAdapter;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;//SimpleAdapter是一种基本版的完整的列表视图public class MainActivity extends AppCompatActivity {//    SimpleAdapter可以进行布局文件定制,从而达到自定义的效果//    SimpleAdapter 步骤1:创建一个布局文件,用来给每一个Item(cell)进行布局//    SimpleAdapter 步骤2:在MainActivity布局文件中添加ListView//    步骤2  在布局文件中添加listview//    SimpleAdapter 步骤3:声明属性并且进行初始化    private ListView mListView;//    ListView在IOS开发里面叫UITableView//    iOS里面没有包的概念,有命名空间    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);//        进行初始化        mListView = (ListView) findViewById(R.id.listview);//        SimpleAdapter 步骤4://        创建一个simple适配器//        参数1:传递了一个上下文对象//        参数2:要显示的数据//        参数3:数据显示的布局文件//        参数4:数据提取时用到的键//        参数5:数据对应的布局文件中的控件id        SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.list_item_layout,new String[]{"image","title","info"},new int[]{R.id.image,R.id.title,R.id.info});//SimpleAdapter 步骤5:将adapter和listview进行绑定        mListView.setAdapter(adapter);    }//    在这个方法里面进行数据的初始化,拼装要显示的数据    private List<Map<String,Object>> getData(){List<Map<String,Object>> list = new ArrayList<>();        Map<String,Object> map = new HashMap<>();        map.put("image",R.drawable.ic_launcher);        map.put("title","杰尼龟①");        map.put("info","杰尼杰尼");        list.add(map);        Map<String,Object> map1 = new HashMap<>();        map1.put("image",R.drawable.ic_launcher);        map1.put("title","杰尼龟②");        map1.put("info","杰尼杰尼");        list.add(map1);        Map<String,Object> map2 = new HashMap<>();        map2.put("image",R.drawable.ic_launcher);        map2.put("title","妙蛙种子");        map2.put("info","种子机关枪");        list.add(map2);        return list;    }}
0 0
原创粉丝点击