我知道您可以使用
限制查询" SELECT * FROM chargelog ORDER BY date DESC LIMIT 10"
但问题是我不知道将它放在我的代码中的哪个位置。有人能帮我吗?一切正常,但列表视图永远不会结束。
DatabaseAdapter.java
public class DatabaseAdapter {
private static final int DBVER = 3;
private static final String COLUMN_ID = "_id";
private static final String COLUMN_DATE = "date";
private static final String COLUMN_PERCENT = "percent";
private static final String COLUMN_STATUS = "status";
private static final String DBNAME_CLOG = "chargelogs";
private static final String DBTABLE_CLOG = "chargelog";
private static final String DB_CREATE_TABLE_CLOG = "CREATE TABLE "
+ DBTABLE_CLOG + "(" + COLUMN_ID
+ " integer primary key autoincrement," + COLUMN_DATE
+ " text not null," + COLUMN_PERCENT + " text not null,"
+ COLUMN_STATUS + " text not null)";
private static final String DB_DROP_CLOG = "DROP TABLE IF EXISTS "
+ DBTABLE_CLOG;
private DBHelper mDbHelper;
private static SQLiteDatabase mDb;
private final Context mCtx;
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, DBNAME_CLOG, null, DBVER);
}
@Override
public void onCreate(SQLiteDatabase mDb) {
mDb.execSQL(DB_CREATE_TABLE_CLOG);
}
@Override
public void onUpgrade(SQLiteDatabase mDb, int oldVersion, int newVersion) {
mDb.execSQL(DB_DROP_CLOG);
onCreate(mDb);
}
}
public DatabaseAdapter(Context ctx) {
this.mCtx = ctx;
}
public DatabaseAdapter open() throws SQLException {
mDbHelper = new DBHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
public long createLog(String date, String percent, String status) {
if (mDb == null)
open();
ContentValues initValues = new ContentValues();
initValues.put(COLUMN_DATE, date);
initValues.put(COLUMN_PERCENT, percent);
initValues.put(COLUMN_STATUS, status);
return mDb.insert(DBTABLE_CLOG, null, initValues);
}
public Cursor fetchInputs() {
if (mDb == null)
open();
return mDb.query(DBTABLE_CLOG, new String[] { COLUMN_ID, COLUMN_DATE,
COLUMN_PERCENT, COLUMN_STATUS }, null, null, null, null, null);
}
public ArrayList<Chargelog> getHistory() {
ArrayList<Chargelog> ins = new ArrayList<Chargelog>();
Cursor mCursor = fetchInputs();
mCursor.moveToFirst();
while (!mCursor.isAfterLast()) {
Chargelog i = new Chargelog();
i.setId(mCursor.getInt(0));
i.setDate(mCursor.getString(1));
i.setPercent(mCursor.getString(2));
i.setStatus(mCursor.getString(3));
ins.add(i);
mCursor.moveToNext();
}
mCursor.close();
return ins;
}
}
ChargeLogActivity.java
@SuppressLint("SimpleDateFormat")
public class ChargeLogActivity extends Activity {
/** Called when the activity is first created. */
private String strText;
private DatabaseAdapter mDb;
private boolean trun = true;
private Handler myHandler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chargelog);
mDb = new DatabaseAdapter(this);
mDb.open();
startMonitor();
}
@Override
public void onDestroy() {
trun = false;
super.onDestroy();
}
private void startMonitor() {
myThread.start();
}
private void updateNow() {
TextView current = (TextView) findViewById(R.id.current);
current.setText(strText);
updateData();
}
private void batteryLevelUpdate() {
BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,
-1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
int level = -1;
if (rawlevel >= 0 && scale > 0) {
level = (rawlevel * 100) / scale;
}
int status = intent
.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
String strStatus = "";
switch (status) {
case BatteryManager.BATTERY_STATUS_UNKNOWN:
strStatus = "Unknown Charged";
break;
case BatteryManager.BATTERY_PLUGGED_USB:
strStatus = "Starts Charging";
mDb.createLog(DateFormat(cal.getTime()).toString(),
Integer.toString(level), strStatus);
break;
default:
strStatus = "Not Charging";
mDb.createLog(DateFormat(cal.getTime()).toString(),
Integer.toString(level), strStatus);
break;
case BatteryManager.BATTERY_STATUS_FULL:
strStatus = "Fully Charged";
mDb.createLog(DateFormat(cal.getTime()).toString(),
Integer.toString(level), strStatus);
break;
}
strText = "\t\t\tDate\t" + "\t\t\tTime\t" + "\t\t\tLevel\t" + "\t\t\t\tStatus";
}
};
// Register for the battery changed event
IntentFilter batteryLevelFilter = new IntentFilter(
Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}
private String DateFormat(Date time) {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
// get current date time with Calendar()
Calendar cal = Calendar.getInstance();
return dateFormat.format(cal.getTime());
}
private void updateData() {
final ArrayList<Chargelog> ins = mDb.getHistory();
Chargelog i = new Chargelog();
String date;
String percent;
String dstatus;
ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
for (int x = 0; x < ins.size(); x++) {
i = ins.get(x);
date = i.getDate();
percent = i.getPercent();
dstatus = i.getStatus();
HashMap<String, String> datadata = new HashMap<String, String>(2);
datadata.put("d", date);
datadata.put("p", percent);
datadata.put("s", dstatus);
data.add(datadata);
}
ListView lv = (ListView) findViewById(R.id.lv1);
CustListAdapter adp = new CustListAdapter(ChargeLogActivity.this, data,
R.layout.cust_list_item, new String[] { "d", "p", "s" },
new int[] { R.id.date, R.id.percent, R.id.status });
lv.setAdapter(adp);
}
private Runnable myRun = new Runnable() {
public void run() {
updateNow();
}
};
DateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy HH:mm");
// get current date time with Calendar()
Calendar cal = Calendar.getInstance();
// using Thread to keep the process running
private Thread myThread = new Thread() {
public void run() {
do {
batteryLevelUpdate();
myHandler.post(myRun);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} while (trun);
}
};
}
答案 0 :(得分:0)
我认为就在这里:
return mDb.query(DBTABLE_CLOG, new String[] { COLUMN_ID, COLUMN_DATE,
COLUMN_PERCENT, COLUMN_STATUS }, null, null, null, null, null, "LIMIT 10");
但是没有测试,检查query
的重载,我无法回想起确切的位置。
编辑,再添加一个null,认为就是它......