Android-----SimpleAdapter创建ListView的实例,及值得注意的一些细节问题
来源:互联网 发布:java数组调用方法 编辑:程序博客网 时间:2024/05/01 23:15
老规矩,先上效果图:
源码:
package com.xiaoming.listviewagain;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.Toast;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class SimpleAdapterTest extends AppCompatActivity { private String[] names = new String[]{"name:XXX","name:YYY","name:AAA","name:CCC"}; private String[] ages = new String[]{"age:22","age:19","age:20","age:20"}; private final int[] headers = new int[]{R.drawable.back,R.drawable.blue1, R.drawable.red1,R.drawable.violet1}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_simple_adapter); List<Map<String,Object>> listItem = new ArrayList<Map<String,Object>>(); for(int i = 0; i < names.length; i++){ Map<String,Object> item = new HashMap<String,Object>(); item.put("mapHead",headers[i]); item.put("mapName",names[i]); item.put("mapAge",ages[i]); listItem.add(item); } SimpleAdapter simpleAdapter = new SimpleAdapter(this,listItem,R.layout.simple_item, new String[]{"mapName","mapAge","mapHead"}, new int[]{R.id.name,R.id.age,R.id.head}); ListView myList = (ListView)findViewById(R.id.myList); myList.setAdapter(simpleAdapter); myList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(SimpleAdapterTest.this,"you clicked "+position,Toast.LENGTH_SHORT).show(); } }); myList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(SimpleAdapterTest.this,"you selected "+position,Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); }}xml:R.layout.activity_simple_adapter
<?xml version="1.0" encoding="utf-8"?><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="match_parent" android:orientation="vertical" tools:context=".SimpleAdapterTest"> <ListView android:id="@+id/myList" android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView></LinearLayout>
xml2:R.layout.simple_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/head" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/age" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout></LinearLayout>
其中应该注意的一些细节问题:
SimpleAdapter simpleAdapter = new SimpleAdapter(this,listItem,R.layout.simple_item, new String[]{"mapName","mapAge","mapHead"}, new int[]{R.id.name,R.id.age,R.id.head});1. 这里第一个String 数组的作用是识别Map集合中的键,数组中的值必须为Map中的键值,否则不能正确加载显示。
2.这里的第二个int 数组用来识别R.layout.simple_item 中的控件id,数组中的值也必须为item布局中的控件id。
3.这两个数组的赋值顺序必须一一对应,否则资源不能正确加载
下面是一个资源不能正确加载的实例:(看数组的赋值顺序)
SimpleAdapter simpleAdapter = new SimpleAdapter(this,listItem,R.layout.simple_item, new String[]{"mapName","mapAge","mapHead"}, new int[]{R.id.age,R.id.head,R.id.name});
0 0
- Android-----SimpleAdapter创建ListView的实例,及值得注意的一些细节问题
- 【JDBC开发】JDBC一些值得注意的细节问题
- Paint的一些值得注意的细节
- Android 创建文件夹,文件的一些注意细节
- 【xenclient】 使用小结 -- 安装过程及值得注意的一些细节
- 值得注意的小细节
- 【Android】【ViewGroup】【ListView】ListView的一些细节
- scanf一些值得注意的问题
- javascript 值得注意的一些问题
- Android实例demo11之listview的简单使用(arrayadapter、simpleadapter)
- android的ListView中SimpleAdapter
- Android中动态生成ListView及SimpleAdapter的使用
- Android - SimpleAdapter适配器支持的组件及Listview模拟下载
- 通过simpleAdapter创建复杂的ListView
- 使用强大的SimpleAdapter创建ListView
- js值得注意的一个细节
- 一些值得注意的地方
- ListView应该注意的一些问题
- memcache详解
- 2016.11.5GDOI模拟总结
- CODEVS 1077多源最短路
- shell编程之字符命令
- 设计模式之单例模式
- Android-----SimpleAdapter创建ListView的实例,及值得注意的一些细节问题
- Android开发 获取屏幕宽高及尺寸的方法
- HDU 1215 七夕节
- effective C++之模板元编程
- SQL case when语法
- HDU 1214 圆桌会议(思维)
- linux 查看内存
- 【web】模拟京东项目-demo01(广告栏、导航条)
- 动态自动匹配输入的控件AutoCompleteTextView和MultiAutoCompleteTextView