
1:Main_activity.java:
public class MainActivity extends Activity {
// 第二步:定义数据集合
List
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{
public void onItemClick(AdapterView
adapterView, View view, inposition,long id)
{Map
item=
)
Toast.makeText(MainActivity.this, item.get(“name”).toString(), Toast.LENGTH_LONG).show();}}
private List
List
new ArrayList
Map
item =
();
item.put(“photo”, R.drawable.p1);
item.put(“name”, “想想”);
item.put(“publish”, “1分钟前”);
item.put(“content”, “正在学习AndroidListView”);
data.add(item);
item = new HashMap
();
item.put(“photo”, R.drawable.p2);
item.put(“name”, “嘻嘻”);
item.put(“publish”, “10分钟前”);
item.put(“content”, “今天真高兴啊!”);
data.add(item);
item = new HashMap
();
item.put(“photo”, R.drawable.p3);
item.put(“name”, “米粒”);
item.put(“publish”, “5分钟前”);
item.put(“content”, “今天糗大了!”);
data.add(item);
item = new HashMap
();
item.put(“photo”, R.drawable.p4);
item.put(“name”, “丽丽”);
item.put(“publish”, “1分钟前”);
item.put(“content”, “今天遇到一件好玩的事情!”);
data.add(item);
return data;}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;}}
<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” >
<ListView
android:id=“@+id/listView”
android:layout_width=“fill_parent”
android:layout_height=“fill_parent” />
LinearLayout>
List_view.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>
Title.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>
String.xml:
<resources><string name=“app_name”>ListView03
string>
<string name=“hello_world”>Hello world!
string>
<string name=“menu_settings”>Settings
string>
<string name=“title_activity_main”>新浪微博——随便看看
string>
resources>
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/198408.html原文链接:https://javaforall.net
