Android4.0.4编程日记(2)--ListView
2013-01-22 14:08
288 查看
package com.example.test; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.SimpleAdapter; /** * http://www.cnblogs.com/pswzone/archive/2012/03/10/2389275.html * http://www.iteye.com/topic/540423 http://www.beijibear.com/?aid=341 * http://www.beijibear.com/?aid=341 * @author Administrator * */ public class MainActivity1 extends Activity { private ListView mylistview; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mylistview = (ListView) findViewById(R.id.listview); ArrayList<HashMap<String, String>> myArrayList = new ArrayList<HashMap<String, String>>(); for (int i = 0; i < 5; i++) { HashMap<String, String> map = new HashMap<String, String>(); map.put("itemTitle", "This Is Title " + i); map.put("itemContent", "This Is Content " + i); myArrayList.add(map); } SimpleAdapter mySimpleAdapter=new SimpleAdapter(this,myArrayList,//数据源 R.layout.list_items,//ListView内部数据展示形式的布局文件listitem.xml new String[]{"itemTitle","itemContent"},//HashMap中的两个key值 itemTitle和itemContent new int[]{R.id.itemTitle,R.id.itemContent});/*布局文件listitem.xml中组件的id 布局文件的各组件分别映射到HashMap的各元素上,完成适配*/ mylistview.setAdapter(mySimpleAdapter); mylistview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { setTitle("点击第" + arg2 + "个项目"); } }); } }
layout中list_items.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:paddingBottom="4dip"
android:paddingLeft="12dip"
android:paddingRight="12dip">
<TextView
android:text="itemTitle"
android:layout_height="wrap_content"
android:textSize="20dip"
android:layout_width="fill_parent"
android:id="@+id/itemTitle"
/>
<TextView
android:text="itemContent"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="@+id/itemTitle"
android:id="@+id/itemContent"
/>
</RelativeLayout>
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"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
</LinearLayout>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
相关文章推荐
- Android4.0.4编程日记(3)--ListView单击获取事件
- Android4.0.4编程日记(4)--List单击Intent跳转并获取数据
- Android4.0.4编程日记(5)--URL网络链接获取数据
- Android4.0.4编程日记(1)--安装配置
- Android编程实战——仿微信群聊-5——Android端UI-多种显示的ListView
- Android编程之控件ListView使用方法
- Android实训案例(三)——实现时间轴效果的ListView,加入本地存储,实现恋爱日记的效果!
- Android 编程下去除 ListView 上下边界蓝色或黄色阴影
- Android编程使用缓存优化ListView的方法
- Android编程实现带有图标的ListView并带有长按菜单效果示例
- 开发日记(4):android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:4090)数组越界异常
- android编程之ExpandableListView使用总结
- android编程之ExpandableListView使用总结
- android-基础编程-ListView
- Android 编程日记 如何通过包名打开一个应用程序
- Android编程心得-ListView的Item高亮显示的办法
- Android实训案例(三)——实现时间轴效果的ListView,加入本地存储,实现恋爱日记的效果!
- Android编程应用--仿QQ我的好友ExpandableListView应用
- Android 编程下自定义 ListView 无法响应 OnItemClickListener 中的 onItemClick 方法问题解决方案
- Android编程之ListView和EditText发布帖子隐藏软键盘功能详解