赵雅智_ListView_BaseAdapter
<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=".MainActivity" > <ListView
android:id="@+id/lv_users"
android:layout_width="match_parent"
android:layout_height="match_parent"
> </ListView> </RelativeLayout>
<?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" > <TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="40dp"
android:layout_marginTop="10dp"
android:text="TextView" /> <TextView
android:id="@+id/tv_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_name"
android:layout_below="@+id/tv_name"
android:layout_marginTop="10dp"
android:text="TextView" /> <TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_name"
android:layout_marginLeft="30dp"
android:layout_toRightOf="@+id/tv_age"
android:text="TextView" /> <TextView
android:id="@+id/tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tv_name"
android:paddingLeft="10dp"
android:text="1" /> </RelativeLayout>
package com.example.android_sqlite; import java.util.List; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast; import com.example.android_sqlite.dao.UserDao;
import com.example.android_sqlite.dao.impl.UserDaoImpls;
import com.example.android_sqlite.database.DatabaseHelper;
import com.example.android_sqlite.domain.Users;
/**
* BaseAdapter
* @author zhaoyazhi
*
*/
public class MainActivity extends Activity implements OnItemClickListener {
private ListView lv_users;
// 模型层
private List<Users> entites;
private UserDao userDao;
private DatabaseHelper dh; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 设置视图层
lv_users = (ListView) findViewById(R.id.lv_users);
// 创建数据库
dh = new DatabaseHelper(this);
userDao = new UserDaoImpls(dh);
// 获取显示数据
entites = userDao.findAll();
// 创建自己定义的adapter对象
MyAdapyer adapter = new MyAdapyer();
// adapter作为控制层
lv_users.setAdapter(adapter); //注冊点击事件
lv_users.setOnItemClickListener(this); }
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) { //获取点击我的数据
Users object = (Users) parent.getItemAtPosition(position);
Toast.makeText(this, object.toString(), 0).show(); }
/**
* 继承了BaseAdapter 这个BaseAdapter实现了ListAdapter接口
*
* @author zhaoyazhi
*
*/
class MyAdapyer extends BaseAdapter { /**
* 返回的总条目数量
*/
@Override
public int getCount() {
return entites.size();
} /**
* 返回当前条目相应的对象
*/
@Override
public Object getItem(int position) {
return entites.get(position);
} /**
*
*/
@Override
public long getItemId(int position) {
return position;
} /**
* 返回每一个条目
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return definedItem(position);
} private View definedItem(int position) {
// 获取当前位置显示的对象
Users entity = entites.get(position);
View v = View.inflate(MainActivitys.this, R.layout.item_phone, null);
TextView tv_id = (TextView) v.findViewById(R.id.tv_id);
TextView tv_name = (TextView) v.findViewById(R.id.tv_name);
TextView tv_age = (TextView) v.findViewById(R.id.tv_age);
TextView tv_phone = (TextView) v.findViewById(R.id.tv_phone); tv_id.setText(""+entity.getUserId());
tv_name.setText("姓名:" + entity.getUserName());
tv_age.setText("年龄:" + entity.getUserAge());
tv_phone.setText("电话"+entity.getUserPhone());
return v;
} /**
* 单选框
*
* @param position
* @return
*/
private View singleCheck(int position) {
// 获取当前位置显示的对象
Users entity = entites.get(position);
View v = View.inflate(MainActivitys.this,
android.R.layout.simple_list_item_single_choice, null);
CheckedTextView tv = (CheckedTextView) v
.findViewById(android.R.id.text1);
tv.setText(entity.getUserName() + "---" + entity.getUserPhone());
tv.setHeight(40);
return tv;
} /**
* 复选框
*
* @param position
* @return
*/
private View multipleCheck(int position) {
// 获取当前位置显示的对象
Users entity = entites.get(position);
View v = View.inflate(MainActivitys.this,
android.R.layout.simple_list_item_multiple_choice, null);
CheckedTextView tv = (CheckedTextView) v
.findViewById(android.R.id.text1);
tv.setText(entity.getUserName() + "---" + entity.getUserPhone());
tv.setHeight(40);
return tv;
} } }
赵雅智_ListView_BaseAdapter的更多相关文章
- 赵雅智_ContentProvider
ContentProvider介绍 ContentProvider是不同应用程序之间进行交换数据的标志API 也就是说:一个应用程序通过ContentProvider暴露自己的数据操作接口,那么无论该 ...
- 赵雅智_Fragment生命周期
官网帮助文档链接: http://developer.android.com/guide/components/fragments.html 主要看两张图.和跑代码 一,Fragment的生命周 w ...
- 赵雅智:js知识点汇总
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhemhpMjEyOQ==/font/5a6L5L2T/fontsize/400/fill/I0 ...
- 赵雅智_BroadcastReceiver电话监听
AndroidManifest.xml 注冊广播接收者 加入权限 <?xml version="1.0" encoding="utf-8"?> &l ...
- 赵雅智_BroadcastReceiver
BroadcastReceiver 用于接收程序(包含用户开放的程序和系统内建程序)所发出的Broadcast intent 耗电量 开机启动 窃取别人短信 窃取别人电话 开发: 创建须要启动的Br ...
- 赵雅智:service_startService生命周期
案例演示 布局文件 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xm ...
- 赵雅智_BroadcastReceiver短信监听
AndroidManifest.xml 注冊广播接收者 加入权限 <?xml version="1.0" encoding="utf-8"?> &l ...
- 赵雅智_Android编码规范
凝视 导入mycodetemplates.xml统一凝视样式 须要加凝视的地方 类凝视(必加) 方法凝视(必加) 块凝视主要是数据结构和算法的描写叙述(必加) 类成员变量和常量凝视(选择性加入) 单行 ...
- 赵雅智:service与訪问者之间进行通信,数据交换
服务类 中间人:service服务中的bind对象 创建中间人并通过onBinder方法的return暴露出去 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQ ...
随机推荐
- set,multiset容器类型
set和multiset会根据特定的排序准则,自动将元素排序.两者不同处在于multiset允许元素重复而set不允许. 一.集和多集(set 和multiset 容器类) 在使用set和multis ...
- 【POJ2482】【线段树】Stars in Your Window
Description Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw ...
- 如何用angularjs制作一个完整的表格之五__完整的案例
由于本人也是边学边写,因此整理的比较乱,下面放出我例子的完整代码,方便大家交流测试,如有问题欢迎评论 首先,表格采用的是BootStrap样式编辑的,主要使用的是angularjs,为了方便也有jQu ...
- CodeIgniter框架——介绍
CodeIgniter 是一个应用程序框架 CodeIgniter 是一个为用 PHP 编写网络应用程序的人员提供的工具包.它的目标是实现让你比从零开始编写代码更快速地开发项目,为此,CI 提供了一套 ...
- <select>与<datalist>的区别
size:下拉框中每次出现选项的个数 multiple:可以一次性选多个选项: disabled:时下拉框不可用,无法点击选项 list:它的值应于id的值对应 datalist要与input标签一 ...
- TatukGIS-TGIS_LayerVector-LocateEx
方法原型: function LocateEx(const _ptg: TGIS_Point; const _prec: Double; const _uid: Integer; var _dist: ...
- python自动开发之第十三天
1.Paramiko模块下的demo.py程序 前面利用Python中的Paramiko模块可以进行SSH的连接,以及用来传送文件(SFTP),但是无论是哪一种方式,连接都是短暂的,并非是长连 ...
- 分布式系统间通信之RPC的基本概念(六)
RPC(Remote Procedure Call Protocol)远程过程调用协议.一个通俗的描述是:客户端在不知道调用细节的情况下,调用存在于远程计算机上的某个对象,就像调用本地应用程序中的对象 ...
- Mvc 页面缓存 OutputCache VaryByCustom
优化网站,dotNet MVC 可以通过(OutputCache)特性在某些Action上使用缓存,如果我们想要自定义缓存依据可以通过如下方式进行: 第一步, 在 global.asax.cs 文件中 ...
- 基于 Webpack & Vue & Vue-Router 的 SPA 初体验
基于 Webpack & Vue & Vue-Router 的 SPA 初体验 本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com ...