listView布局:

<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.homework04.MainActivity" > <ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" /> </RelativeLayout>

item布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- 图像 -->
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/image1"
/>
<!-- 姓名 -->
<TextView
android:id="@+id/text_name"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image"
android:textSize="20sp"
android:layout_marginTop="10dp"
android:text="名侦探柯南"/>
<!-- 年龄 -->
<TextView
android:id="@+id/text_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image"
android:layout_alignBottom="@id/image"
android:textSize="20sp"
android:layout_marginBottom="10dp"
android:text="17"/>
<!-- 按钮 -->
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_toRightOf="@id/text_name"
android:layout_marginTop="25dp"
android:text="更多"
/>
</RelativeLayout>

源代码:

package com.example.homework04;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.SimpleAdapter; public class MainActivity extends Activity {
private ListView listview;
private SimpleAdapter adapter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化adapter
adapter = new SimpleAdapter(
MainActivity.this, // 第一个参数:上下文
getData(),// 第二个参数:listview中显示的数据
R.layout.item,// 第三个参数:每行的布局
new String[]{"image", "name", "age", "button"},// 第四个参数:字符串数组 Map中的key的值
new int[]{R.id.image, R.id.text_name, R.id.text_age, R.id.button}); // 第五个参数:item.xml中每个控件的id
listview = (ListView) findViewById(R.id.listview);
listview.setAdapter(adapter);
} // 生成listview显示的数据

    private List<Map<String, ?>> getData() {
    // TODO Auto-generated method stub
      List<Map<String,?>> list = new ArrayList<Map<String,?>>();
      int[] image = {R.drawable.image1,R.drawable.image2,R.drawable.image3};
      String[] name={"野比大雄","野比先生","野比太太"};
      String[] age={"12","40","38"};
      for (int i = 0; i < age.length; i++) {
      Map<String,Object> map = new HashMap<String, Object>();
      map.put("image", image[i]);
      map.put("text_name", name[i] );
      map.put("text_age", age[i] );
      map.put("button", "更多");
      list.add(map);

      }
    return list;

    }
}

Android_listView_exc的更多相关文章

随机推荐

  1. 【HDOJ】Power Stations

    DLX.针对每个城市,每个城市可充电的区间构成一个plan.每个决策由N*D个时间及N个精确覆盖构成. /* 3663 */ #include <iostream> #include &l ...

  2. BOM List demo

    select level level_id,        t.*   from (select msi1.segment1 farther_item,                msi1.inv ...

  3. trash目录: ~/.local/share/Trash

    trash目录:~/.local/share/Trash

  4. hdu4655Cut Pieces

    http://acm.hdu.edu.cn/showproblem.php?pid=4655 先以最大的来算为 N*所有的排列数  再减掉重复的 重复的计算方法:取相邻的两个数的最小值再与它前面的组合 ...

  5. bzoj1054

    弱弱的搜索题, 我的做法是将矩阵看做二进制然后用位运算来做的,感觉比较舒服 ..] ,,,);       dy:..] ,,-,); type node=record        po,next: ...

  6. Guid 的几种形式

    Guid.NewGuid().ToString()得几种格式显示 1.Guid.NewGuid().ToString("N") 结果为:       38bddf48f43c485 ...

  7. web网站加速之CDN(Content Delivery Network)技术原理

    在不同地域的用户访问网站的响应速度存在差异,为了提高用户访问的响应速度.优化现有Internet中信息的流动,需要在用户和服务器间加入中间层CDN. 使用户能以最快的速度,从最接近用户的地方获得所需的 ...

  8. 负载均衡服务器session共享的解决方案

    在ASP.NET的程序中要使用Session对象时,必须确保页面的@page指令中EnableSessionState属性是True或者Readonly,并且在web.config文件中正确的设置了S ...

  9. NameValueCollection类

    最近在研究HttpRequest类,发现里面的很多属性都返回一个NameValueCollection对象,今天再来了解一下这个神秘的对象. 随便写了个例子,发现跟HashTable类似.但是这个东西 ...

  10. lightoj 1002

    最短路的变形,使用spfa做. #include<set> #include<map> #include<list> #include<stack> # ...