zoukankan      html  css  js  c++  java
  • day03

    1 数据库介绍
        sqlite
        问:什么情况下用数据库? ----------有大量相似结构的数据需要存储的时候
        File file = new File("info.txt);

    2 数据库的创建
        定义一个类继承sqliteOpenHelper

    3 数据库的oncreate方法和onupgrade方法
        [1]使用SQLite Expert Professional这个工具可以打开我们创建的数据库
        [2]需要提前安装SQLite Expert Professional这个工具

    4 使用sql语句对数据库进行增删改查
        优点:多表查询容易
        缺点:<1>sql语句容易写错
                <2>执行SQL语句没有返回值 不容易进行判断

    4.1    [1]使用命令行工具sqlite3可以打开数据库
             [2]改变dos的编码方式 chcp 936 改变成GBK   编码方式  如果改成utf-8  chcp 65001 

    5 使用谷歌封装好的api对数据库增删改查
        优点    [1]写法简单    不需要写复杂的SQL语句  不容易写错
                   [2]有返回值方便开发者进行开发
        缺点    如果有多张表  使用谷歌封装的api不容易进行查询

    6 2种增删改查的优缺点

    7 Android中数据库的事务的介绍
        事务:执行一段逻辑  要么同时成功要么失败   银行转账
    1. //开启事务
    2. db.beginTransaction();
    3. try
    4. {
    5. //实现转账的逻辑
    6. //实际上就写sql语句
    7. db.execSQL("update info set money = money - 100 where name=?",new Object[]{"张三"});
    8. db.execSQL("update info set money = money + 100 where name=?",new Object[]{"李四"});
    9. //给当前事务设置一个成功的标记
    10. db.setTransactionSuccessful();
    11. }
    12. catch(Exception e)
    13. {
    14. Toast.makeText(getApplicationContext(), "服务器忙,请稍后再转", 1).show();
    15. }
    16. finally
    17. {
    18. //关闭事务
    19. db.endTransaction();
    20. }
    8 listview入门
        [1]定义listview在布局中
    1. <ListView
    2. android:id="@+id/lv"
    3. android:layout_width="match_parent"
    4. android:fastScrollEnabled="true"
    5. android:layout_height="match_parent" //要用match_parent,优化
    6. >

        [2]定义listview的数据适配器
    1. //[1]找到我们关心的控件
    2. ListView lv = (ListView)findViewById(R.id.lv);
    3. //[2]显示数据 和 其他普通控件(textview)有点区别数据来源于数据适配器
    4. lv.setAdapter(new MyListAdapter());

        [3]实现baseAdapter的getCount方法和getView方法
    1. //[3]定义listview的数据适配器
    2. private class MyListAdapter extends BaseAdapter
    3. {
    4. //一共有多少条数据需要展示
    5. public int getCount()
    6. {
    7. return 1000000000;
    8. }
    9. //返回指定position位置对应的对象
    10. public Object getItem(int position)
    11. {
    12. return null;
    13. }
    14. //返回position位置对应id
    15. public long getItemId(int position)
    16. {
    17. return 0;
    18. }
    19. /**
    20. *
    21. * 获取一个view 用来显示listview的数据 会作为listview的一个条目出现
    22. *
    23. * converView 历史缓存对象
    24. */
    25. public View getView(int position, View converView, ViewGroup parent)
    26. {
    27. TextView tv;
    28. if(converView == null)
    29. {
    30. //创建新的view对象
    31. tv = new TextView(MainActivity.this);
    32. System.out.println("创建新的view对象----" + position);
    33. }
    34. else
    35. {
    36. System.out.println("复用历史缓存对象----" + position);
    37. //若历史缓存对象存在,则用历史缓存对象赋值
    38. tv = (TextView) converView;
    39. }
    40. //System.out.println("getView---" + position);
    41. tv.setText("hahahahha" + position);
    42. return tv;
    43. }
    44. }


    9 listview的优化
        Out of memory on a 108-byte allocation(内存溢出)  使用converView 历史缓存对象
    1. public View getView(int position, View converView, ViewGroup parent)
    2. {
    3. TextView tv;
    4. if(converView == null)
    5. {
    6. //创建新的view对象
    7. tv = new TextView(MainActivity.this);
    8. System.out.println("创建新的view对象----" + position);
    9. }
    10. else
    11. {
    12. System.out.println("复用历史缓存对象----" + position);
    13. //若历史缓存对象存在,则用历史缓存对象赋值
    14. tv = (TextView) converView;
    15. }
    16. //System.out.println("getView---" + position);
    17. tv.setText("hahahahha" + position);
    18. return tv;
    19. }

    10 listview显示数据的原理
           mvc模式
           Android:
           m:mode   数据(javabean)
           v:view    listview
           c:adapter
        

    补充 listview的奇怪现象
        以后再使用listview 高度的设置使用 填充父窗体(match_parent)
        不管是什么adapter 作用就是把数据展示到listview

    11 listview显示复杂的页面
        线性布局    相对布局都继承自ViewGroup    可以有自己的孩子
        通过一个打气筒    inflate可以把一个布局转换成有一个view对象


    12 获取打气筒常用api
        [1]view = View.inflate(getApplicationContext(), R.layout.item, null);
        [2]view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item, null);
        [3]LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.item, null);

    13 ArrayAdapter的使用
    1. String objects[] = {"老张","老方","老黎","老毕","老刘","老韩"};
    2. //[1]找到我们关心的控件
    3. ListView lv = (ListView) findViewById(R.id.lv);
    4. //[2]创建一个arrayAdapter
    5. ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.item, objects);
    6. //ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.item1, R.id.tv_name, objects);
    7. //[3]设置数据适配器
    8. lv.setAdapter(adapter);
        
    权重一般用在LinearLayout布局

    14 simpleAdapter使用
    1. //[1]找到控件
    2. ListView lv = (ListView) findViewById(R.id.lv);
    3. //[1.1]准备listview要显示的数据
    4. List<Map<String, String>> data = new ArrayList<Map<String,String>>();
    5. Map<String, String> map1 = new HashMap<String, String>();
    6. map1.put("name", "官朝辉");
    7. map1.put("phone", "1377888");
    8. Map<String, String> map2 = new HashMap<String, String>();
    9. map2.put("name", "赵云");
    10. map2.put("phone", "999");
    11. Map<String, String> map3 = new HashMap<String, String>();
    12. map3.put("name", "刘备");
    13. map3.put("phone", "1371237888");
    14. Map<String, String> map4 = new HashMap<String, String>();
    15. map4.put("name", "小乔");
    16. map4.put("phone", "2222222");
    17. //[1.1]把map加入到集合中
    18. data.add(map1);
    19. data.add(map2);
    20. data.add(map3);
    21. data.add(map4);
    22. //[2]设置数据设备器
    23. //resource 我们定义的布局文件
    24. // from map集合的键
    25. // to 布局里的控件
    26. SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), data, R.layout.item, new String[]{"name","phone"}, new int[]{R.id.tv_name,R.id.tv_phone});
    27. //[3]设置数据适配器
    28. lv.setAdapter(adapter);

    15 把数据库里面的数据查询出来展示到listview上
    1. package com.phone.sqliteapi;
    2. import java.util.ArrayList;
    3. import java.util.List;
    4. import android.app.Activity;
    5. import android.content.ContentValues;
    6. import android.database.Cursor;
    7. import android.database.sqlite.SQLiteDatabase;
    8. import android.os.Bundle;
    9. import android.view.LayoutInflater;
    10. import android.view.View;
    11. import android.view.ViewGroup;
    12. import android.widget.BaseAdapter;
    13. import android.widget.ListView;
    14. import android.widget.TextView;
    15. import android.widget.Toast;
    16. public class MainActivity extends Activity {
    17. private MyOpenHelper myOpenHelper;
    18. private List<Person> lists;
    19. private ListView lv;
    20. @Override
    21. protected void onCreate(Bundle savedInstanceState) {
    22. super.onCreate(savedInstanceState);
    23. setContentView(R.layout.activity_main);
    24. //[0]找到控件
    25. lv = (ListView) findViewById(R.id.lv);
    26. myOpenHelper = new MyOpenHelper(getApplicationContext());
    27. lists = new ArrayList<Person>();
    28. //打开或者创建数据库 如果是第一次就是创建
    29. //SQLiteDatabase sqLiteDatabase = myOpenHelper.getWritableDatabase();
    30. //打开或者创建数据库 如果是第一次就是创建 如果磁盘满了 返回只读的
    31. //SQLiteDatabase readableDatabase = myOpenHelper.getReadableDatabase();
    32. }
    33. //点击按钮增加一条记录
    34. public void click1(View v)
    35. {
    36. //[1]获取数据库对象
    37. SQLiteDatabase db = myOpenHelper.getReadableDatabase();
    38. //[2]执行增加一条的SQL语句
    39. //db.execSQL("insert into info(name,phone) values(?,?)", new Object[]{"张三","1388888"});
    40. /**
    41. * table 表名
    42. * ContentValues 内部封装了一个map key:对应列的名字i value对应的值
    43. */
    44. ContentValues values = new ContentValues();
    45. values.put("name", "王五");
    46. values.put("phone", "110");
    47. //返回值代表插入 新行的id
    48. long insert = db.insert("info", null, values);
    49. //[3]数据库用完需要关闭
    50. db.close();
    51. if(insert > 0)
    52. {
    53. Toast.makeText(getApplicationContext(), "添加成功", 1).show();
    54. }
    55. else
    56. {
    57. Toast.makeText(getApplicationContext(), "添加失败", 1).show();
    58. }
    59. }
    60. //点击按钮删除一条记录
    61. public void click2(View v)
    62. {
    63. SQLiteDatabase db = myOpenHelper.getWritableDatabase();
    64. //db.execSQL("delete from info where name=?",new Object[]{"张三"});
    65. //返回值代表影响的行数
    66. int delete = db.delete("info", "name=?", new String[]{"王五"});
    67. db.close();
    68. Toast.makeText(getApplicationContext(), "删除了" + delete + "行", 1).show();
    69. }
    70. //点击按钮更新一条记录
    71. public void click3(View v)
    72. {
    73. SQLiteDatabase db = myOpenHelper.getWritableDatabase();
    74. //db.execSQL("update info set phone=? where name=?", new Object[]{"1377777777","张三"});
    75. ContentValues values = new ContentValues();
    76. values.put("phone", "119");
    77. //代表更新了多少行
    78. int update = db.update("info", values, "name=?", new String[]{"王五"});
    79. db.close();
    80. Toast.makeText(getApplicationContext(), "更新了" + update + "行", 1).show();
    81. }
    82. //点击按钮查找记录
    83. public void click4(View v)
    84. {
    85. SQLiteDatabase db = myOpenHelper.getReadableDatabase();
    86. /**
    87. * columns 代表你要查询的列
    88. */
    89. //Cursor cursor = db.query("info", new String[]{"phone"}, "name=?", new String[]{"王五"}, null, null, null);
    90. Cursor cursor = db.query("info", null, null, null, null, null, null);
    91. //Cursor cursor = db.rawQuery("select * from info", null);
    92. if(cursor != null && cursor.getCount() > 0)
    93. {
    94. while(cursor.moveToNext())
    95. {
    96. //columnIndex代表列的索引
    97. //String name = cursor.getString(1);
    98. //String phone = cursor.getString(2);
    99. //System.out.println("name:" + name + "-----" + phone);
    100. String name = cursor.getString(1);
    101. String phone = cursor.getString(2);
    102. //把数据封装到javabean
    103. Person person = new Person();
    104. person.setName(name);
    105. person.setPhone(phone);
    106. //把javabean加入到集合
    107. lists.add(person);
    108. }
    109. //设置数据适配器
    110. lv.setAdapter(new MyAdapter());
    111. }
    112. cursor.close();
    113. }
    114. private class MyAdapter extends BaseAdapter
    115. {
    116. @Override
    117. public int getCount() {
    118. // TODO Auto-generated method stub
    119. return lists.size();
    120. }
    121. @Override
    122. public Object getItem(int position) {
    123. // TODO Auto-generated method stub
    124. return null;
    125. }
    126. @Override
    127. public long getItemId(int position) {
    128. // TODO Auto-generated method stub
    129. return 0;
    130. }
    131. @Override
    132. public View getView(int position, View convertView, ViewGroup parent) {
    133. View view;
    134. if(convertView == null)
    135. {
    136. //创建新的view对象
    137. view = View.inflate(getApplicationContext(), R.layout.item, null);
    138. //view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item, null);
    139. //LayoutInflater systemService = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    140. //view = systemService.inflate(R.layout.item, null);
    141. }
    142. else
    143. {
    144. view = convertView;
    145. }
    146. TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
    147. TextView tv_phone = (TextView) view.findViewById(R.id.tv_phone);
    148. Person person = lists.get(position);
    149. tv_name.setText(person.getName());
    150. tv_phone.setText(person.getPhone());
    151. return view;
    152. }
    153. }
    154. }

    16今日总结
        [1]数据库如何创建(掌握)
            定义一个类继承SqliteOpenHelper
            sqliteDatabase:操作数据库
       
        oncreate方法:当数据库第一次创建的时候调用  特别适合做表结构的初始化
        onupgrade方法:当数据库版本进行更新的时候调用    

        [2]第一种方式对数据库进行增删改查(练习)
            传统的SQL语句
        [3]谷歌工程师给我们提供操作数据库的api(掌握)
        [4]命令行工具可以打开数据库 sqlite3(练习)
        [5]数据库的事务(练习)
        [6]listview 显示数据 需要数据适配器(掌握)
        [7]baseAdapter(掌握)
        [8]获取打气筒的常见api(掌握)
            <1>view = View.inflate(getApplicationContext(), R.layout.item, null);
            <2>view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item, null); 
            <3>LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.item, null);
        [9]ArrayAdapter(掌握)
        [10]simpleAdapter(了解)







    只言片语任我说,提笔句句无需忖。落笔不知寄何人,唯有邀友共斟酌。
  • 相关阅读:
    盒子跟随鼠标移动而移动(兼容IE8)
    JavaScript捕获鼠标坐标
    JavaScript判断滚动条是否滚动到底部
    JavaScript自定义getStyle方法获取元素样式
    JavaScript实现简单的图片切换功能
    SpeedReader
    JavaScript定时器
    远程登陆ubantu服务器 .bashrc文件每次打开终端都需要source的问题
    阿里云服务器Ubantu16.04升级python3.6
    阿里云服务器ubantu创建新用户登录显示问题
  • 原文地址:https://www.cnblogs.com/phonecom/p/5bd7fa3735c7b5a7ebdfb72b20609cf3.html
Copyright © 2011-2022 走看看