设为首页收藏本站

Scripts 学盟

 找回密码
 加入学盟

QQ登录

只需一步,快速开始

查看: 3749|回复: 8
打印 上一主题 下一主题

android(七) Activity 与 ListActivity 使用BaseAdapter [复制链接]

Rank: 8Rank: 8

跳转到指定楼层
1#
那个谁 发表于 2011-5-18 17:07:25 |只看该作者 |倒序浏览
  1. package com.qwj.MyAndroid.bll;

  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;

  6. import android.app.Activity;
  7. import android.content.Context;
  8. import android.os.Bundle;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.BaseAdapter;
  13. import android.widget.ListView;
  14. import android.widget.TextView;

  15. /**
  16. * @author QWJ
  17. * 如果需要测试setListAdapter 把 ListView01 继承 ListActivity
  18. */
  19. public class ListView01 extends Activity {

  20.         private ListView listView = null;
  21.         private List<Map<String, Object>> hasmapData = null;

  22.         @Override
  23.         protected void onCreate(Bundle savedInstanceState) {
  24.                 // TODO Auto-generated method stub
  25.                 super.onCreate(savedInstanceState);
  26.                  
  27.                 try {
  28.                         // ArrayAdapter 使用   
  29.                         // listView = new ListView(this);
  30.                         // listView.setAdapter(new ArrayAdapter<String>(this,
  31.                         // android.R.layout.simple_list_item_1, getData()));
  32.                         //
  33.                         // SimpleAdapter  使用
  34.                         // SimpleAdapter simpleadapter = new SimpleAdapter(this,
  35.                         // getHashMapData(), R.layout.tablelayout, new String[] {
  36.                         // "name", "sex", "descripton" }, new int[] {
  37.                         // R.id.txtName, R.id.txtSex, R.id.txtdescription });
  38.                         // setListAdapter(simpleadapter);
  39.                        
  40.                         // BaseAdapter  使用
  41.                         // hasmapData = getHashMapData();
  42.                         // MyAdapter adpter = new MyAdapter(this);
  43.                         // setListAdapter(adpter);
  44.                        
  45.                         setContentView(R.layout.tablelayout); //继承 Activity 必须先加载布局文件、不然后面使用的View就不能找到。
  46.                         hasmapData = getHashMapData();
  47.                         MyAdapter adpter = new MyAdapter(getApplicationContext());
  48.                         ListView listView = (ListView) findViewById(R.id.listview);
  49.                         listView.setAdapter(adpter);

  50.                 } catch (Exception ex) {
  51.                         System.out.println(ex.getMessage());
  52.                 }
  53.         }

  54.         /**
  55.          * 获取适配器的数据
  56.          *
  57.          * @return List<String>
  58.          */
  59.         public List<String> getData() {
  60.                 List<String> resultList = new ArrayList<String>();
  61.                 resultList.add("漠漠");
  62.                 resultList.add("非男");
  63.                 resultList.add("漠漠老撞树!");

  64.                 return resultList;
  65.         }

  66.         /**
  67.          * 获取适配器的数据
  68.          *
  69.          * @return List<Map<String, Object>>
  70.          */
  71.         public List<Map<String, Object>> getHashMapData() {
  72.                 List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();

  73.                 Map<String, Object> tempData0 = new HashMap<String, Object>();
  74.                 tempData0.put("name", "漠漠");
  75.                 tempData0.put("sex", "非男");
  76.                 tempData0.put("descripton", "漠漠老撞树!");
  77.                 resultList.add(tempData0);

  78.                 Map<String, Object> tempData1 = new HashMap<String, Object>();
  79.                 tempData1.put("name", "漠漠");
  80.                 tempData1.put("sex", "女");
  81.                 tempData1.put("descripton", "漠漠老迟到!");
  82.                 resultList.add(tempData1);

  83.                 Map<String, Object> tempData2 = new HashMap<String, Object>();
  84.                 tempData2.put("name", "漠漠");
  85.                 tempData2.put("sex", "其他");
  86.                 tempData2.put("descripton", "漠漠老喜欢帅哥啦!");
  87.                 resultList.add(tempData2);
  88.                
  89.                 Map<String, Object> tempData3 = new HashMap<String, Object>();
  90.                 tempData3.put("name", "漠漠");
  91.                 tempData3.put("sex", "火星");
  92.                 tempData3.put("descripton", "漠漠老不淡定了。!");
  93.                 resultList.add(tempData3);
  94.                
  95.                 Map<String, Object> tempData4 = new HashMap<String, Object>();
  96.                 tempData4.put("name", "漠漠");
  97.                 tempData4.put("sex", "地球");
  98.                 tempData4.put("descripton", "漠漠老撞树。!");
  99.                 resultList.add(tempData4);

  100.                 return resultList;
  101.         }

  102.         /**
  103.          * 页面缓存
  104.          * @author QWJ
  105.          */
  106.         public final class ViewHolder {
  107.                 public TextView name;
  108.                 public TextView sex;
  109.                 public TextView info;
  110.                 // public Button viewBtn;
  111.         }

  112.         /**
  113.          * 自定义适配器
  114.          * @author QWJ
  115.          */
  116.         public class MyAdapter extends BaseAdapter {

  117.                 private LayoutInflater mInflater;

  118.                 public MyAdapter(Context context) {
  119.                         mInflater = LayoutInflater.from(context); // 加载一个空页
  120.                 }

  121.                 @Override
  122.                 public int getCount() {
  123.                         // TODO Auto-generated method stub 循环多少行。
  124.                         return hasmapData.size();
  125.                 }

  126.                 @Override
  127.                 public Object getItem(int position) {
  128.                         // TODO Auto-generated method stub 当前项
  129.                         return position;
  130.                 }

  131.                 @Override
  132.                 public long getItemId(int position) {
  133.                         // TODO Auto-generated method stub 当前第几行
  134.                         return position;
  135.                 }

  136.                 @Override
  137.                 public View getView(int position, View convertView, ViewGroup parent) {
  138.                         // TODO Auto-generated method stub 返回一个View
  139.                         ViewHolder vh = null;
  140.                         try {
  141.                                 //如果页面为null 创建一个缓存ViewHolder页并且设置Tag
  142.                                 if (convertView == null) {
  143.                                         vh = new ViewHolder();
  144.                                         convertView = mInflater
  145.                                                         .inflate(R.layout.resultlayout, null);
  146.                                         vh.name = (TextView) convertView.findViewById(R.id.txtName);
  147.                                         vh.sex = (TextView) convertView.findViewById(R.id.txtSex);
  148.                                         vh.info = (TextView) convertView
  149.                                                         .findViewById(R.id.txtdescription);
  150.                                         convertView.setTag(vh);
  151.                                 } else {
  152.                                         //获取Tag
  153.                                         vh = (ViewHolder) convertView.getTag();
  154.                                 }
  155.                                 //设置值
  156.                                 vh.name.setText((String) hasmapData.get(position).get("name"));
  157.                                 vh.sex.setText((String) hasmapData.get(position).get("sex"));
  158.                                 vh.info.setText((String) hasmapData.get(position).get(
  159.                                                 "descripton"));
  160.                         } catch (Exception ex) {
  161.                                 System.out.println(ex.getMessage());
  162.                         }

  163.                         return convertView;
  164.                 }

  165.         }
  166. }
