Advertisement

android获取mysql数据_Android 数据库读取数据显示 [5]

阅读量:

2016-12-1 课程内容

昨天学了Android数据库升级、降级、创建

今天把数据库表里面的数据读取出来显示到手机屏幕上

下面代码是MainActivity.java 的代码

8f900a89c6347c561fdf2122f13be562.png
961ddebeb323a10fe0623af514929fc1.png

packagecom.example.winxinmff;importjava.sql.SQLException;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importandroid.app.Activity;importandroid.database.sqlite.SQLiteDatabase;importandroid.os.Bundle;importandroid.util.Log;importandroid.view.View;importandroid.widget.AdapterView;importandroid.widget.AdapterView.OnItemClickListener;importandroid.widget.ListView;importandroid.widget.SimpleAdapter;importandroid.widget.Toast;importcom.example.entity.Message;importcom.example.winxinmffSQL.R;importcom.j256.ormlite.dao.Dao;public class MainActivity extendsActivity {private SimpleAdapter sa; //是android中一个列表适配器 主要用于做一些简单的列表适配

该类是一个用于ORMLite演示的小程序类。
其名称"databasehelper"表明其作用就是完成基本的数据库操作。
该类包含必要的功能用于执行基本的增删查改操作。
由于可能是基础实现类, 该类还可能包含一些简化后的增删查改功能。
具体来说:
它支持数据库连接与关闭操作;
它提供了一些查询与更新功能;
它包含了基本的数据库 JDBC 操作方法;
可能还包括一些辅助功能, 如数据验证等。

privateDatabaseHelper db_helper;//首次创建时,会调用这个方法。

protected void onCreate(Bundle savedInstanceState) {
// 调用父类create方法
super.onCreate([something]);
}

这三条代码用于构建表架构并完成数据基础的初始化机制

MyDatabaseOpenHelper helper = newMyDatabaseOpenHelper(

MainActivity1.this);

SQLiteDatabase db=helper.getWritableDatabase();

db.close();

lv=(ListView) findViewById(R.id.listView1);

sa= new SimpleAdapter(this, messageList2,//data 不仅仅是数据,而是一个与界面耦合的数据混合体

R.layout.listview_item_layout, new String[] { "tou", "username", "lastMsg", "datetime" }, //from

new int[] { R.id.imageView1, R.id.tv_userName,

R.id.tv_lasMessage, R.id.tv_datetime }//to 到那里去

);

lv.setAdapter(sa);

lvursivelyCall被赋值为newOn...

Map item =messageList2.get(position);

item.put("userName", "赵本山" +position);

sa.notifyDataSetChanged();

}

});

}//UI呈现在眼前

protected voidonResume() {

Log.i("ok", "onResume,界面出现了");//使用ORMLite

db_helper = new DatabaseHelper(this);

Dao message_dao =db_helper.getMessageDao();try{

msgCount = message_dao.queryForAll(); // Log.i("ormlite", "统计数量:" + msgCount);

msgCount = message_dao.queryForAll(); // Log.i("ormlite", "统计数量:" + msgCount);

String t = "记录条数:" +messageList3.size();

Toast.makeText(this, t, Toast.LENGTH_LONG).show();

}catch(SQLException e) {

String t= "记出错:" +e.getMessage();

Toast.makeText(this, t, Toast.LENGTH_LONG).show();

}//模拟读取数据库或者互联网

for (int i = 0; i < messageList3.size(); i++) {

Message p= newMessage();

p.setTou1("xxx");

p.setUserName("不对外开放 高内聚 ");

p.setLastMessage("无命名嵌套类本质上就是指没有显式命名的嵌套类无命名嵌套类本质上就是指没有显式命名的嵌套类");

p.setDatetime("11.11");

messageList.add(p); // 上周 创建一个包含数据和界面元素的混合体, 每个item对应一条记录信息

Map item = new HashMap();//一行记录,包含多个控件

item.put("tou", R.drawable.a); //头像

item.put("userName", messageList3.get(i).getUserName() + i); //名字

item.put("lastMessage", messageList3.get(i).getLastMessage()); //内容

item.put("datetime", messageList3.get(i).getDatetime()); //时间

messageList2.add(item); //添加到集合

}super.onResume();

}//UI完全消失在眼前,完全被另外一个进程覆盖

