Android-----SimpleAdapter创建ListView的实例,及值得注意的一些细节问题

老规矩,先上效果图:



源码:

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值