Android数据库操作_表格显示

Android数据库操作_表格显示


显示表格布局


完成后效果如下:

首先需要一个主布局文件main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<View
android:layout_width="0.5px"
android:layout_height="fill_parent"
android:background="#B8B8B8"
android:visibility="visible" />

<TextView
android:id="@+id/id"
android:layout_width="0dip"
android:layout_height="35dip"
android:layout_weight="2"
android:textColor="#CD3700"
android:textSize="20sp" />

<View
android:layout_width="0.5px"
android:layout_height="fill_parent"
android:background="#B8B8B8"
android:visibility="visible" />

<TextView
android:id="@+id/name"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="3"
android:textColor="#000000"
android:textSize="17sp" />

<View
android:layout_width="0.5px"
android:layout_height="fill_parent"
android:background="#B8B8B8"
android:visibility="visible" />

<TextView
android:id="@+id/age"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#000000"
android:textSize="17sp" />

<View
android:layout_width="0.5px"
android:layout_height="fill_parent"
android:background="#B8B8B8"
android:visibility="visible" />

</LinearLayout>

View起到的作用是在两列之间起到分割的作用,纵观这个布局文件,就是完成这样的工作,设置一个表头,将三个TextView放置在一个水平的线性布局中去,分别显示一列的表头,然后需要一个ListView与上述的线性布局一同放入一个垂直的线性布局中去,用来显示每一条记录。而每一条记录的显示需要我们来实现一个adapter去完成每一项的显示,下面就完成这个项的布局文件:itemlayout.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<View
android:layout_width="0.5px"
android:layout_height="fill_parent"
android:background="#B8B8B8"
android:visibility="visible" />

<TextView
android:id="@+id/id"
android:layout_width="0dip"
android:layout_height="35dip"
android:layout_weight="2"
android:textColor="#CD3700"
android:textSize="20sp" />

<View
android:layout_width="0.5px"
android:layout_height="fill_parent"
android:background="#B8B8B8"
android:visibility="visible" />

<TextView
android:id="@+id/name"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="3"
android:textColor="#000000"
android:textSize="17sp" />

<View
android:layout_width="0.5px"
android:layout_height="fill_parent"
android:background="#B8B8B8"
android:visibility="visible" />

<TextView
android:id="@+id/age"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#000000"
android:textSize="17sp" />

<View
android:layout_width="0.5px"
android:layout_height="fill_parent"
android:background="#B8B8B8"
android:visibility="visible" />

</LinearLayout>

在listview中每一项的布局应该是这样的,需要View来分割每一列,然后需要TextView来显示数据的信息,这些组件之间放在一个水平的线性布局中去。

这样我们就完成了程序的主体布局。接下来我们需要一个适配器(adapter)来完成对listview中每一项的数据填入。SimpleCursorAdapter是一个简单
的适配器,可以将cursor中的每一行的记录映射到一个显示的组件上一般是TextView或者是ImageView。那我们就继承这个类来完成自己的adapter。

下面是我们的adapter它继承了SimpleCursorAdapter。


package com.example.gird;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleCursorAdapter;

public class MySimpleCursorAdapter extends SimpleCursorAdapter {

public MySimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View view = null;
if (convertView != null) {
view = convertView;

} else {
view = super.getView(position, convertView, parent);

}

/*author:conowen
* date:2012.4.2
* MySimpleCursorAdapter
*/

int[] colors = { Color.WHITE, Color.rgb(219, 238, 244) };// RGB颜色

view.setBackgroundColor(colors[position % 2]);// 每隔item之间颜色不同

return super.getView(position, view, parent);
}

}

在其中完成的主要是对getView方法的重写。position当前由不可见到可见的项的位置,convertView就是要显示的组件项,这个时候Android不会每次都去实例化一个新的view对象,而是去看在缓存中是否存在一个这样的对象,若有就直接拿来,若没有才会去实例化新的对象。而parent是告诉这些个项最终会依附在哪一个父亲组件上去(Listview)。


