MainActivity.java
package cn.bzu.listview03;
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();
}//添加吐丝,进行事件的处理显示
}
//开始对每个事件添加内容,使用put方法
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", "瞅瞅");
item.put("publish", "10分钟前");
item.put("content", "以前,很多电脑显示器最多支持256色,因此出现了216种Web安全颜色,以保证网页的颜色能够正确显示。 那为什么不是256种Web安全颜色呢?因为Microsoft和Mac操作系统有40种不同的系统保留颜色。");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p2);
item.put("name", "欧欧");
item.put("publish", "20分钟前");
item.put("content", "人们认为目前的形式对以为即将上任的新总理实在不是个好时机");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p3);
item.put("name", "米粒");
item.put("publish", "15分钟前");
item.put("content", "惺惺惜惺惺!");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p4);
item.put("name", "等等");
item.put("publish", "1分钟前");
item.put("content", "将风浪踏在脚下");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p5);
item.put("name", "强强");
item.put("publish", "20分钟前");
item.put("content", "正在学习Androi");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p6);
item.put("name", "露露");
item.put("publish", "10分钟前");
item.put("content", "今天真高兴啊!");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p7);
item.put("name", "深深");
item.put("publish", "2分钟前");
item.put("content", "将风浪踏在脚下");
data.add(item);
item = new HashMap<String, Object>();
item.put("photo", R.drawable.p8);
item.put("name", "东东");
item.put("publish", "5分钟前");
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", "晕晕");
item.put("publish", "13分钟前");
item.put("content", "今天天气真好哈!");
data.add(item);
return data;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
activity_main.xml
<LinearLayout 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:textColorHint="#000000">
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
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"
android:background="#00ccff" >
<!-- 左边图片 -->
<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:textColor = "#000000" //文字颜色更改
android:orientation="vertical" >
<!-- 上边布局 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor = "#000000"
android:orientation="horizontal" >
<!-- 发布人 -->
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:textColor = "#000000"
android:layout_height="wrap_content" />
<!-- 发布时间 -->
<TextView
android:id="@+id/publish"
android:layout_width="fill_parent"
android:textColor = "#000000"
android:layout_height="wrap_content"
android:gravity="right" />
</LinearLayout>
<!-- 下边发布内容 -->
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:textColor = "#000000"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
string.xml
<resources>
<string name="app_name">ListView3</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">新浪微博——随便看看</string>
</resources>
dimens.xml
<resources>
<dimen name="padding_small">8dp</dimen>
<dimen name="padding_medium">8dp</dimen>
<dimen name="padding_large">16dp</dimen>
</resources>
bar.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">#00ccFF</item>//更改背景颜色
</style>
<style name="myTheme" parent="android:Theme">
<item name="android:windowNoTitle">false</item>
<item name="android:windowTitleSize">30dp</item>
<item name="android:background">#00ccFF</item>
<item name="android:windowTitleBackgroundStyle">@style/myTitleBg</item>
</style>
</resources>
styles.xml
<resources>
<style name="AppTheme" parent="android:Theme.Light" /> //设置系统自带的格式,给标题栏
</resources>
完成