首页 > 学院 > 开发设计 > 正文

用SimpleAdapter来设置ListView的内容

2019-11-09 14:16:57
字体:
来源:转载
供稿:网友

输入图片说明

Mainactivit.java

package com.kale.listview;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.ListView;import android.widget.SimpleAdapter;public class MainActivity extends Activity { ListView myLv; @Override PRotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myLv = (ListView)findViewById(R.id.red_listView_id); //定义5个人名数组,5个性别数组,5个图片(图省事,我用一个图片代替了) final String []name = {"Jack","Tony","Tom","Luna","Nacy"}; String []sex = {"male","male","male","female","female"}; int []pic = {R.drawable.kale,R.drawable.kale,R.drawable.kale,R.drawable.kale,R.drawable.kale}; //建立一个list,里面的元素师Map,Map中是String,object List<Map<String, Object>> list = new ArrayList<Map<String,Object>>(); for (int i = 0; i < name.length; i++) { //建立list中的子元素,用键值对的形式来存放数据 Map<String, Object> listItem = new HashMap<String, Object>(); listItem.put("picture", pic[i]); listItem.put("name", name[i]); listItem.put("sex", sex[i]); //将子元素添加入list中 list.add(listItem); } //建立一个simpleAdapter //1.Context对象,2.list对象,3.item的布局文件,4.各个元素来源的键名,5.各个元素对应控件的id SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.item, new String[] {"picture","name","sex"}, new int[] {R.id.pic_imageView_id,R.id.name_textView_id,R.id.sex_textView_id}); myLv.setAdapter(adapter); myLv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View parent, int position,long id) { // TODO 自动生成的方法存根 System.out.println("onItemClick"+name[position]+"被选中了"); } }); //列表项被选中后的监听器,两个监听器的区别参考这篇文章:http://blog.csdn.net/bcai2/article/details/15028979 //就和电视的菜单一样,上下选择来触发选中(selected)事件,按下确定才是点击(click)事件。 myLv.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View arg1,int position, long id) { // TODO 自动生成的方法存根 System.out.println("onItemSelected"+name[position]+"被选中了"); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO 自动生成的方法存根 } }); }}

activity_main.xml

<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" > <!-- 设置红色分割线的listView --> <ListView android:id="@+id/red_listView_id" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="#f00" android:dividerHeight="2dp" android:layout_gravity="center" android:headerDividersEnabled="false"/></LinearLayout>

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="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/pic_imageView_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/name_textView_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Large Text" android:layout_marginLeft="10dp" android:layout_gravity="center_vertical" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/sex_textView_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" android:textColor="#0000ff" android:text="Small Text" android:textAppearance="?android:attr/textAppearanceSmall" /></LinearLayout>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表