package com.example.gird;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.database.sqlite.SQLiteCursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class GridActivity extends Activity {

public int DB_VERSION = 1;
SQLiteDatabase db;
// DbHelper类在DbHelper.java文件里面创建的
ListView lv;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 建立打开数据库
db = openOrCreateDatabase("test.db", Context.MODE_PRIVATE, null);
db.execSQL("DROP TABLE IF EXISTS person");
// 创建person表
db.execSQL("CREATE TABLE person (_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, age SMALLINT)");

// 插入数据
for (int i = 0; i < 20; i++) {
Person person = new Person();
person.name = "john" + i;
person.age = 30 - i;
db.execSQL("INSERT INTO person VALUES (NULL, ?, ?)", new Object[] {
person.name, person.age });
}

lv = (ListView) findViewById(R.id.lv);
updatelistview();
// 添加一个长按事件
lv.setOnItemLongClickListener(new OnItemLongClickListener() {

@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
// 实例化一个弹出框
new AlertDialog.Builder(GridActivity.this)
.setTitle("选择操作")
.setItems(new String[] { "更新", "删除", "取消" },
// 为弹出框上的选项添加事件
new OnClickListener() {

@Override
public void onClick(DialogInterface dialog,
int which) {
switch (which) {
// 表示更新内容
case 0:
LayoutInflater inflater = getLayoutInflater();
// 自定义一个弹出口布局
final View layout = inflater
.inflate(
R.layout.dialog,
(ViewGroup) findViewById(R.id.dialog));
EditText nameTxt = (EditText) layout
.findViewById(R.id.editText1);
TextView ageTxt = (EditText) layout
.findViewById(R.id.editText2);
SQLiteCursor s_old = (SQLiteCursor) lv
.getItemAtPosition(position);
final int _id_old = s_old.getInt(s_old
.getColumnIndex("_id"));
final String name_old = s_old.getString(s_old
.getColumnIndex("name"));
final String age_old = s_old.getString(s_old
.getColumnIndex("age"));

nameTxt.setText(name_old);
ageTxt.setText(age_old);

new AlertDialog.Builder(
GridActivity.this)
.setTitle("更新")
.setView(layout)
.setPositiveButton(
"确定",
new OnClickListener() {

@Override
public void onClick(
DialogInterface dialog,
int which) {
ContentValues cv = new ContentValues();
String temp_name = ((EditText) layout
.findViewById(R.id.editText1))
.getText()
.toString();
String temp_age = ((EditText) layout
.findViewById(R.id.editText2))
.getText()
.toString();

cv.put("_id",
_id_old);
cv.put("name",
temp_name);
cv.put("age",
temp_age);

String[] id_index = { String
.valueOf(_id_old) };
db.update(
"person",
cv,
"_id=?",
id_index);
updatelistview();
}
})
.setNegativeButton("取消",
null).show();
break;
// 删除记录
case 1:
// getItemAtPosition()得到一个item里的数据
SQLiteCursor s = (SQLiteCursor) lv
.getItemAtPosition(position);
final int _id = s.getInt(s
.getColumnIndex("_id"));
String name = s.getString(s
.getColumnIndex("name"));
Log.i("id ::", _id + "");
new AlertDialog.Builder(
GridActivity.this)
.setTitle(
"确定删除" + name
+ "吗?")
.setPositiveButton(
"确定",
new OnClickListener() {

@Override
public void onClick(
DialogInterface dialog,
int which) {
db.execSQL(
"delete from person where _id =?",
new Integer[] { _id });
updatelistview();
}
})
.setNegativeButton(
"取消",
new OnClickListener() {

@Override
public void onClick(
DialogInterface dialog,
int which) {
}
}).show();
break;
// 取消操作
case 2:
break;
}
}
}).show();

return false;
}

});
}

// 更新listview
public void updatelistview() {
Cursor cr = db.query("person", null, null, null, null, null, null);

String id = cr.getColumnName(0);
String name = cr.getColumnName(1);
String age = cr.getColumnName(2);
String[] ColumnNames = { id, name, age };

ListAdapter adapter = new MySimpleCursorAdapter(this,
R.layout.listviewlayout, cr, ColumnNames, new int[] { R.id.id,
R.id.name, R.id.age });

lv.setAdapter(adapter);
}

@Override
protected void onPause() {
onDestroy();
Log.i("message", "数据库连接销毁");
super.onPause();
}

@Override
protected void onDestroy() {// 关闭数据库
super.onDestroy();
if (db != null) {
db.close();
}
}

}

最后的效果图

对话框的布局文件代码


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:id="@+id/dialog"
>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="新姓名" />

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
android:ems="10" />

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/editText1"
android:ems="10" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1"
android:layout_marginTop="39dp"
android:text="新年龄" />

</RelativeLayout>

</LinearLayout>

Android数据库操作_表格显示

时间: 2024-08-24 19:11:50

Android数据库操作_表格显示的相关文章

如何将Android数据库操作通用化(三)

