我需要知道如何在没有intent服务的情况下将数据从活动页面传递给dbhelper类。我从服务器获取数据并将数据传递给数据库。我真的很困惑。
我已经存储了这样的数据
public static HashMap<String, Object> listhashmap;
我将它传递给dbhelper类,我使用了
listitems = FirstActivity.arraylistitems;
这是代码
public DBHelper(Context context) {
super(context, DBName, null, version);
currentContext = context;
newarraylistitems = FirstActivity.arraylistitems;
System.out.println("newarraylistitems="+newarraylistitems);
DBPath = "/data/data/" + context.getPackageName() + "/databases";
createDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
private void createDatabase() {
boolean dbExists = checkDbExists();
/* ("ID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE ,
"user_message_id" VARCHAR DEFAULT NA, "message" VARCHAR DEFAULT
NA, "title" VARCHAR DEFAULT NA, "message_time" VARCHAR DEFAULT NA, "mp3" VARCHAR DEFAULT NA,
"userID" VARCHAR DEFAULT NA)*/
if (dbExists) {
// do nothing
} else {
DB = currentContext.openOrCreateDatabase(DBName, 0, null);
DB.execSQL("CREATE TABLE IF NOT EXISTS " +tableName +" (ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, " +
"user_message_id VARCHAR DEFAULT NA," +"message VARCHAR DEFAULT NA," + "title VARCHAR DEFAULT NA)"+
"mp3 VARCHAR DEFAULT NA "+ "userID VARCHAR DEFAULT NA;");
////some dummy contents
DB.execSQL("INSERT INTO " +
tableName +
" Values ('M','Sing','India',25);");
DB.execSQL("INSERT INTO " +
tableName +
" Values ('C','Raje','India',25);");
DB.execSQL("INSERT INTO " +
tableName +
" Values ('D','Phonu','Argentina',20);");
DB.execSQL("INSERT INTO " +
tableName +
" Values ('V','Veera','EU',25);");
DB.execSQL("INSERT INTO " +
tableName +
" Values ('T','Shenoi','Bangla',25);");
DB.execSQL("INSERT INTO " +
tableName +
" Values ('L','Lamha','Australia',20);");
}
}
private boolean checkDbExists() {
SQLiteDatabase checkDB = null;
try {
String myPath = DBPath + DBName;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
// database does't exist yet.
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
}
我已经为listview创建了基础适配器,有没有其他方法可以将活动页面的hashmap数据传递给dbhelper类
这里listitems
是我在dbhelper类中创建的hashmap
。
有人可以帮助我@Thanks
答案 0 :(得分:0)
看看这个在活动中实现SQLiteAdapter的简单示例。
http://android-er.blogspot.com/2011/06/simple-example-using-androids-sqlite.html
请注意,还有其他通过内容提供商(使用SQLite)提供数据的方式,尤其是当您需要与其他应用程序共享时。为此,请查看Lar Vogel的教程(顺便说一句,很棒的来源)
http://www.vogella.com/articles/AndroidSQLite/article.html
另外,请随意查看我之前使用过SQLite的Android项目的一些(较旧的,而不是最严格/最干净的代码)。
您可以使用DBhelper / adapter类中的公共方法通过参数传递listview项的数据。
如果您需要其他帮助,请发布您的DBHelper代码。
public long createEnding(int weekNo, int stemNo, String ending) {
ContentValues values = new ContentValues();
values.put(WEEK_NUMBER, weekNo);
values.put(STEM_NUMBER, stemNo);
values.put(ENDING, ending);
return database.insert(TABLE_ENDINGS, null, values);
}
/**
* @updateEnding updates an ending that is already attached to the
* designated stem.
*/
public long updateEnding(int rowid, String ending) {
ContentValues values = new ContentValues();
values.put(ENDING, ending);
return database.update(TABLE_ENDINGS, values, "ROWID" + " = " + rowid,
null);
}
/**
* @deleteEnding deletes an the designated ending attached to a stem.
*/
public boolean deleteEnding(int rowid) {
return database.delete(TABLE_ENDINGS, "ROWID" + " = " + rowid, null) > 0;
}
我通过活动对话框中的onclick侦听器调用上述方法之一的示例。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.endingadddialog);
// inflate our views.
inputEnding = (EditText) findViewById(R.id.endingAddInputText);
stem = (TextView) findViewById(R.id.endingAddStemText);
ok = (Button) findViewById(R.id.okEndingAddButton);
cancel = (Button) findViewById(R.id.cancelEndingAddButton);
setTitle("Add Ending");
ok.setOnClickListener(new View.OnClickListener() { // assign the
// function of our
// ok button.
public void onClick(View v) {
StemEsteemApplication.getDbHelper().createEnding(
b.getInt("weekNo"), b.getInt("stemNo"),
inputEnding.getText().toString());
dismiss(); // this will call FillData() through the dismiss
// listener and also kill the dialog.
}
});
免责声明:请注意,上面的代码中有一些东西可能是脏的(比如使用全局变量(StemEsteemApplication)来获取我的DbHelper。如果有其他更好的设计模式的人想要纠正我,请随意。