SimpleAdapter创建ListView
来源:互联网 发布:人工智能硬件有哪些 编辑:程序博客网 时间:2024/05/16 14:45
1 说明
SimpleAdapter是比较常用的列表适配器,用它来创建更加丰富和个性化的列表单项样式。这里介绍一下它的用法。
2 使用说明
构造函数如下:
SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
context:当前所在的Activity,一般直接写this;
data:包含Map的List数据;
resource:列表单项的布局文件;
from:Map中的Key的数组;
to:对应每个列表单项布局文件中的控件的id数组,该id应该和from中的key值一一对应。
3 创建ListView的步骤
1:包含有ListView的布局文件;
2:包含有列表单项的布局文件;
3:准备数据资源;
4:将数据填充到HashMap中,将HashMap填充到ArrayList中;
5:实例化SimpleAdapter;
6:为Listview 设置Adapter。
4 示例代码如下
1:包含有ListView的布局文件,名称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/id_list" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="#0ff" android:dividerHeight="5dp"></ListView></RelativeLayout>
2:列表单项的布局文件如下,名称item_layout.xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/imageView1" android:layout_width="150dp" android:layout_height="150dp" android:scaleType="fitXY" android:src="@drawable/ic_launcher" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="70dp" android:layout_alignLeft="@+id/textView1" android:layout_alignParentTop="true" android:layout_alignRight="@+id/textView1" android:text="button" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="70dp" android:layout_alignParentRight="true" android:layout_below="@+id/button1" android:layout_toRightOf="@+id/imageView1" android:gravity="center" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /></RelativeLayout>
3:MainActivity.Java文件代码如下:
public class MainActivity extends Activity { //文本框中的内容 private String[] name = {"螃蟹","狮子","小猪","海狮","老虎"}; //图片 private int[] imageId = {R.drawable.crack,R.drawable.lion,R.drawable.pig, R.drawable.sealion,R.drawable.tiger}; //编号 private String[] strNum = {"001","002","003","004","005"}; //列表视图 private ListView listView ; //List容器包含项目列表要填充的数据 private List<Map<String,Object>> list ; //SimpleAdapter private SimpleAdapter adapter ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //实例化List容器,使用了上转型 list = new ArrayList<Map<String,Object>>(); //向容器中添加数据 for (int i = 0; i < name.length; i++) { //创建Map容器,用来保存单个列表项的数据内容,保证每个列表项对应一个Map容器 Map<String,Object> map = new HashMap<String,Object>(); //数据填充 map.put("name", name[i]); map.put("imageId", imageId[i]); map.put("strNum", strNum[i]); //将Map添加到List容器中 list.add(map); } //*********************关 键 部 分************************* //实例化SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.item_layout, new String[]{"imageId","strNum","name"}, new int[]{R.id.imageView1,R.id.button1,R.id.textView1}); //实例化ListView listView = (ListView) findViewById(R.id.id_list); //为ListView添加适配器(SimpleAdapter) listView.setAdapter(adapter); }}
4:显示效果如下(示例中的小图片来源于百度,侵删)
5 总结
本示例可以实现列表样式,但是存在一下3个小问题,(哪位仁兄清楚这些问题,请不吝赐教,肯定感激不尽):
1:使用listView.setOnItemSelectedListener()和listView.setOnItemClickListener( )在本示例中不能实现监听;
2:对于每个列表中的Button如何设置单独监听;
3:添加的资源文件中示例图片不能太大,会出现outOfMemory的错误。
6 更新
对于总结中出现的问题这里有了一定的进展。
关于不能实现“监听”的问题:由于列表单项中包含了按钮,当点击时,按钮直接捕获了点击事件,因此listView.setOnItemSelectedListener()和listView.setOnItemClickListener( )在本示例中不能实现监听。修改方法:
(1)在activity_main.xml文件的RelativeLayout中添加:
这一句表示容器组件与其包含的容器组件之间,关于面对焦点事件时的处理顺序。这里选择阻止子容器组件获得焦点。参考这篇博文和这篇博文。
<!--Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus. > android:descendantFocusability="blocksDescendants"
在item_layout.xml文件中Button按钮的属性添加:
android:focusable="false"
这样就可以实现列表的监听事件了。
(2)对于按钮的监听事件:
我是在listView.setOnItemClickListener( )内写的Button的OnClick()方法,定义的方法listClick()如下,并将其写入onCreate()方法中。
private void listClick() { //adapter.getDropDownView(position, convertView, parent) listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { //按钮作为全局变量,在此处实例化。 btn = (Button) view.findViewById(R.id.button1); Toast.makeText(MainActivity.this, name[position], 100).show(); //按钮的点击事件。 btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, name[position]+strNum[position], 100).show(); } }); } }); }
但以上还是存在一个小问题,必须先点击item,然后再点击item上的Button它才会有反应。(到此,觉得要进一步解决该问题,要自定义一个继承BasedAdapter类的类了。)
- 使用SimpleAdapter创建ListView
- ListView-使用SimpleAdapter创建
- 使用SimpleAdapter创建ListView
- SimpleAdapter创建ListView
- SimpleAdapter创建ListView
- SimpleAdapter创建ListView
- 使用SimpleAdapter创建ListView
- 使用SimpleAdapter创建ListView
- 通过simpleAdapter创建复杂的ListView
- 第20章:使用SimpleAdapter创建ListView
- 【Android】利用ArrayAdapter/SimpleAdapter创建ListView
- 使用强大的SimpleAdapter创建ListView
- ListView(SimpleAdapter)
- ListView SimpleAdapter
- listview simpleadapter
- ListView SimpleAdapter
- ListView--SimpleAdapter
- 安卓自学笔记:7:用SimpleAdapter创建ListView
- hdu5641King's Phone
- 网站缓存技术
- 1010-小朋友排队
- Java中的除
- mysql和oracle的分页
- SimpleAdapter创建ListView
- Java eclipse下 Ant build.xml实例详解 附完整项目源码
- PAT_A 1108. Finding Average (20)
- HDU 5640:King's Cake【几何】
- C++string类型转换为C数组
- Matlab Multi-core Parallel Computing
- javascript中,函数的声明与表达式
- 用Source Insight追踪Arduino的setup和loop
- Delphi 10 Seattle 编译DevExpress VCl 14.x版本源码的问题