我正在创建android报警应用程序,因为我必须在sqlite数据库中存储警报,但我无法解决它。我在listadapter和光标getalarm()方法遇到问题任何人都可以帮助我如何存储数据并提前从sqlite数据库中检索。
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class MyDatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "ALARM";
private Object databaseHelper;
public MyDatabaseHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL("CREATE TABLE alarms(_id INTEGER PRIMARY KEY AUTOINCREMENT, sHour INTEGER, sMinute INTEGER )");
} catch (SQLException e) {
e.printStackTrace();
}
}
public void onCreate(Bundle savedInstanceState,
MyDatabaseHelper databaseHelper, int hour, int minute) {
// super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_db);
databaseHelper.addAlarm("hour", hour);
databaseHelper.addAlarm("minute", minute);
}
private void setContentView(int activityAlarmDb) {
// TODO Auto-generated method stub
}
public void addAlarm(String hour, int minute)
{
ContentValues values = new ContentValues(2);
values.put("hour", hour);
values.put("minute", minute);
getWritableDatabase().insert("hour", "minute", values);
}
public Cursor getAlarm()
{
Cursor cursor = getReadableDatabase().rawQuery("select * from alarm",
null);
// Cursor AllFriends = databaseHelper.getAlarm();
Cursor getAlarm = databaseHelper.getAlarm();
getAlarm.moveToFirst();
while (!getAlarm.isAfterLast()) {
String Name = getAlarm.getString(1);
int number = getAlarm.getInt(2);
getAlarm.moveToNext();
String[] from = { "name", "phonenumber" };
int[] to = { android.R.id.text1, android.R.id.text2 };
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, getAlarm, from, to);
ListView myList = (ListView) findViewById(android.R.id.list);
myList.setAdapter(adapter);
}
}
private ListView findViewById(int list) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS alarms");
onCreate(db);
}
}