android之simpleAdaper应用
listView页面布局:layout/activity_main.xml:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.day_05_02.MainActivity" >
<ListView
android:id="@+id/lvGenerals"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#ccc"
android:dividerHeight="10dp"
/>
</RelativeLayout>
该布局下的java代码 :
package com.example.day_05_02;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends ActionBarActivity {
private ListView lvGenerals;//listView对象
private List<Map<String,Object>> generals;//要显示数据的集合
private final String ImageSource = "imageSource";//map中key的值
private final String GeneralName = "name";//map中key的值
private BaseAdapter generalAdapter;//适配器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
}
private void initData() {
// TODO Auto-generated method stub
//初始化要显示的数据集合
generals = new ArrayList<Map<String,Object>> ();
//初始化图片资源
int [] images = {
R.drawable.baiqi,R.drawable.caocao,R.drawable.chengjisihan,
R.drawable.hanxin,R.drawable.lishimin,R.drawable.nuerhachi,
R.drawable.sunbin,R.drawable.sunwu,R.drawable.yuefei,
R.drawable.zhuyuanzhang
};
//初始化文字资源
String [] names = getResources().getStringArray(R.array.generals);
//把图片和文字资源放到一个map中,然后再把该map加到generals这个list之中
for(int i=0;i<names.length;i++){
Map<String,Object> general = new HashMap<String,Object>();
general.put(ImageSource, images[i]);
general.put(GeneralName, names[i]);
generals.add(general);
}
}
private void initView() {
// TODO Auto-generated method stub
lvGenerals = (ListView) findViewById(R.id.lvGenerals);
//初始化适配器
//重点解释第一个参数是要显示的布局所在的activity对象,第二个参数是要显示数据的集合对象,第三个是指用于适配的布局页面,
//第四个是指map中的key值,第五个值是指适配布局页面和map中value对应上的布局控件
generalAdapter = new SimpleAdapter(this, generals,
R.layout.activity_general_,
new String[]{ImageSource,GeneralName} , new int[]{R.id.ivTumb,R.id.tvName});
//设置适配器
lvGenerals.setAdapter(generalAdapter);
}
}
适配页面布局:layout/activity_general_.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">
<ImageView
android:id="@+id/ivTumb"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/baiqi"
/>
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="白起"/>
</LinearLayout>
String字符串资源:values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Day_05_02</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="title_activity_general_">General_Activity</string>
<string-array name="generals">
<item>白起</item>
<item>曹操</item>
<item>成吉思汗</item>
<item>韩信</item>
<item>李世民</item>
<item>努尔哈赤</item>
<item>孙膑</item>
<item>孙武</item>
<item>朱元璋</item>
<item>岳飞</item>
</string-array>
</resources>
效果:
本文出自 “追随心的彼岸” 博客,谢绝转载!
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。