Android中SimpleAdapter详解
来源:互联网 发布:淘宝一元起拍靠谱吗 编辑:程序博客网 时间:2024/06/11 19:23
- SimpleAdapter的简单实例
SimpleAdapter是有够Simple的,它只提供了一个构造方法。
SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)data 是要显示的数据,它被要求是一个List,List里的每一项是一个Map,每一个Map里装好了一行的数据。resource 是展示数据的那个layout文件from 这个数组里收集了data里的Map中要显示的那些内容的key。to 这个数组收集了resource里要展示内容的view的id。每一个id都根据数组下标对应着from中的一个key。这样就明确的指定了哪项内容展示在哪个view里。
SimpleAdapter的简单实例
主函数:
public class MainActivity extends AppCompatActivity { private ListView mListView; private SimpleAdapter simpleAdapter; private List<Map<String, Object>> data; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (ListView) findViewById(R.id.listview); //SimpleAdapter(context,data,resource,from,to) /** *context:上下文 * data:数据源(List<? extends Map<String,?>>data)一个Map所组成的List集合 * 每一个Map都会去对应ListView列表中的一行 * 每一个Map(键值-对)中的键必须包含所有在from中指定的键 * resource:列表项的布局id * from:Map中的键名 * to:绑定数据视图中的ID,与from成对应关系 */ data = new ArrayList<>(); simpleAdapter = new SimpleAdapter(this, getData(), R.layout.item, new String[]{"pic", "text"}, new int[]{R.id.imageview, R.id.textview_pic}); mListView.setAdapter(simpleAdapter); } private List<Map<String, Object>> getData() { for (int i = 0; i < 20; i++) { Map<String, Object> map = new HashMap<>(); map.put("pic", R.mipmap.xiao); map.put("text", "身份" + i); data.add(map); } return data; }}
activity_main布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" tools:context=".MainActivity"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content"></ListView></LinearLayout>
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="wrap_content"> <ImageView android:id="@+id/imageview" android:layout_marginLeft="30dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher"/> <TextView android:id="@+id/textview_pic" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="#ff00ff" android:text="头像"/></LinearLayout>
0 0
- Android中SimpleAdapter详解
- Android中SimpleAdapter,BaseAdapte详解
- Android中SimpleAdapter,BaseAdapte详解
- Android中SimpleAdapter,ArrayAdapter和BaseAdapter详解
- Android中SimpleAdapter,ArrayAdapter和BaseAdapter详解
- Android SimpleAdapter源码详解
- Android SimpleAdapter源码详解
- android-SimpleAdapter详解
- android的ListView中SimpleAdapter
- Android SimpleAdapter 的详解和使用
- simpleAdapter详解
- simpleAdapter详解
- SimpleAdapter 详解
- Android 中 ListAactivity 和Activity 使用ListView 以及ArrayAdapter和SimpleAdapter参数详解
- Android中SimpleAdapter,ArrayAdapter和BaseAdapter小记
- Android中ListView之SimpleAdapter的使用
- Android中ArrayAdapter、SimpleAdapter、BaseAdapter总结
- android中SimpleAdapter的编程实例
- ocp-138
- ocp-139
- HDU 2295 Radar (DLX求重复覆盖, A*搜索)
- 文章标题
- 关于软件测试的个人思考
- Android中SimpleAdapter详解
- 欢迎使用CSDN-markdown编辑器
- 链表创建、反转
- Sublime快捷键
- poj1930
- 网络编程(5)——HttpURLConnection/HttpClient的区别
- 在ubuntu下对stm32开发板的编译、烧写、调试
- linux md5sum与sha1sum
- 给ubuntu的firefox添加“百度搜索”