0

我需要知道如何在没有意图服务的情况下将数据从活动页面传递到 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 类这listitemshashmap我在 dbhelper 类中创建的。

有人可以帮我吗@谢谢

4

1 回答 1

0

看看这个在活动中实现 SQLiteAdapter 的简单示例。

http://android-er.blogspot.com/2011/06/simple-example-using-androids-sqlite.html

请注意,还有其他方法可以通过 Content Provider(使用 SQLite)提供数据,尤其是当您需要与其他应用程序共享时。为此,请查看 Lar Vogel 的教程(顺便说一下,很好的来源)

http://www.vogella.com/articles/AndroidSQLite/article.html

另外,请随意查看我不久前使用 SQLite 完成的 Android 项目的一些(较旧的,而不是最紧凑/最干净的代码)。

您可以使用 DBhelper/adapter 类中的公共方法通过参数传递列表视图项的数据。

如果您需要其他帮助,请发布您的 DBHelper 代码。

https://github.com/zerkz/Stem-Esteem/blob/master/StemEsteem/src/whipkey/stemrespect/db/StemDbAdapter.java

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;
}

我通过 Activity 对话框中的 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。如果其他人对设计模式更擅长想纠正我,请随意。

于 2013-05-17T16:13:49.107 回答