安卓学习笔记之SimpleAdapter
来源:互联网 发布:英国情报机构 大数据 编辑:程序博客网 时间:2024/05/17 06:25
今天学习了一个适配器. 让我们看一下这个适配器的构造方法
SimpleAdapter(Context context, List <? extends Map <String, ?>> data, int resource, String[] from, int[] to)
这里我把simpleAdapter用于ListView
那么context 就代表ListView 这个类了.
List 代表的是需要显示的数据.
resource 代表显示需要的布局文件.
from 为data中map对应的键值
to 为需要显示的组件的id.
下面这段是转载的.
一个SimlpleAdapter是这个工作的。假设将SimpleAdapter用于ListView。那么ListView的每一个列表项就是resource参数值指定的布局。而data参数就是要加载到ListView中的数据。我们先看每一个列表项,假设列表项所对应的布局文件中包含了两个组件:TextView和EditText,id分别为textview和edittext。那么在加载列表项时,需要通过组件的id和data参数中List元素中的Map对象对应。因此,from参数Map对象的key,而to表示组件的id,例如,本例中的参数值为from = new String[]{"textview", "edittext"},to = new int[]{R.id.textview,R.id.edittext}。意思就是将Map对象中key为textview的value绑定到R.id.textview上,edittext也类似。
让我们来看一个例子
主布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" tools:context=".MainActivity" > <LinearLayout android:id = "@+id/listLinearLayou" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <ListView android:id = "@id/android:list" android:layout_width ="fill_parent" android:layout_height = "wrap_content" android:drawSelectorOnTop ="false" //这句话的意思是,点击一条记录,颜色会放映在文字的后面.为ture则为前面 android:scrollbars = "vertical" /> </LinearLayout> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:padding="10dp" android:paddingRight="10dp" android:paddingTop="1dp" android:paddingBottom="1dp" > <TextView android:id="@+id/user_name" android:layout_width="180dp" android:layout_height="30dp" android:textSize="12sp" android:singleLine="true" android:text="@string/hello_world" /> <TextView android:id="@+id/user_ip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right" android:textSize="12sp" android:text="@string/hello_world" /> </LinearLayout>
下面是主代码.
package com.example.day0324;import java.util.ArrayList;import java.util.HashMap;import android.os.Bundle;import android.app.ListActivity;import android.view.Menu;import android.view.View;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.Toast;public class MainActivity extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>(); HashMap<String, String> map1 = new HashMap<String, String>(); HashMap<String, String> map2 = new HashMap<String, String>(); HashMap<String, String> map3 = new HashMap<String, String>(); map1.put("user_name", "chenkai"); map1.put("user_ip", "192.168.0.1"); map2.put("user_name", "kuangfen"); map2.put("user_ip", "192.168.0.3"); map3.put("user_name", "yupengfei"); map3.put("user_ip", "192.168.0.5"); list.add(map1); list.add(map2); list.add(map3); SimpleAdapter listAdapter = new SimpleAdapter(this, list, R.layout.user, new String[]{"user_name","user_ip"}, new int[]{R.id.user_name,R.id.user_ip}); setListAdapter(listAdapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }@Overrideprotected void onListItemClick(ListView l, View v, int position, long id) {// TODO Auto-generated method stubsuper.onListItemClick(l, v, position, id);//点击的响应必须继承它.Toast.makeText(this, "位置" +position + " id"+id, Toast.LENGTH_SHORT).show();} }
- 安卓学习笔记之SimpleAdapter
- 安卓Adapter之SimpleAdapter
- 安卓学习之路_20140421_ListView、ArrayAdapter、SimpleAdapter
- Android学习笔记之SimpleAdapter
- Android学习笔记之SimpleAdapter
- Android学习笔记之SimpleAdapter
- Android学习笔记之ListView采用SimpleAdapter
- 安卓自学笔记:7:用SimpleAdapter创建ListView
- 安卓开发Android之SimpleAdapter ArrayAdapter 用法总结
- android学习之SimpleAdapter
- 安卓学习之控件学习笔记
- 安卓入门--SimpleAdapter详解
- 安卓之路-学习笔记1
- 安卓之路-学习笔记2
- 安卓之路-学习笔记3
- 安卓学习笔记之布局
- 安卓学习笔记之SQLite数据库
- 安卓学习笔记之调试
- YII 点滴(更新中)
- android自定义控件(五) 自定义组合控件
- Android中ViewPager使用FragmentPagerAdapter(底部圆点)
- WebView
- Shell 排序法
- 安卓学习笔记之SimpleAdapter
- 黑马程序员_iOS 的基础控件之xib
- 签到程序
- 黑马程序员---集合类数据结构
- 无插件Vim编程技巧
- Windows下Mysql主从配置(Mysql5.5)
- 获取div等标签的高度
- 在英特尔® 凌动™ 处理器上将 OpenGL* 游戏移植到 Android* (第二部分)
- Android WP双系统若要发展的难题