你好!我想问问怎样才可以把sqliteDB里面的内容显示在手机 上?我用的ListView 显示的!为什么达不到像Excel表格一样的形式了??代码如下:
package com.example.com.ht.sqlitelistview;
import java.io.File;
import java.io.IOException;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class MainActivity extends Activity {
SQLiteDatabase ht_read_db;
public void initialize () {
sqliteopenhelper ht_dbhelper = new sqliteopenhelper(MainActivity.this, "ht_write.db", null, 1);
// sqliteopenhelper ht_dbhelper = new sqliteopenhelper(MainActivity.this, "F:\users.db", null, 1);
final SQLiteDatabase ht_write_db = ht_dbhelper.getWritableDatabase();
Button btn_read,btn_write,btn_create_table;
Button btn_insert,btn_update,btn_query,btn_del;
btn_read = (Button) findViewById(R.id.button_read);
btn_write = (Button) findViewById(R.id.button_write);
btn_create_table = (Button) findViewById(R.id.create_table);
btn_insert = (Button) findViewById(R.id.btn_insert);
btn_update = (Button) findViewById(R.id.btn_updata);
btn_query = (Button) findViewById(R.id.btn_querey);
btn_del = (Button) findViewById(R.id.btn_del);
btn_read.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("创建一个只读华泰—数据库");
sqliteopenhelper ht_dbhelper = new sqliteopenhelper(MainActivity.this, "ht_read.db", null, 1);
ht_read_db = ht_dbhelper.getReadableDatabase();
}
});
btn_write.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("创建一个可读可写的的华泰数据库");
sqliteopenhelper ht_dbhelper = new sqliteopenhelper(MainActivity.this, "ht_write2.db", null, 1);
// sqliteopenhelper ht_dbhelper = new sqliteopenhelper(MainActivity.this, "F:\\users.db", null, 1);
SQLiteDatabase ht_write_db2 = ht_dbhelper.getWritableDatabase();
}
});
btn_create_table.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
System.out.println("create table user_info");
String createstr = "create table user_info("+"_id int,"+"name char(20),"+"age int)";
ht_write_db.execSQL(createstr);
System.out.println("create table ht_user_information");
String createstr2 = "create table ht_user_information("+"user_id int,"+"user_name char(20),"+"user_phone int,"+"user_address char,"+"user_age int,"+"user_password int)";
ht_write_db.execSQL(createstr2);
}
});
btn_insert.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
System.out.println("insert table str1");
String insertstr = "insert into user_info(_id,name,age)"+"values(?,?,?)";
Object[] valuesObjestc = {1,"seven",22};
ht_write_db.execSQL(insertstr, valuesObjestc);
System.out.println("insert table str2");
String inserstr2 = "insert into user_info(_id,name)"+"values(2,'Jim')";
ht_write_db.execSQL(inserstr2);
System.out.println("insert table str3");
String inserstr3 = "insert into user_info(_id,name,age)"+"values(3,'Jdf',24)";
ht_write_db.execSQL(inserstr3);
System.out.println("insert table str2");
String inserstr4 = "insert into user_info(_id,name,age)"+"values(4,'Jff',10)";
ht_write_db.execSQL(inserstr4);
System.out.println("insert table str5");
String inserstr5 = "insert into user_info(_id,name,age)"+"values(5,'Jgg',563)";
ht_write_db.execSQL(inserstr5);
ContentValues valzw = new ContentValues();
valzw.put("_id", "45");
valzw.put("name", "zzzzz");
valzw.put("age", 100);
//ht_write_db.insert("user_info", "_id", valzw);//android insert
ht_write_db.insert("user_info", "name", valzw);
}
});
btn_update.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
System.out.println("更新table");
ContentValues values = new ContentValues();
values.put("name", "zw");
ht_write_db.update("user_info", values, "_id=?", new String[]{"1"});
ContentValues value_age = new ContentValues();
value_age.put("age",100);
value_age.put("name", "two");
value_age.put("age", 555);
ht_write_db.update("user_info", value_age, "_id=?", new String[]{"2"});
ContentValues value_id = new ContentValues();
value_id.put("name", "周希");
ht_write_db.update("user_info", value_id, "_id=?", new String[]{"5"});
}
});
btn_query.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
System.out.println("query table!");
Cursor cursor = ht_write_db.query("user_info", new String[]{"_id", "name","age" },null, null, null,null,"name");
//cursor.move(1);//向下
cursor.moveToFirst();
//cursor.moveToLast();
//cursor.move(-1);//向上
//cursor.moveToNext();
//cursor.moveToPosition(1);
// cursor.moveToPrevious();//上一行!
String str_cursor = cursor.getString(cursor.getColumnIndexOrThrow("age"));
System.out.println("cursor.getColumIndex is:" +cursor.getColumnIndex("age"));
System.out.println("str_cursor is:" + str_cursor);
//cursor.close();//此处不能关,否则listview显示不出内容来.
SimpleCursorAdapter sCursorAdapter = new SimpleCursorAdapter(MainActivity.this, R.layout.user, cursor, new String[]{"_id","name","age"}, new int[]{R.id.editText1,R.id.editText2,R.id.editText3});
ListView listview = (ListView)findViewById(R.id.listview);//取listView 对像!
listview.setAdapter(sCursorAdapter);
}
});
btn_del.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
System.out.println("del table !");
ht_write_db.delete("user_info", "age=?", new String[] {"100"});
String del_db = "drop database ht_write";
//ht_write_db.execSQL(del_db);
String del_table = "drop table user_info";
ht_write_db.execSQL(del_table);
System.out.println("已经删除table info");
String del_table2 = "drop table ht_user_information";
ht_write_db.execSQL(del_table2);
System.out.println("已经删除table ht_user_information");
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
this.initialize();
}finally{
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class sqliteopenhelper extends SQLiteOpenHelper{
public sqliteopenhelper(Context sqliteopenhelper, String name,
CursorFactory factory, int version) {
super(sqliteopenhelper, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
System.out.println("华泰—数据库创建");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
System.out.println("华泰-数据库更新");
}
}
}