Android---MVP设计模式高级(三)

点击打开链接,下载demo即可。。。


Android---MVP设计模式初级(一)

Android---MVP设计模式中级(二)


页面的布局样式如下:

Android---MVP设计模式高级(三)_第1张图片Android---MVP设计模式高级(三)_第2张图片

Android---MVP设计模式高级(三)_第3张图片


先看数据库部分

DataBaseHelper这个类主要做的事情是:创建数据库、数据库表

package com.dandy.sqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.TextUtils;

public class DataBaseHelper extends SQLiteOpenHelper {

	/**
	 * 数据库名字
	 */
	private static final String DATABASE_NAME = "dandy.db";

	/**
	 * 学生信息表名
	 */
	protected static final String TABLE_NAME_STUDENT = "students";

	/**
	 * 老师信息表
	 */
	protected static final String TABLE_NAME_TEACHER = "teachers";

	/**
	 * 在构造器里面,创建数据库的名字
	 */
	public DataBaseHelper(Context context, int version) {
		this(context, DATABASE_NAME, null, version);
	}

	public DataBaseHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, DATABASE_NAME, factory, version);
	}

	/**
	 * 创建数据库
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
		createTables(db);
	}

	/**
	 * 数据库升级回调方法
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		if (oldVersion < newVersion) {

			upgradeTables_method1(db, oldVersion);
		}
	}

	/**
	 * 表创建
	 */
	private void createTables(SQLiteDatabase db) {
		createStudentTable(db);
	}

	/**
	 * 学生信息表的创建 public static final String CTEATETABLE =
	 * "create table if not exists "; public static final String ID_AUTO =
	 * " integer primary key autoincrement,"; public static final String TEXT =
	 * " text "; public static final String TEXT_NOT_NULL = "text not null";
	 * public static final String LEFT_PARENTHESES = "("; public static final
	 * String RIGHT_PARENTHESES = ")"; public static final String COMMA = ",";
	 * public static final String ALTER = "alter table "; public static final
	 * String RENAME = " rename to "; public static final String INSERT =
	 * "insert into "; public static final String DROPTABLE =
	 * "drop table if exists "; public static final String SELECT = " select ";
	 * public static final String ADD = " add "; public static final String FROM
	 * = " from "; public static final String SPACE = " "; public static final
	 * String DEFAULT = " default ";
	 * 
	 * sql拼接的语句如下: create table if not exists students(id integer primary key
	 * autoincrement,name text,age text,sex default 0)
	 */
	private void createStudentTable(SQLiteDatabase db) {

		StringBuffer createStudentTableSql = new StringBuffer();
		createStudentTableSql.append(ConstantString.CTEATETABLE)
				.append(TABLE_NAME_STUDENT)
				.append(ConstantString.LEFT_PARENTHESES).append(BaseColum.ID)
				.append(ConstantString.ID_AUTO).append(BaseColum.NAME)
				.append(ConstantString.TEXT).append(ConstantString.COMMA)
				.append(BaseColum.AGE).append(ConstantString.TEXT)
				.append(ConstantString.COMMA).append(BaseColum.SEX)
				.append(ConstantString.DEFAULT).append("0")
				.append(ConstantString.RIGHT_PARENTHESES);

		db.execSQL(createStudentTableSql.toString());
	}

	/**
	 * 老师信息表的创建
	 */
	private void createTeacherTable(SQLiteDatabase db) {
		StringBuffer createTeacherTableSql = new StringBuffer();
		createTeacherTableSql.append(ConstantString.CTEATETABLE)
				.append(TABLE_NAME_TEACHER)
				.append(ConstantString.LEFT_PARENTHESES).append(BaseColum.ID)
				.append(ConstantString.ID_AUTO).append(BaseColum.NAME)
				.append(ConstantString.TEXT).append(ConstantString.COMMA)
				.append(BaseColum.AGE).append(ConstantString.TEXT)
				.append(ConstantString.RIGHT_PARENTHESES);
		db.execSQL(createTeacherTableSql.toString());
	}

	/**
	 * 数据库升级,确定相邻的2个版本之间的差异,数据库可以依次迭代,如:V1-->V2,V2-->V3
	 * 
	 * 优点:每次更新数据库的时候只需要在onUpgrade方法的末尾加一段从上个版本升级到新版本的代码,易于理解和维护
	 * 
	 * 缺点:当版本变多之后,多次迭代升级可能需要花费不少时间,增加用户等待
	 */
	private void upgradeTables_method1(SQLiteDatabase db, int oldVersion) {
		int upgradeVersion = oldVersion;

		// V1-->V2
		if (upgradeVersion == 1) {
			upgradeTables1(db);
			upgradeVersion = 2;
		}

		// V2-->V3
		if (upgradeVersion == 2) {
			upgradeTables2(db);
		}
	}

	/**
	 * 数据库第一次升级,实现增加一张新表(如:teachers)
	 */
	private void upgradeTables1(SQLiteDatabase db) {
		createTeacherTable(db);
	}

	/**
	 * 数据库第二次升级,新增表的列数(students表中增加address列)
	 */
	private void upgradeTables2(SQLiteDatabase db) {
		try {
			db.beginTransaction();
			addTableColums(db, TABLE_NAME_STUDENT, BaseColum.ADD, "text",
					"China");
			db.setTransactionSuccessful();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			db.endTransaction();
		}
	}

	/**
	 * 增加数据库表中字段
	 * 
	 * @param db
	 * @param table
	 *            :表名
	 * @param coloum
	 *            :字段名
	 * @param type
	 *            :字段属性
	 * @param def
	 *            :字段默认值
	 */
	private void addTableColums(SQLiteDatabase db, String table, String colum,
			String type, String def) {
		try {
			StringBuffer addSql = new StringBuffer();
			addSql.append(ConstantString.ALTER)
					.append(table)
					.append(ConstantString.ADD)
					.append(colum)
					.append(type.startsWith(ConstantString.SPACE) ? type
							: ConstantString.SPACE + type);
			if (!TextUtils.isEmpty(def)) {
				if (def.contains("default")) {
					addSql.append(def.startsWith(ConstantString.SPACE) ? def
							: ConstantString.SPACE + def);
				} else {
					addSql.append(ConstantString.DEFAULT).append(def);
				}
			}
			db.execSQL(addSql.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 学生信息表字段名
	 */
	static class BaseColum {
		public static final String ID = "id";
		public static final String NAME = "name";
		public static final String AGE = "age";
		public static final String SEX = "sex";
		public static final String ADD = "address";
	}

	/**
	 * 字符串常量
	 */
	static class ConstantString {
		public static final String CTEATETABLE = "create table if not exists ";
		public static final String ID_AUTO = " integer primary key autoincrement,";
		public static final String TEXT = " text ";
		public static final String TEXT_NOT_NULL = "text not null";
		public static final String LEFT_PARENTHESES = "(";
		public static final String RIGHT_PARENTHESES = ")";
		public static final String COMMA = ",";
		public static final String ALTER = "alter table ";
		public static final String RENAME = " rename to ";
		public static final String INSERT = "insert into ";
		public static final String DROPTABLE = "drop table if exists ";
		public static final String SELECT = " select ";
		public static final String ADD = " add ";
		public static final String FROM = " from ";
		public static final String SPACE = " ";
		public static final String DEFAULT = " default ";
	}

}

AbstractSQLManager 这个类的方法是提供了数据库关闭,打开的操作

package com.dandy.sqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.MvpApplication;
import com.dandy.util.AppUtil;

/**
 * 这个类作用: 提供数据库的关闭,打开操作
 */
public class AbstractSQLManager {

	private DataBaseHelper mDataBaseHelper;

	private SQLiteDatabase mSQLitedb;

	public AbstractSQLManager() {
		openDataBase(MvpApplication.getApplication());
	}

	/**
	 * 方法作用:
	 * 创建数据库名字(有的话,就不会创建了)、并且获取getWritableDatabase
	 */
	private void openDataBase(Context context) {
		if (mDataBaseHelper == null) {
			mDataBaseHelper = new DataBaseHelper(context,
					AppUtil.getVersionCode(context));
		}
		if (mSQLitedb == null) {
			mSQLitedb = mDataBaseHelper.getWritableDatabase();
		}
	}

	/**
	 * @return
	 * 创建数据库名字(有的话,就不会创建了)、并且获取getWritableDatabase
	 */
	protected final SQLiteDatabase sqliteDB() {
		if (mSQLitedb == null) {
			openDataBase(MvpApplication.getApplication());
		}
		return mSQLitedb;
	}

	/**
	 * 数据库关闭
	 */
	public void close() {
		try {
			if (mSQLitedb != null && mSQLitedb.isOpen()) {
				mSQLitedb.close();
				mSQLitedb = null;
			}
			if (mDataBaseHelper != null) {
				mDataBaseHelper.close();
				mDataBaseHelper = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

上个类中需要获取全局变量,我是自定义了一个application(如果有好的方法,给我留言,多谢)

MvpApplication

package com;

import android.app.Application;

public class MvpApplication extends Application{
	
	private static MvpApplication mApplication;
	
	@Override
	public void onCreate() {
		super.onCreate();
		MvpApplication.mApplication = this;
	}

	
	public static Application getApplication(){
		return mApplication;
	}
}	
还需要一个获取版本号的工具类

AppUtil

package com.dandy.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

import com.MvpApplication;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Environment;

public class AppUtil {
	
	private static final String TAG = AppUtil.class.getSimpleName();
	
	/**
	 * 获取应用当前版本号
	 * @param mContext 
	 */
	public static int getVersionCode(Context mContext) {
		int versionCode = 1;
		if(mContext == null) {
			return versionCode;
		}
		try {
			PackageInfo packageInfo = mContext.getPackageManager().getPackageInfo(
					mContext.getPackageName(), 0);
			versionCode = packageInfo.versionCode;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return versionCode;
	}
	
	/**
	 * 拷贝数据库到sd卡
	 * 
	 * @deprecated 
	 */
	public static void copyDataBaseToSD(){
		 if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
             return ;
          }
		 File dbFile = new File(MvpApplication.getApplication().getDatabasePath("dandy")+".db");
		 File file  = new File(Environment.getExternalStorageDirectory(), "seeker.db");
		 
		 FileChannel inChannel = null,outChannel = null;
		 
		 try {
			file.createNewFile();
			inChannel = new FileInputStream(dbFile).getChannel();
			outChannel = new FileOutputStream(file).getChannel();
			inChannel.transferTo(0, inChannel.size(), outChannel);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if (inChannel != null) {
					inChannel.close();
					inChannel = null;
				}
				if(outChannel != null){
					outChannel.close();
					outChannel = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
}


StudentSQLManager--这个类的作用是:提供一些业务方法

package com.dandy.sqlite;

import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.database.Cursor;
import android.text.TextUtils;
import com.dandy.bean.Student;
import com.dandy.sqlite.DataBaseHelper.BaseColum;

public class StudentSQLManager extends AbstractSQLManager{
	
	private static StudentSQLManager instance;
	
	public static StudentSQLManager getInstance(){
		if(instance == null){
			synchronized (StudentSQLManager.class) {
				if(instance == null){
					instance = new StudentSQLManager();
				}
			}
		}
		return instance;
	}
	
	/**
	 * 学生信息的插入
	 * @param student 
	 */
	public void insertStudent(Student student){
		if(student == null){
			return;
		}
		ContentValues values = null;
		try {
			values = new ContentValues();
			values.put(BaseColum.NAME, student.getName());
			values.put(BaseColum.AGE, student.getAge());
			if(!TextUtils.isEmpty(student.getSex())){
				values.put(BaseColum.SEX, student.getSex());
			}
			sqliteDB().insert(DataBaseHelper.TABLE_NAME_STUDENT, null, values);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(values != null){
				values.clear();
				values = null;
			}
		}
	}
	
	/**
	 * 查找所有学生的信息 
	 */
	public List queryAllStudent(){
		List students = new ArrayList();
		Cursor cursor = null;
		try {
			cursor = sqliteDB().query(DataBaseHelper.TABLE_NAME_STUDENT, null, null, null, null, null, null);
			if(cursor != null && cursor.getCount() >0 && cursor.moveToFirst()){
				do {
					Student student = new Student();
					student.setId(cursor.getString(cursor.getColumnIndexOrThrow(BaseColum.ID)));
					student.setName(cursor.getString(cursor.getColumnIndexOrThrow(BaseColum.NAME)));
					student.setAge(cursor.getString(cursor.getColumnIndexOrThrow(BaseColum.AGE)));
					student.setSex(cursor.getString(cursor.getColumnIndexOrThrow(BaseColum.SEX)));
					students.add(student);
				} while (cursor.moveToNext());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(cursor != null){
				cursor.close();
				cursor = null;
			}
		}
		return students;
	}
	
	/**
	 * 删除学生信息
	 *  
	 * @param id
	 */
	public int deleteStudent(String id){
		try {
			return sqliteDB().delete(DataBaseHelper.TABLE_NAME_STUDENT, BaseColum.ID+"=?", new String[]{id});
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}
	
	/**
	 * 更新学生信息
	 * 
	 * @param student
	 */
	public int updateStudent(Student student){
		ContentValues values = null;
		try {
			values = new ContentValues();
			values.put(BaseColum.NAME, student.getName());
			values.put(BaseColum.AGE, student.getAge());
			if(!TextUtils.isEmpty(student.getSex())){
				values.put(BaseColum.SEX, student.getSex());
			}
			return sqliteDB().update(DataBaseHelper.TABLE_NAME_STUDENT, values, BaseColum.ID+"=?", new String[]{student.getId()});
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}
}


接下来看M层

Student

package com.dandy.bean;

public class Student {
	
	private String id;
	private String name;
	private String age;
	private String sex;//male(0),female(1)
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", sex=" + sex + "]";
	}
	
}

M层对外提供的方法StudentModel

package com.dandy.bean;

import java.util.List;
import com.dandy.bean.Student;
import com.dandy.sqlite.StudentSQLManager;

public class StudentModel {

	private StudentSQLManager manager;
	
	public StudentModel() {
		this.manager = StudentSQLManager.getInstance();
	}
	
	public void insertStudent(Student student) {
		manager.insertStudent(student);
	}

	public List queryAllStudents() {
		return manager.queryAllStudent();
	}

	public List queryStudentBySex(String sex) {
		return null;
	}

	public int deleteStudent(String id) {
		return manager.deleteStudent(id);
	}

	public int updateStudent(Student student) {
		return manager.updateStudent(student);
	}

}

接下来看V层

IStudentView

package com.dandy.view;

import java.util.List;
import com.dandy.bean.Student;

public interface IStudentView {
	
	/**
	 * 所有学生信息的获取展示
	 * @params students 
	 */
	void showAllStudents(List students);
	
	/**
	 * 学生信息的删除
	 * @params stident
	 */
	void deleteStudent(Student student);
	
	/**
	 * 更新学生信息 
	 */
	void updateStudent(Student student);
	
	/**
	 * 获取学生姓名 
	 */
	String getName();
	
	/**
	 * 获取学生年龄 
	 */
	String getAge();
	
	/**
	 * 获取学生性别 
	 */
	String getSex();
}

接下来看P层

StudentPresenter

package com.dandy.presenter;

import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.widget.Toast;

import com.dandy.bean.Student;
import com.dandy.bean.StudentModel;
import com.dandy.view.IStudentView;

public class StudentPresenter {

	private Context mContext;
	private IStudentView mStudentView;
	private StudentModel mStudentModel;

	public StudentPresenter(Context mContext, IStudentView mStudentView) {
		this.mContext = mContext;
		this.mStudentView = mStudentView;
		this.mStudentModel = new StudentModel();
	}

	/**
	 * 保存学生的信息
	 * 
	 * @param student
	 */
	public void saveStudentInfo() {
		Student student = new Student();
		student.setName(mStudentView.getName());
		student.setAge(mStudentView.getAge());
		student.setSex(mStudentView.getSex());
		mStudentModel.insertStudent(student);
		Toast.makeText(mContext, student.toString(), Toast.LENGTH_SHORT).show();
	}

	/**
	 * 加载学生信息
	 */
	public void loadAllStudent() {
		List students = mStudentModel.queryAllStudents();
		mStudentView.showAllStudents(students);
	}

	/**
	 * 根据性别加载学生信息
	 * 
	 * @param sex
	 */
	public void loadStudentsBySex(String sex) {
		if (TextUtils.isEmpty(sex)) {
			return;
		}
		List students = mStudentModel.queryStudentBySex(sex);
		mStudentView.showAllStudents(students);
	}

	/**
	 * 删除学生的信息
	 * 
	 * @param student
	 */
	public void deleteStudent(Student student) {
		if (student == null) {
			return;
		}
		String id = student.getId();
		if (TextUtils.isEmpty(id)) {
			return;
		}
		if (mStudentModel.deleteStudent(student.getId()) != -1) {
			mStudentView.deleteStudent(student);
		}
	}

	/**
	 * 更新学生信息
	 * 
	 * @param student
	 */
	public void updateStudent(Student student) {
		if (student == null) {
			return;
		}
		if (mStudentModel.updateStudent(student) != -1) {
			mStudentView.updateStudent(student);
		}
	}
}


最后看主界面MainActivity

package com.example.mvpdemo;

import java.util.ArrayList;
import java.util.List;
import com.dandy.adapter.StudentAdapter;
import com.dandy.bean.Student;
import com.dandy.presenter.StudentPresenter;
import com.dandy.util.AppUtil;
import com.dandy.view.IStudentView;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity implements IStudentView,OnClickListener,OnItemLongClickListener{

	private StudentPresenter mStudentPresenter;
	
	private EditText name,age,sex;
	private ListView studentList;
	
	private List mStudents = new ArrayList();
	private StudentAdapter mStudentAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mStudentPresenter = new StudentPresenter(this, this);
		
		findViewById(R.id.insert).setOnClickListener(this);
		findViewById(R.id.query).setOnClickListener(this);
		findViewById(R.id.copy).setOnClickListener(this);
		
		name = (EditText) findViewById(R.id.name);
		age = (EditText) findViewById(R.id.age);
		sex = (EditText) findViewById(R.id.sex);
		studentList = (ListView) findViewById(R.id.student_list);
		
		mStudentAdapter = new StudentAdapter(this, mStudents, R.layout.student_info_item_layout);
		studentList.setAdapter(mStudentAdapter);
		
		studentList.setOnItemLongClickListener(this);
	}

	/**
	 * implements IStudentView
	 */
	@Override
	public void showAllStudents(List students) {
		mStudents.clear();
		mStudents.addAll(students);
		mStudentAdapter.notifyDataSetChanged();
	}

	@Override
	public void deleteStudent(Student student) {
		mStudentPresenter.loadAllStudent();
	}

	@Override
	public void updateStudent(Student student) {
		mStudentPresenter.loadAllStudent();
	}
	
	@Override
	public String getName() {
		return name.getText().toString();
	}

	@Override
	public String getAge() {
		return age.getText().toString();
	}

	@Override
	public String getSex() {
		return sex.getText().toString();
	}
	
	@SuppressWarnings("deprecation")
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.insert:
			mStudentPresenter.saveStudentInfo();
			break;

		case R.id.query:
			mStudentPresenter.loadAllStudent();
			break;
		case R.id.copy:
			AppUtil.copyDataBaseToSD();
			break;
		}
	}

	/**
	 * 实现listView的item长按事件的回调方法
	 */
	@Override
	public boolean onItemLongClick(AdapterView parent, View view,int position, long id) {
		final Student student = mStudents.get(position);
		final Dialog dialog = new Dialog(this, R.style.DialogStyle);
		dialog.setContentView(R.layout.student_edit_layout);
		dialog.show();
		dialog.findViewById(R.id.delete).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mStudentPresenter.deleteStudent(student);
				dialog.cancel();
			}
		});
		dialog.findViewById(R.id.update).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mStudentPresenter.updateStudent(student);
				dialog.cancel();
			}
		});
		dialog.findViewById(R.id.cancle).setOnClickListener(new OnClickListener() {
	
			@Override
			public void onClick(View v) {
				dialog.cancel();
			}
		});
		return true;
	}
}

另外在看看通用的adapter的使用

ViewHolder

package com.dandy.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

public class ViewHolder{
	private final SparseArray mViews;
	private int mPosition;
	private View mConvertView;
	private int itemViewType;

	private ViewHolder(Context context, ViewGroup parent, int layoutId,int position,int itemViewType) {
		this.mPosition = position;
		this.itemViewType = itemViewType;
		this.mViews = new SparseArray();
		this.mConvertView = LayoutInflater.from(context).inflate(layoutId, parent,false);
		// setTag
		this.mConvertView.setTag(this);
	}

	/**
	 * 拿到一个ViewHolder对象
	 * @param context
	 * @param convertView
	 * @param parent
	 * @param layoutId
	 * @param position
	 * @return
	 */
	public static ViewHolder get(Context context, View convertView,ViewGroup parent, int layoutId, int position,int itemViewType) {
		if (convertView == null) {
			return new ViewHolder(context, parent, layoutId, position,itemViewType);
		}		
		ViewHolder viewHolder = (ViewHolder) convertView.getTag();
		if(viewHolder != null && viewHolder.getItemViewType() == itemViewType){
			return viewHolder;
		}else{
			viewHolder = null;
		}
		return viewHolder;
	}

	public int getItemViewType() {
		return itemViewType;
	}
	
	public View getConvertView() {
		return this.mConvertView;
	}

	/**
	 * 通过控件的Id获取对于的控件,如果没有则加入views
	 * @param viewId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public  T getView(int viewId) {
		View view = mViews.get(viewId);
		if (view == null) {
			view = mConvertView.findViewById(viewId);
			mViews.put(viewId, view);
		}
		return (T) view;
	}
	
	/**
	 *通过控件的Id控制控件是否显示
	 * @param viewId 
	 * @param visibility
	 */
	public void setVisibility(int viewId,int visibility){
		View view = getView(viewId);
		view.setVisibility(visibility);
	}
	
	/**
	 *通过控件的Id控制控件是否可点击
	 * @param viewId 
	 * @param visibility
	 */
	public void setClickable(int viewId,boolean clickable){
		View view = getView(viewId);
		view.setClickable(clickable);
	}
	
	/**
	 * 为TextView设置字符串
	 * @param viewId
	 * @param charSequence
	 * @param visible
	 * @return
	 */
	public void setText(int viewId, CharSequence charSequence) {
		TextView view = getView(viewId);
		view.setText(charSequence);
	}

	public void setText(int viewId, SpannableString text) {
		TextView view = getView(viewId);
		view.setText(text);
	}


	public void setImageBitmap(int viewId, Bitmap bm){
		ImageView view = getView(viewId);
		if(view != null && bm != null){
			view.setImageBitmap(bm);
		}
	}
	public void setImageDrawable(int viewId, Drawable draw){
		ImageView view = getView(viewId);
		if(view != null && draw != null){
			view.setImageDrawable(draw);
		}
	}
	
	/**
	 * 
	 */
	public void setEnable(int viewId, boolean enable){
		View view = getView(viewId);
		view.setEnabled(enable);
	}
	
	public void setChecked(int viewId, boolean checked){
		CompoundButton cb = getView(viewId);
		cb.setChecked(checked);
	}
	
	public int getPosition() {
		return mPosition;
	}
}


CommonAdapter

package com.dandy.adapter;

import java.util.Arrays;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

/**
 * 抽象类
 * 通用的adapter适配器
 */
public abstract class CommonAdapter extends BaseAdapter{
	
	public Context mContext;
	private List mDatas;
	private int [] itemLayoutIds;
	private int [] viewTypes;
	private static final int defaultViewType = 0x00;
	
	/**
	 * 
	 * @param context 环境
	 * @param mDatas 数据源
	 * @param itemLayoutId 对应布局格式的布局数量--本例只有1个
	 */
	public CommonAdapter(Context context, List mDatas, int itemLayoutId) {
		this.mContext = context;
		this.mDatas = mDatas;
		//布局格式数量--及其对应布局格式的布局数量本例都为1
		this.itemLayoutIds = new int[1];
		this.viewTypes = new int[1];
		//获取本例的布局格式、及其对应布局格式的布局数量
		this.itemLayoutIds[0] = itemLayoutId;
		this.viewTypes[0] = defaultViewType;
	}
	/**
	 * 
	 * @param context 环境变量
	 * @param mDatas 数据源
	 * @param itemLayoutIds 对应布局格式的布局数量
	 * @param viewTypes 布局格式数量
	 */
	public CommonAdapter(Context context, List mDatas,int [] itemLayoutIds,int [] viewTypes) {
		this.mContext = context;
		this.mDatas = mDatas;
		this.itemLayoutIds = itemLayoutIds;
		this.viewTypes = viewTypes;
	}
	@Override
	public int getCount() {
		return mDatas.size();
	}

	@Override
	public T getItem(int position) {
		return mDatas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	//返回你有多少个不同的布局
	@Override
	public int getViewTypeCount() {
		return viewTypes.length;
	}
	
	/**
	 * 多种布局格式需要重写此方法 
	 *
	 * 因为 The item view type you are returning from getItemViewType() is >= getViewTypeCount(),
	 * 所以type的最大值最好从0开始,然后++
	 */
	public int getItemViewType(int position,int [] viewTypes){
		return defaultViewType;
	};
	/**
	 * 
	 */
	@Override
	public int getItemViewType(int position) {
		return getItemViewType(position,viewTypes);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		int viewType = getItemViewType(position);
		int index = Arrays.binarySearch(viewTypes, viewType);
		
		ViewHolder viewHolder = getViewHolder(position, convertView,parent,itemLayoutIds[index],viewType);
		convert(viewHolder, getItem(position),viewType, position);
		return viewHolder.getConvertView();
	}

	public abstract void convert(ViewHolder viewHolder, T item,int itemViewType, int position);

	private ViewHolder getViewHolder(int position, View convertView,ViewGroup parent,int layoutId,int itemViewType) {
		return ViewHolder.get(mContext, convertView, parent,layoutId,position,itemViewType);
	}
}

StudentAdapter

package com.dandy.adapter;

import java.util.List;
import android.content.Context;
import com.dandy.bean.Student;
import com.example.mvpdemo.R;

public class StudentAdapter extends CommonAdapter{

	public StudentAdapter(Context context, List mDatas,int itemLayoutId) {
		super(context, mDatas, itemLayoutId);
	}

	@Override
	public void convert(ViewHolder viewHolder, Student item, int itemViewType,int position) {
		String sex = "";
		if(item.getSex().equals("0")){
			sex = "男";
		}else{
			sex = "女";
		}
		viewHolder.setText(R.id.student_info, item.getName()+"_"+item.getAge()+"_"+sex);
	}

}

activity_main.xml



    

    

    

    

        

student_info_item_layout




    



student_edit_layout




    

    

    




Android---MVP设计模式高级(三)_第4张图片


style.xml 就是定义dialog的样式的

 
    
    
    
    
    
    


更新的

selector_item_back_top.xml




    
    
    




    
    
        
            

            

            
        
    





    
    
        
            

            

            
        
    



删除




    
        
            
            
            
        
    
    
    
        
            
            
            
        
    
    
    
        
            
            
            
        


取消



    
        
            

            

            
        
    





    
        
            

            

            
        
    



尺寸


    
    16dp
    16dp

    10dp
    0.01dp
    12sp
    

颜色


    
    #cccccc
    #ffffff
    #e5e5e5




你可能感兴趣的:(Android基础)