package com.example.myapplication;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class BookAdapter extends BaseAdapter {
@SuppressWarnings("unused")
private Context context;
private LayoutInflater inflater; // 视图容器
private List<Map<String, Object>> listItem;
private String index[];
public final class ViewHolder {
public TextView tvId, tvBookname, tvWriter, tvPress, tvPrice;
}
/**
* @description 创建视图设置上下文
*/
public BookAdapter(Context context, List<Map<String, Object>> list,
String index[]) {
this.index = index;
this.context = context;
inflater = LayoutInflater.from(context);
this.listItem = list;
}
/**
* @description 子项目个数
*/
@Override
public int getCount() {
// TODO Auto-generated method stub
return listItem.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return listItem.get(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
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
String strId = listItem.get(position).get(index[0]) + "";
String strBookname = listItem.get(position).get(index[1]) + "";
String strWriter = listItem.get(position).get(index[2]) + "";
String strPress = listItem.get(position).get(index[3]) + "";
String strPrice = listItem.get(position).get(index[4]) + "";
// 获取文件视图
convertView = inflater.inflate(R.layout.book_item, null);
holder.tvId = (TextView) convertView
.findViewById(R.id.book_item_id);
holder.tvBookname = (TextView) convertView
.findViewById(R.id.book_item_bookname);
holder.tvWriter = (TextView) convertView
.findViewById(R.id.book_item_writer);
holder.tvPress = (TextView) convertView
.findViewById(R.id.book_item_press);
holder.tvPrice = (TextView) convertView
.findViewById(R.id.book_item_price);
// 设置字符串内容
holder.tvId.setText(strId);
holder.tvBookname.setText(strBookname);
holder.tvWriter.setText(strWriter);
holder.tvPress.setText(strPress);
holder.tvPrice.setText(strPrice);
// 控件到converview
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
}
package com.example.myapplication; public class BookBean { // 常量************************************ public final static String FILED_ID = "_id"; public final static String FILED_BOOKNAME = "bookname"; public final static String FILED_WRITER = "writer"; public final static String FILED_PRESS = "press"; public final static String FILED_PRICE = "price"; /** * id号 */ private int id; /** * 书名 */ private String bookname; /** * 作者 */ private String writer; /** * 出版社 */ private String press; /** * 价格 */ private double price; /** * @description 构造函数 * @return */ public BookBean(int id, String bookname, String writer, String press, double price) { this.id = id; this.bookname = bookname; this.writer = writer; this.press = press; this.price = price; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getBookname() { return bookname; } public void setBookname(String bookname) { this.bookname = bookname; } public String getWriter() { return writer; } public void setWriter(String writer) { this.writer = writer; } public String getPress() { return press; } public void setPress(String press) { this.press = press; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }
package com.example.myapplication; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class BookDao extends SQLiteOpenHelper { /** * 数据库名字 */ public static String DB_NAME = "lib.db"; /** * 表名 */ public static String TAB_NAME = "lib_book_tab"; /** * 版本号 */ public static int DB_VERSION = 1; /** * bookbean */ // private ArrayList<BookBean> booklist = new ArrayList<BookBean>(); public BookDao(Context context) { super(context, DB_NAME, null, DB_VERSION); } /** * @description 创建数据库 * @param context * 内容 * @param name * 数据库名字 * @param factory * CursorFactory指定在执行查询时获得一个游标实例的工厂类,设置为null,代表使用系统默认的工厂类 * @param version * 版本号 */ public BookDao(Context context, String name, CursorFactory factory, int version) { super(context, DB_NAME, factory, DB_VERSION); // TODO Auto-generated constructor stub } /** * @description 建表 */ @Override public void onCreate(SQLiteDatabase db) { Log.i("tag", "onCreate"); String sql = "create table " + TAB_NAME + " (" + BookBean.FILED_ID + " integer primary key autoincrement, " + BookBean.FILED_BOOKNAME + " varchar(20), " + BookBean.FILED_WRITER + " text, " + BookBean.FILED_PRESS + " varchar(20), " + BookBean.FILED_PRICE + " long)"; System.out.println("sql--->" + sql); db.execSQL(sql); } /** * @description 更新数据库 */ @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.i("tag", "onUpgrade"); String sql = "drop table if exist " + TAB_NAME; db.execSQL(sql); onCreate(db); } /** * @description 插入 */ public void insert(){ // SQLiteDatabase db=DB_NAME; } }
package com.example.myapplication; import android.content.Intent; import android.