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
原创粉丝点击