У меня есть бд
Код: Выделить всё
package com.example.tamagochi;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class IStorage {
private DBHelper dbHelper;
private SQLiteDatabase db;
private ContentValues cv;
IStorage(Context context){
this.dbHelper = new DBHelper(context);
// подключаемся к БД
this.db = dbHelper.getWritableDatabase();
// создаем объект для данных
this.cv = new ContentValues();
}
public void Save(MTamagochi tamagochi){
// создаем объект для данных
db.delete("tamagochi", null, null);
cv = new ContentValues();
cv.put("name", tamagochi.getName());
cv.put("age", tamagochi.getAge());
cv.put("healthy", tamagochi.getHealthy());
cv.put("hangry", tamagochi.getHangry());
cv.put("energy", tamagochi.getEnergy());
cv.put("money", tamagochi.getMoney());
cv.put("exp", tamagochi.getExp());
cv.put("img", tamagochi.getImgUrl());
cv.put("clean", tamagochi.getClean());
db.insert("tamagochi", null, cv);
cv.clear();
}
public void load(MTamagochi aTamagochi){
// делаем запрос всех данных из таблицы mytable, получаем Cursor
Cursor c = db.query("tamagochi", null, null, null, null, null, null);
if (c.moveToFirst()) {
// определяем номера столбцов по имени в выборке
int nameColIndex = c.getColumnIndex("name");
int ageColIndex = c.getColumnIndex("age");
int healthyColIndex = c.getColumnIndex("healthy");
int hangryColIndex = c.getColumnIndex("hangry");
int energyColIndex = c.getColumnIndex("energy");
int moneyColIndex = c.getColumnIndex("money");
int expColIndex = c.getColumnIndex("exp");
int imgColIndex = c.getColumnIndex("img");
int CleanColIndex = c.getColumnIndex("clean");
do {
// получаем значения по номерам столбцов и пишем все в..
aTamagochi.setName(c.getString(nameColIndex));
aTamagochi.setAge(c.getInt(ageColIndex));
aTamagochi.setHealthy(c.getInt(healthyColIndex));
aTamagochi.setHangry(c.getInt(hangryColIndex));
aTamagochi.setEnergy(c.getInt(energyColIndex));
aTamagochi.setMoney(c.getInt(moneyColIndex));
aTamagochi.setExp(c.getInt(expColIndex));
aTamagochi.setImgUrl(imgColIndex);
aTamagochi.setClean(CleanColIndex);
// переход на следующую строку
// а если следующей нет (текущая - последняя), то false - выходим из цикла
} while (c.moveToNext());
} else
c.close();
}
public void Save(MFoods food){
// создаем объект для данных
ContentValues cv = new ContentValues();
cv.put("name", food.getName());
cv.put("count", food.getCount());
cv.put("hangry", food.getHangry());
cv.put("energy", food.getEnergy());
cv.put("cost", food.getCost());
cv.put("exp", food.getExp());
cv.put("img", food.getUrlImg());
cv.put("eatAge", food.getEatAge());
db.insert("tamagochi", null, cv);
cv.clear();
}
public void load(MFoods food){
// делаем запрос всех данных из таблицы mytable, получаем Cursor
Cursor c = db.query("foods", null, null, null, null, null, null);
if (c.moveToFirst()) {
// определяем номера столбцов по имени в выборке
int idColIndex = c.getColumnIndex("id");
int nameColIndex = c.getColumnIndex("name");
int countColIndex = c.getColumnIndex("count");
int hangryColIndex = c.getColumnIndex("hangry");
int energyColIndex = c.getColumnIndex("energy");
int costColIndex = c.getColumnIndex("cost");
int expColIndex = c.getColumnIndex("exp");
int imgColIndex = c.getColumnIndex("img");
int eatAgeColIndex = c.getColumnIndex("eatAge");
do {
// получаем значения по номерам столбцов и пишем все в..
food.set("name", c.getString(nameColIndex));
food.set("count", c.getString(countColIndex));
food.set("hangry", c.getString(hangryColIndex));
food.set("energy", c.getString(energyColIndex));
food.set("cost", c.getString(costColIndex));
food.set("exp", c.getString(expColIndex));
food.set("img", c.getString(imgColIndex));
food.set("eatAge", c.getString(eatAgeColIndex));
// переход на следующую строку
// а если следующей нет (текущая - последняя), то false - выходим из цикла
} while (c.moveToNext());
} else
c.close();
}
public void Save(MToilet toilet){}
public void load(MToilet toilet){}
public void Save(MSand sand){}
public void load(MSand sand){}
}
class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, "TAMAGOCHI_DB", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table tamagochi ("
+ "id integer primary key autoincrement,"
+ "name text,"
+ "age int null,"
+ "healthy int null,"
+ "hangry int null,"
+ "energy int null,"
+ "money int null,"
+ "exp int null,"
+ "img int null"
+ ");");
db.execSQL("create table foods ("
+ "id integer primary key autoincrement,"
+ "name text,"
+ "count int null,"
+ "energy int null,"
+ "hangry int null,"
+ "cost int null,"
+ "exp int null,"
+ "img int null,"
+ "eatAge int null"
+ ");");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
в MainActivity
Код: Выделить всё
IStorage storage = new IStorage(this);
t = new MTamagochi("pentr");
t.setAge(3);
storage.Save(t);
storage.load(t);
Код: Выделить всё
public static IStorage storage;
мой 2 активити
Код: Выделить всё
MTamagochi ta = new MTamagochi("");
//IStorate storate = new IStorate(this);
MainActivity.storage.load(ta);
Toast.makeText(this,""+ta.getExp(), Toast.LENGTH_SHORT).show();
Как можно получить contex первого активити, или как использувать одну бд во всех активити.