0

嗨,有人可以指出我如何将 arraylist 值插入数据库的正确方向。我有两个数组列表如下:

    ArrayList<HashMap<String, String>> achieve_val = new ArrayList<HashMap<String, String>>();
    ArrayList<HashMap<String, String>> achieve_vol = new ArrayList<HashMap<String, String>>();

HashMap<String, String> Achiev_Vol_map = new HashMap<String, String>();
     Achiev_Vol_map.put(PRODUCT_CAKE, Achieve_Vcake);
     Achiev_Vol_map.put(PRODUCT_YEASTEXTRACT, Achieve_Vyeastextract);
     .
     .
     .
     achieve_vol.add(Achiev_Vol_map);

我为此值创建了一个 POJO 类,如下所示:

    public class ProductCategory {
        ArrayList<HashMap<String, String>> achieve_val,achieve_vol ;

         public ArrayList<HashMap<String, String>> getAchieve_val() {
            return achieve_val;}
        public void setAchieve_val(ArrayList<HashMap<String, String>> achieve_val) {
            this.achieve_val = achieve_val;}

        public ArrayList<HashMap<String, String>> getAchieve_vol() {
            return achieve_vol;}
        public void setAchieve_vol(ArrayList<HashMap<String, String>> achieve_vol) {
            this.achieve_vol = achieve_vol;}

/**my constructor**/
        public ProductCategory(ArrayList<HashMap<String, String>> achieve_val,
                ArrayList<HashMap<String, String>> achieve_vol) {
            super();
            this.achieve_val = achieve_val;
           this.achieve_vol = achieve_vol;}}

我将这些值传递给我的数据库适配器,如下所示

dbHelper.open();
dbHelper.topinchartsRecord(new ProductCategory(achieve_val,achieve_vol));

现在在我的数据库适配器中,我不确定如何访问这些 vlaues 并插入数据库,如下所示:

public void topinchartsRecord(ProductCategory productCategory) {

    for(ArrayList<HashMap<String, String>> map : achieve_val){
           ContentValues cv = new ContentValues();
           cv.put(FILE_NAME, map.get(FILE_NAME));

           db.insert("tablename", null, cv);
        }
}
4

1 回答 1

0

试试这个方法希望对你有帮助

public void insertRecords(ArrayList<HashMap<String, String>> data,
            String tableName) {
        if (data != null && data.size() > 0) {

            ContentValues con = new ContentValues();
            db.beginTransaction();

            for (int i = 0; i < data.size(); i++) {
                con.clear();
                HashMap<String, String> row = data.get(i);
                Iterator<String> it = row.keySet().iterator();

                while (it.hasNext()) {

                    String columnName = it.next();
                    con.put(columnName, row.get(columnName));
                }

                db.insertWithOnConflict(tableName, null, con,
                        SQLiteDatabase.CONFLICT_IGNORE);

            }
            db.setTransactionSuccessful();
            db.endTransaction();
        }
    }

从你的方法

public void topinchartsRecord(ProductCategory productCategory) {

        try{
            insertRecords(productCategory.getAchieve_val(),"tableNameForThisValues");
            insertRecords(productCategory.getAchieve_vol(),"tableNameForThisValues");
        }catch (Exception e) {
        }

    }

将 Hashmap 的 ArrayList 传递给此方法,表的名称将插入数据库。

于 2013-09-25T06:14:29.180 回答