Android自定义适配器父类继承BaseAdapter,自定义生成对话框工具类
来源:互联网 发布:matlab量化投资源码 编辑:程序博客网 时间:2024/06/05 15:28
工作内容:
1.不使用SQLiteHelper获取本地数据库中的表的内容
2.自定义适配器父类
3.自定义生成对话框的工具类
学习分享:
1.不使用SQLiteHelper,直接找到本地数据库文件打开,并获取其中的表内容
SQLiteDatabase sqliteDatabase = SQLiteDatabase.openOrCreateDatabase(File databaseFile,null);
参数:databaseFile是指数据库文件的File对象
2.自定义适配器父类,继承BaseAdapter【方便复用】
在以后使用适配器时,只需要继承它就可以非常方便的构造适配器
实例1:2个适配器父类主要不同点在于构造函数不同,添加数据的方式不同
适配器父类【1】
import android.content.Context;import android.widget.BaseAdapter;import java.util.List;/** * Created by Administrator on 2016/8/11. * 适配器的泛型父类 */public abstract class BaseDataAdapter<E> extends BaseAdapter { protected List<E> list; protected int resourceID; protected Context context;// protected LayoutInflater layoutInflater; //用于解析出view /** * 构造函数 * @param context 上下文 * @param resourceID 资源文件ID(layout布局文件DD) * @param list 数据 */ public BaseDataAdapter(Context context,int resourceID, List<E> list) { this.context = context; this.resourceID = resourceID; this.list = list; //获取到layoutInflater// this.layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return list.size(); } @Override public E getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; }}
适配器父类【2】
import android.content.Context;import android.widget.BaseAdapter;import java.util.ArrayList;import java.util.List;/** * Created by Administrator on 2016/8/25. */public abstract class MyBaseAdapter<E> extends BaseAdapter { protected List<E> list = null; //数据 protected Context context = null; //上下文用于解析布局文件 protected int resourceID; //布局文件ID //构造函数 public MyBaseAdapter(Context context,int resourceID){ this.context = context; this.resourceID = resourceID; //在子类(适配器)中的getView方法中使用这两个参数得到解析过的View list = new ArrayList<>(); } /** * 设置数据 * 在Activity中调用,为适配器添加数据 * 在使用此方法后,需调用adapter.notifyDataSetChanged();刷新列表项 */ public void setData(List<E> addList){ this.list.addAll(addList); } @Override public int getCount() { return list.size(); } @Override public E getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } /** * 没有重写getView方法是原因 * 1.本类是个abstract类,可以不重写父类的抽象方法 * 2.每个适配器的getView方法都不同,不能在这里强些定制 */// @Override// public View getView(int position, View convertView, ViewGroup parent) {// return null;// }}
子类适配器使用:
实例2://Fruit是一个自定义类public class FruitAdapter extends MyBaseAdapter<Fruit> { public FruitAdapter(Context context, int resourceID) { super(context, resourceID); } @Override public View getView(int position, View convertView, ViewGroup parent) { //此处代码不再赘术,详情参考http://blog.csdn.net/yingtian648/article/details/52083194 return convertView; }}
import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.support.v7.app.AlertDialog;/** * Created by Administrator on 2016/8/11. * 用于生成对话框的工具类 */public class DialogTools { //不运行创建对象 private DialogTools(){} /** * 使用系统的对话框样式 * @param context 上下文(显示在哪个界面) * @param title 对话框标题 * @param message 对话框标题内容 * @param intent 点击确定按钮后跳转的页面 */ public static void open(final Context context, String title, String message, final Intent intent){ AlertDialog.Builder dialog = new AlertDialog.Builder(context); dialog.setTitle(title) .setMessage(message) .setPositiveButton("取消",null) .setNegativeButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { context.startActivity(intent); } }).setCancelable(true) .create(); dialog.show(); }}
1 0
- Android自定义适配器父类继承BaseAdapter,自定义生成对话框工具类
- android-继承BaseAdapter自定义适配器基础
- android BaseAdapter 自定义适配器 BaseAdapter基类抽取 一
- android BaseAdapter 自定义适配器 BaseAdapter基类抽取 二
- Android自定义适配器BaseAdapter
- android 自定义适配器BaseAdapter
- 自定义ListView适配器继承BaseAdapter
- android-继承BaseAdapter--自定义适配器,getView执行多次的解决方法
- android-继承BaseAdapter自定义适配器,getView执行多次的解决方法
- 自定义适配器的实现继承BaseAdapter
- android自定义对话框工具类
- 自定义ListView ,适配器(BaseAdapter)
- 自定义BaseAdapter适配器
- Android学习ListView自定义布局,适配器BaseAdapter
- Android自定义适配器,BaseAdapter的使用方法
- Android中自定义适配器BaseAdapter基本用法
- 关于Android自定义适配器BaseAdapter浅谈
- android 自定义适配器Adapter基类BaseAdapter
- UML图
- 从 DOCTYPE 和 CSS 盒模型讲起
- 《Spring敲门砖之基础教程第一季》 第一章 概要介绍
- Android开发之不用for,if,while计算1--100之间的和
- Java_数组续集
- Android自定义适配器父类继承BaseAdapter,自定义生成对话框工具类
- 【win32】day05-鼠标消息/定时器消息/菜单
- hdu 5773 (The All-purpose Zero)
- 文章推荐
- 数据结构实验之二叉树五:层序遍历
- 上证指数大盘分析
- cmd 执行oracle sql脚本
- I/O(input/output)是什么意思?
- 求二叉树的深度