我是android的新手。我为我的android应用程序创建了一个sqlite数据库。我希望使用固定头而不是ListView的表格式显示我的数据。下面的代码非常精细。我需要使用光标适配器吗?对此有哪些可能的控制?有任何想法吗?请发给我链接(例子)谢谢。
如何使用固定标题和第一列中的简单表格格式?像这样。
logdate | logtime | batlevel | batcharging | signalstrength | wifi | useraction |
---------------------------------------------------------------------------------
data | data | data | data | data | data | data |
data | data | data | data | data | data | data |
data | data | data | data | data | data | data |
data | data | data | data | data | data | data |
...
...
- 这是我的SQLiteOpenHelper类
package com.example.activitylifecycle;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MySQLiteHelper extends SQLiteOpenHelper {
//Attributes name
public static final String _ID = "_id";
public static final String LOGDATE = "Logdate";
public static final String LOGTIME = "Logtime";
public static final String BATLEVEL = "Batlevel";
public static final String BATCHARGING = "Batcharging";
public static final String SIGSTRENGTH = "Sigstrength";
public static final String WIFI = "Wifi";
public static final String USERACTION = "Useraction";
//database creation//
public static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "dbase";
public static final String DATABASE_TABLE = "applog";
private static final int DATABASE_VERSION = 1;
//table create..
private static final String DATABASE_CREATE = "create table applog (_id integer primary key autoincrement,"
+ "Logdate text null,"
+ "Logtime text null,"
+ "Batlevel text null,"
+ "Batcharging text null,"
+ "Sigstrength text null,"
+ "Wifi text null,"
+ "Useraction text null);";
public MySQLiteHelper(Context context) { super(context, DATABASE_NAME, null,
DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
- 这是我的数据源类
package com.example.activitylifecycle;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class AppLogDataSource {
//Database Fields
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allColumns = {MySQLiteHelper._ID, MySQLiteHelper.LOGDATE,
MySQLiteHelper.LOGTIME, MySQLiteHelper.BATLEVEL, MySQLiteHelper.BATCHARGING,
MySQLiteHelper.SIGSTRENGTH, MySQLiteHelper.WIFI, MySQLiteHelper.USERACTION};
public AppLogDataSource(Context context) {
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
//Insert
public void createApplog(String Logdate, String Logtime, String Batlevel, String
Batcharging, String Sigstrength, String Wifi, String Useraction){
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.LOGDATE, Logdate);
values.put(MySQLiteHelper.LOGTIME, Logtime);
values.put(MySQLiteHelper.BATLEVEL, Batlevel);
values.put(MySQLiteHelper.BATCHARGING, Batcharging);
values.put(MySQLiteHelper.SIGSTRENGTH, Sigstrength);
values.put(MySQLiteHelper.WIFI, Wifi);
values.put(MySQLiteHelper.USERACTION, Useraction);
long insertId = database.insert(MySQLiteHelper.DATABASE_TABLE, null,values);
Cursor cursor = database.query(MySQLiteHelper.DATABASE_TABLE,allColumns,
MySQLiteHelper._ID+ " = " + insertId, null, null, null, null);
cursor.moveToFirst();
cursor.close();
}
public Cursor getLogs() {
Cursor mCursor = database.query(MySQLiteHelper.DATABASE_TABLE,allColumns, null, null,
null, null, null);
mCursor.moveToFirst();
while(!mCursor.isAfterLast()){
mCursor.moveToNext();
}
return mCursor;
}
}
答案 0 :(得分:1)
试试这个例子,它给你看表,但是使用listview http://www.heikkitoivonen.net/blog/2009/02/15/multicolumn-listview-in-android/
祝你好运