protected voidonPause() {

Log.i("ok", "onPause,被抛弃");

db_helper.close();//??到底要不要?

super.onPause();

}

}

MainActivity

数据库助手类

8f900a89c6347c561fdf2122f13be562.png
961ddebeb323a10fe0623af514929fc1.png

This package belongs to com.example.winxinmff. It imports java.sql SQLException and provides related handling of Java SQL operations. The application accesses context information in Android applications through the android.content.Context import. It manages message entities of type com.example.message.Message using the imported com.example.entity.Message import. The class OrmLiteSqliteOrmLiteSqliteTransactionManager extends OrmLite SqliteTransactionManager for handling SQLite database operations within an Android application context. The class DatabaseHelper is defined as a helper class for managing SQLite database operations within an Android application, extending OrmLite SqliteTransactionManager and implementing specific database functionality. The private static final String DB_NAME is defined as "mydata.db" with a comment indicating its purpose as the database name for storage of application data.

private static final int version号为1;
//数据库版本标识
//为每一张表定义成员变量,其中一个主要功能是关闭helper组件,移除所有DAO对象

类型名为自定义的消息 DAO 类型实例;获取消息 DAO 的方法名称。
如果消息 DAO 实例不存在时 {
try {

messageDao= getDao(Message.class);

}catch(SQLException e) {

e.printStackTrace();

}

}returnmessageDao;

}public dbHelper(super::helper, Context context) {super(context, DB_NAME, null, version);

}/** 1. 此方法,不会自动执行,需要手动执行,因为它不是回调函数

    1. 最后也不要自己调用,因为只应该执行1次*/

public voidonCreate(SQLiteDatabase arg0, ConnectionSource arg1) {

}/** 此方法,不会自动执行,需要手动执行,因为它不是回调函数*/

public void duringUpgrade(SQLiteDatabase db, ConnectionSource source, int stateCode, int connectionId) {

protected Object clone() throws CloneNotSupportedException {if (messageDao != null) {

messageDao= null;

}return super.clone();

}

}

DatabaseHelper

显示效果:

791588a592a76f1e0492d41dfceea2b4.png

和昨天用命令查询是结果是一样的 (图是昨天的)

97a6ac1d5726017007342374cac6397f.png

说一下里面的内容

a3c30abb69a4e65569abd5e0da6dbfe6.png

41行 是new一个自己定义的MyDatabaseOpenHelper()方法

43行 getWritableDatabase() 方法以读写方式打开数据库

44行 关闭资源

Android可以通过调用get!!, getReadableDatabase()这两个方法都能够成功地访问到用于操作数据库的SQL SQLite Database实例。

(getReadableDatabase()方法中会调用getWritableDatabase()方法)

其中getWritableDatabase() 方法以读写方式打开数据库,

当数据库的磁盘空间达到上限时,数据库将停止执行写入操作而仅能进行读取操作;如果所采用的方法是getAbsolutePath()的话,则会导致错误。

每当用户从pause(暂停)状态再次进入该activity时,该系统将resume该activity,并触发onResume()方法。

c6613db12bd84b340a2c768a31dc2823.png

暂停(pause)你的Activity

当系统执行activity的onPause()方法时, 然而就技术层面而言该activity是可以被识别的

然而通常情况下这表明用户将离开该活动 并导致你的活动进入停止状态

499ae5b22682b3658ef2f55805ed3afb.png

Activity的生命周期之Pause(暂停)和Resume(重新返回)

全部评论 (0)

还没有任何评论哟~