复制代码
页面 resultlayout.xml
  1. <?xml version="1.0" encoding="utf-8"?>

  2.         <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.                 android:layout_width="match_parent" android:layout_height="match_parent"
  4.                 android:shrinkColumns="1" android:orientation="vertical" >
  5.                 <TableRow>
  6.                         <TextView android:id="@+id/txtName" android:layout_width="match_parent" android:layout_height="60px" android:text="姓名222"
  7.                         android:layout_weight="1" android:gravity="center"
  8.                         ></TextView>
  9.                         <TextView android:id="@+id/txtSex" android:layout_width="match_parent" android:layout_height="match_parent" android:text="性别222"
  10.                         android:layout_weight="1" android:gravity="center"
  11.                         ></TextView>
  12.                         <TextView android:id="@+id/txtdescription" android:layout_width="match_parent" android:layout_height="match_parent" android:text="描述333"
  13.                         android:layout_weight="1" android:gravity="center"
  14.                         ></TextView>
  15.                 </TableRow>
  16.                  
  17.         </TableLayout>
复制代码
页面 resultlayout.xml
  1. <?xml version="1.0" encoding="utf-8"?>

  2.         <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.                 android:layout_width="match_parent" android:layout_height="match_parent"
  4.                 android:shrinkColumns="1" android:orientation="vertical" >
  5.                 <TableRow>
  6.                         <TextView  android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="姓名"
  7.                         android:layout_weight="1" android:gravity="center" android:background="#aa0000"
  8.                         ></TextView>
  9.                         <TextView  android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="性别"
  10.                         android:layout_weight="1" android:gravity="center" android:background="#00aa00"
  11.                         ></TextView>
  12.                         <TextView  android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="描述"
  13.                         android:layout_weight="1" android:gravity="center" android:background="#0000aa"
  14.                         ></TextView>
  15.                 </TableRow>
  16.                  <ListView   
  17.         android:id="@+id/listview"  
  18.         android:layout_width="fill_parent"   
  19.         android:layout_height="wrap_content"  
  20.     />  
  21.         </TableLayout>
