- 注册时间
- 2011-3-22
- 最后登录
- 2013-6-27
- 在线时间
- 11644 小时
- 阅读权限
- 150
- 积分
- 62779
- 帖子
- 28923
- 精华
- 1
- UID
- 6
|
- package com.qwj.MyAndroid.bll;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import android.app.Activity;
- import android.content.Context;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ListView;
- import android.widget.TextView;
- /**
- * @author QWJ
- * 如果需要测试setListAdapter 把 ListView01 继承 ListActivity
- */
- public class ListView01 extends Activity {
- private ListView listView = null;
- private List<Map<String, Object>> hasmapData = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
-
- try {
- // ArrayAdapter 使用
- // listView = new ListView(this);
- // listView.setAdapter(new ArrayAdapter<String>(this,
- // android.R.layout.simple_list_item_1, getData()));
- //
- // SimpleAdapter 使用
- // SimpleAdapter simpleadapter = new SimpleAdapter(this,
- // getHashMapData(), R.layout.tablelayout, new String[] {
- // "name", "sex", "descripton" }, new int[] {
- // R.id.txtName, R.id.txtSex, R.id.txtdescription });
- // setListAdapter(simpleadapter);
-
- // BaseAdapter 使用
- // hasmapData = getHashMapData();
- // MyAdapter adpter = new MyAdapter(this);
- // setListAdapter(adpter);
-
- setContentView(R.layout.tablelayout); //继承 Activity 必须先加载布局文件、不然后面使用的View就不能找到。
- hasmapData = getHashMapData();
- MyAdapter adpter = new MyAdapter(getApplicationContext());
- ListView listView = (ListView) findViewById(R.id.listview);
- listView.setAdapter(adpter);
- } catch (Exception ex) {
- System.out.println(ex.getMessage());
- }
- }
- /**
- * 获取适配器的数据
- *
- * @return List<String>
- */
- public List<String> getData() {
- List<String> resultList = new ArrayList<String>();
- resultList.add("漠漠");
- resultList.add("非男");
- resultList.add("漠漠老撞树!");
- return resultList;
- }
- /**
- * 获取适配器的数据
- *
- * @return List<Map<String, Object>>
- */
- public List<Map<String, Object>> getHashMapData() {
- List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
- Map<String, Object> tempData0 = new HashMap<String, Object>();
- tempData0.put("name", "漠漠");
- tempData0.put("sex", "非男");
- tempData0.put("descripton", "漠漠老撞树!");
- resultList.add(tempData0);
- Map<String, Object> tempData1 = new HashMap<String, Object>();
- tempData1.put("name", "漠漠");
- tempData1.put("sex", "女");
- tempData1.put("descripton", "漠漠老迟到!");
- resultList.add(tempData1);
- Map<String, Object> tempData2 = new HashMap<String, Object>();
- tempData2.put("name", "漠漠");
- tempData2.put("sex", "其他");
- tempData2.put("descripton", "漠漠老喜欢帅哥啦!");
- resultList.add(tempData2);
-
- Map<String, Object> tempData3 = new HashMap<String, Object>();
- tempData3.put("name", "漠漠");
- tempData3.put("sex", "火星");
- tempData3.put("descripton", "漠漠老不淡定了。!");
- resultList.add(tempData3);
-
- Map<String, Object> tempData4 = new HashMap<String, Object>();
- tempData4.put("name", "漠漠");
- tempData4.put("sex", "地球");
- tempData4.put("descripton", "漠漠老撞树。!");
- resultList.add(tempData4);
- return resultList;
- }
- /**
- * 页面缓存
- * @author QWJ
- */
- public final class ViewHolder {
- public TextView name;
- public TextView sex;
- public TextView info;
- // public Button viewBtn;
- }
- /**
- * 自定义适配器
- * @author QWJ
- */
- public class MyAdapter extends BaseAdapter {
- private LayoutInflater mInflater;
- public MyAdapter(Context context) {
- mInflater = LayoutInflater.from(context); // 加载一个空页
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub 循环多少行。
- return hasmapData.size();
- }
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub 当前项
- return position;
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub 当前第几行
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub 返回一个View
- ViewHolder vh = null;
- try {
- //如果页面为null 创建一个缓存ViewHolder页并且设置Tag
- if (convertView == null) {
- vh = new ViewHolder();
- convertView = mInflater
- .inflate(R.layout.resultlayout, null);
- vh.name = (TextView) convertView.findViewById(R.id.txtName);
- vh.sex = (TextView) convertView.findViewById(R.id.txtSex);
- vh.info = (TextView) convertView
- .findViewById(R.id.txtdescription);
- convertView.setTag(vh);
- } else {
- //获取Tag
- vh = (ViewHolder) convertView.getTag();
- }
- //设置值
- vh.name.setText((String) hasmapData.get(position).get("name"));
- vh.sex.setText((String) hasmapData.get(position).get("sex"));
- vh.info.setText((String) hasmapData.get(position).get(
- "descripton"));
- } catch (Exception ex) {
- System.out.println(ex.getMessage());
- }
- return convertView;
- }
- }
- }
复制代码 页面 resultlayout.xml- <?xml version="1.0" encoding="utf-8"?>
- <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent" android:layout_height="match_parent"
- android:shrinkColumns="1" android:orientation="vertical" >
- <TableRow>
- <TextView android:id="@+id/txtName" android:layout_width="match_parent" android:layout_height="60px" android:text="姓名222"
- android:layout_weight="1" android:gravity="center"
- ></TextView>
- <TextView android:id="@+id/txtSex" android:layout_width="match_parent" android:layout_height="match_parent" android:text="性别222"
- android:layout_weight="1" android:gravity="center"
- ></TextView>
- <TextView android:id="@+id/txtdescription" android:layout_width="match_parent" android:layout_height="match_parent" android:text="描述333"
- android:layout_weight="1" android:gravity="center"
- ></TextView>
- </TableRow>
-
- </TableLayout>
复制代码 页面 resultlayout.xml- <?xml version="1.0" encoding="utf-8"?>
- <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent" android:layout_height="match_parent"
- android:shrinkColumns="1" android:orientation="vertical" >
- <TableRow>
- <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="姓名"
- android:layout_weight="1" android:gravity="center" android:background="#aa0000"
- ></TextView>
- <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="性别"
- android:layout_weight="1" android:gravity="center" android:background="#00aa00"
- ></TextView>
- <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="描述"
- android:layout_weight="1" android:gravity="center" android:background="#0000aa"
- ></TextView>
- </TableRow>
- <ListView
- android:id="@+id/listview"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </TableLayout>
复制代码 结果:
|
-
1
查看全部评分
-
|