ListView.SimpleAdapter笔记

来源:互联网 发布:汽车设计软件发展趋势 编辑:程序博客网 时间:2024/05/29 16:32

《定义map对象的layout排版simple_item.xml文件》


<?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="match_parent"
    android:orientation="horizontal" >     //水平摆放
    <ImageView 
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        />
    <LinearLayout                  //嵌套线性布局      人名和备注信息垂直摆放
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView 
            android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:textColor="#f0f"
        android:paddingLeft="10dp"
        />
        <TextView 
            android:id="@+id/desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14dp"
        android:paddingLeft="10dp" />
        </LinearLayout>
</LinearLayout>



package com.example.simpleadapterapplication;



import android.os.Bundle;
import java.util.*;


import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;


public class MainActivity extends Activity {
private static final String TAG="result";
//需要显示的名字
private String[] names=new String[]{
"workharder","furture","love","parents"
};
//备注信息
private String[] descs=new String[]{
"叶大神","未来","迷茫","树木"
};
//添加头像信息
private int[] imageIds=new int[]{
R.drawable.yecun1,R.drawable.yecun2,R.drawable.yecun3,R.drawable.yecun4
};
private List<? extends Map<String, ?>> listItems;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//定义列表集合
List<Map<String,Object>> listItems=new ArrayList<Map<String,Object>>();
//利用循环向列表集合添加Map<string,Object>元素
for(int i=0;i<names.length;i++){
Map<String,Object> listItem=new HashMap<String,Object>();
listItem.put("header", imageIds[i]);   //header为key,往里添加图片元素
listItem.put("personName", names[i]);// personName为key,往里添加名字
listItem.put("desc", descs[i]);// desc  添加备注信息
listItems.add(listItem);  //将行的信息map ,添加到listItems
}
//创建SimpleAdapter,context第一个参数,list集合对象第二个参数,simple_item.xml定义map布局文件第三个参数,map对象的key第四个参数,int【】型参数,该参数决定填充那些组件,其为simple_item的id
SimpleAdapter simpleAdapter=new SimpleAdapter(this, listItems,R.layout.simple_item, new String[]{"personName","header","desc"},new int[]{R.id.name,R.id.header,R.id.desc});
   ListView alllist=(ListView)findViewById(R.id.list);
   // 添加adapter
   alllist.setAdapter(simpleAdapter);
   alllist.setOnItemClickListener(new AdapterView.OnItemClickListener(){


@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Log.i(TAG, "ASDF ASDF");
}
   
   });
}

@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;
}

}

0 0
原创粉丝点击