复制代码
结果:
QQ截图未命名.png
1

查看全部评分

分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
分享分享0 收藏收藏0

Rank: 8Rank: 8

2#
那个谁 发表于 2011-5-18 17:09:01 |只看该作者
本帖最后由 那个谁 于 2011-5-18 17:10 编辑

没文字说明情况注释。给个被人写的地址:http://blog.csdn.net/Android_Tut ... /07/01/5707835.aspx

解释下老喜欢用漠漠进行测试。。因为她长不大。可爱的漠漠 {:3_42:}

使用道具 举报

管理员

超级大菜鸟

Rank: 9Rank: 9Rank: 9

3#
Alvin 实名认证  发表于 2011-5-18 21:55:42 |只看该作者
very gelivable!

使用道具 举报

Rank: 6Rank: 6

4#
Yisin 发表于 2011-5-20 08:41:25 |只看该作者
那个谁 好厉害
路不好走,你却依旧满眼的爱,找不到理由...

使用道具 举报

Rank: 8Rank: 8

5#
那个谁 发表于 2011-5-20 09:20:50 |只看该作者
回复 4# Yisin


    略懂略懂。。

使用道具 举报

管理员

超级大菜鸟

Rank: 9Rank: 9Rank: 9

6#
Alvin 实名认证  发表于 2011-5-20 16:00:51 |只看该作者
那个杰

后面还有木有啊。。。继续上啊

使用道具 举报

Rank: 8Rank: 8

7#
那个谁 发表于 2011-5-20 17:28:18 |只看该作者
回复 6# 混混@普宁.中国


    别急。。。忙哦。。。压力

使用道具 举报

管理员

超级大菜鸟

Rank: 9Rank: 9Rank: 9

8#
Alvin 实名认证  发表于 2011-5-20 17:29:10 |只看该作者


我等的好心急

使用道具 举报

Rank: 8Rank: 8

9#
那个谁 发表于 2011-5-20 17:32:47 |只看该作者
回复 8# 混混@普宁.中国


    心急吃不了热豆腐。。。。

使用道具 举报

您需要登录后才可以回帖 登录 | 加入学盟

手机版|Scripts 学盟   |

GMT+8, 2024-4-29 14:01 , Processed in 1.124308 second(s), 17 queries .

Powered by Discuz! X2

© 2001-2011 Comsenz Inc.

回顶部