<h2><strong>1、运行效果图。</strong></h2><pre name="code" class="html"><img src="https://img-blog.csdn.net/20150512164505984?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGlhbWVuZ3lhb18yMDE1/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
2、目的
3、程序分析
1、两部分:主页面和ListView页面
2、编写实体类和ListView自定义适配器
3、MainActivity中连接适配器和实体类,绑定数据源与控件
4、XML布局
1)在res/value下建立mytitlebar.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="myTitleBg">
<item name="android:background">#FF0000</item>
</style>
<style name="myTheme" parent="android:Theme">
<item name="android:windowNoTitle">false</item>
<item name="android:windowTitleSize">30dp</item>
<item name="android:windowTitleBackgroundStyle">@style/myTitleBg</item>
</style>
</resources>
2)修改AndroidManifest.xml文件
android:theme="@style/myTheme"
3)添加两个xml文件:activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_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=".MainActivity" >
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<!-- 左边图片 -->
<ImageView
android:id="@+id/photo"
android:padding="10dp"
android:layout_width="48dp"
android:layout_height="48dp" />
<!-- 右边布局 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- 上边布局 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- 发布人 -->
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- 发布时间 -->
<TextView
android:id="@+id/publish"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right" />
</LinearLayout>
<!-- 下边发布内容 -->
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
5、实现代码:
<span style="font-family: Arial, Helvetica, sans-serif;">package cn.bzu.listview03;</span>
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
//第一步:extends Activity
public class MainActivity extends Activity {
// 第二步:定义数据集合
List<Map<String, ?>> data;
ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data = getData();
// 第三步:创建SimpleAdapter绑定数据
SimpleAdapter adapter = new SimpleAdapter(this, data,
R.layout.list_item, new String[] { "photo", "name", "publish",
"content" }, new int[] { R.id.photo, R.id.name,
R.id.publish, R.id.content });
listView=(ListView) this.findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new ListClickHandler());
}
//第四步:添加单击事件
private class ListClickHandler implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
Map<String, String> item=(Map<String, String>) data.get(position);
Toast.makeText(MainActivity.this, item.get("name").toString(), Toast.LENGTH_LONG).show();
}
}
private List<Map<String, ?>> getData() {
List<Map<String, ?>> data = new ArrayList<Map<String, ?>>();
Map<String, Object> item = new HashMap<String, Object>();
item.put("photo", R.drawable.p1);
item.put("name", "Monkey逗");
item.put("publish", "1分钟前");
item.put("content", "本人正在练习七十二变,以确保将来取经路上不被妖怪揍,嘎嘎。");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p2);
item.put("name", "华华迷");
item.put("publish", "5分钟前");
item.put("content", "今天刷微博,半个小时献给了霍建华大帅哥,还是那么迷人,快hold不住了,永远支持华华!!!");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p3);
item.put("name", "果壳中的世界");
item.put("publish", "8分钟前");
item.put("content", "一个都比今天来找我了,让我跟她去超市买A4纸,但是不幸的是姐正好有,于是她一脸崇拜的看着纸,走了!");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p4);
item.put("name", "一毛钱");
item.put("publish", "2分钟前");
item.put("content", "今天我得到了两只杯子,一看价好贵的,虽然它们只是赠品,哈哈。");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p5);
item.put("name", "sunshine");
item.put("publish", "3分钟前");
item.put("content", "今天看了,爱不将就的预告片,瞬间被刺激到了!");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p6);
item.put("name", "林JJ");
item.put("publish", "10分钟前");
item.put("content", "林俊杰啥时候在出新歌啊,期盼期盼!");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p7);
item.put("name", "DaMingLing");
item.put("publish", "15分钟前");
item.put("content", "滨州这小破天,我也是醉了,啥时候正常了,记得提示下,OK!");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p8);
item.put("name", "核桃小丸子");
item.put("publish", "20分钟前");
item.put("content", "今天买了一本书,名字叫《时间简史》霍金写的,买回来的时候好激动,因为想看他好久了,结果一舍友说咱俩不在一个世界里,你的世界真难懂,我问为啥," +
"她说就冲霍金这个名字我们就已经完败了,哈哈哈。");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p9);
item.put("name", "二进制时代");
item.put("publish", "4分钟前");
item.put("content", "今天一老同学说,一看你这名字就知道你是干啥的,我。。。。。。");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p10);
item.put("name", "Monkey逗");
item.put("publish", "1分钟前");
item.put("content", "我师父找不着了 ,你们造吗?");
data.add(item);
return data;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}