0

我是 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;
}
}
4

1 回答 1

1

试试这个例子,它给了你表格的外观,但使用列表视图 http://www.heikkitoivonen.net/blog/2009/02/15/multicolumn-listview-in-android/

祝你好运

于 2013-04-04T08:14:35.070 回答