概述 悠悠绿水傍林侵日落观山四望回 幽林古寺孤明月冷井寒泉碧映台 鸥飞满浦渔舟泛鹤伴闲亭仙客来 游径踏花烟上走流溪远棹一篷开 概述 一个不小心都写了三篇了,也不知道大家还看得懂不?如果看不懂最好给我留个言,我好下一次改正. 接着上次的说,准备工作都已经做好了,现在咱们就要开始着手解决阻挡Android数据库操作通用化的五个问题了. 我们先回顾一下问题: 问题1:表名的获取 问题2:如何将实体中的数据,按照对应关系导入到数据库中 问题3:明确实体中主键是谁?获取到主键中封装的值 问题4:如何将数据

如何将Android数据库操作通用化(一)

概述 在开始考虑Android的数据库操作之前,我们先回想一下Web方面的数据库操作.如果我们只是停留在JDBC的简单使用和封装上(比如纯JDCB,或者DBUtils),即使我们对数据库的增删改查操作进行了接口的抽取,代码依旧会和业务有很强的耦合性. 经过我们分析,解除耦合性的关键在于如何解决自动映射"实体类 与 数据库表"之间的对应关系.如果能够做到这一步,那么我们就能够更好的解耦了,也能降低我们的代码重复率. 如果我们再跨前一步,使用更为优秀的框架(比如:Hibernate),这一

android菜鸟学习笔记20----Android数据存储(四))Android数据库操作

Android内置了一个名为SQLite的关系型数据库,这是一款轻量型的数据库,操作十分简便.SQLite与别的数据库不同的是,它没有数据类型.可以保存任何类型的数据到你所想要保存的任何表的任何列中.但它又支持常见的类型比如: NULL, VARCHAR, TEXT, INTEGER, BLOB, CLOB...等. 唯一的例外是:integer primary key 此字段只能存储64位整数. 在JAVA项目中,要使用JDBC操作数据库需要加载数据库驱动,连接数据库等操作.Android简化

android数据库操作之直接读取db文件

在对数据库操作时,常用的有两种方法: 1.在代码中建库.建表: 2.直接将相关库.表建立好,将db文件拷贝至assets目录下: 现在来看看第二种方法: private String GetDataBasePath(Context context) { String packageName = context.getPackageName(); //Log.i("PackName", packageName); // String DB_PATH = String.format(&qu

android数据库操作

package cn.hackcoder.beautyreader.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; /** * Created by hackcoder on 15-1-25. */ public class Data

android 数据库操作详解

请看郭大神的八篇专栏,包含sql语句  android封装的databasehelper 和郭大神自己的LitePal  三种使用详解 http://blog.csdn.net/column/details/android-database-pro.html

android数据库事务操作

在android应用程序开发中,在使用到数据库的时候,事务处理是非常重要的. 首先android数据库操作(特别是写操作)是非常慢的,将所有操作打包成一个事务能大大提高处理速度. 其次是保证数据的一致性,让一个事务中的所有操作都成功执行,或者失败,或者所有操作回滚. 标签:Android 事务 [1].[代码] [Java]代码 跳至 [1] ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 mDatabase.beginTransaction(); try{   //在这里

IT观察】网络通信、图片显示、数据库操作……Android程序员如何利用开源框架

每个Android 程序员都不是Android应用开发之路上孤军奋战的一个人,GitHub上浩如烟海的开源框架或类库就是前人为我们发明的轮子,有的轮子能提高软件性能,而有的轮子似乎是以牺牲性能为代价换取编程速度.擅长利用轮子的程序员已经遥遥领先,不擅长利用轮子的程序员总是嫌前人发明的轮子不够圆,自己造个方轮子上路后才发现落后了. 作者:玖哥来源:51CTO|2017-10-19 16:06 移动端 收藏 分享 [51CTO.com原创稿件]每个Android 程序员都不是Android应用开发之

Android打造属于自己的数据库操作类。

1.概述 开发Android的同学都知道sdk已经为我们提供了一个SQLiteOpenHelper类来创建和管理SQLite数据库,通过写一个子类去继承它,就可以方便的创建.管理数据库.但是当我们需要去做增删改查的操作的时候,就得通过getWritableDatabase获取一个SQLiteDataBase然后老老实实去写操作值的put以及查询返回的Cursor处理,其实我们可以搞一个对象来帮我们干这些事情,打造属于你自己的数据库操作类. 2.操作类的初显形 假设现在我们什么都没有,我们要去搞一