从其他类创建的Access数据库(Android / JAVA)

时间:2017-04-21 23:07:53

标签: java android sql database sqlite

我试图列出从另一个类创建的数据库中的项目。 但我不知道如何在不再创建对象的情况下访问它。这是代码:

AddItemActivity:

Long pawns = Long.valueOf(14L);
...
placePiece(pawns, i, j, true); break;

MyDBHandler:

public class AddItemActivity extends AppCompatActivity {

MyDBHandler dbHandler;

...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_item);

    dbHandler = new MyDBHandler(this, null, null, 1);
}

public void insertButtonClick(View view) {
    Item item = new Item(...);

    dbHandler.addItems(item);
}

现在我要列出另一个活动中的所有项目,但我不知道如何访问在 AddItemActivity

中创建的 dbHandler
Public class MyDBHandler extends SQLiteOpenHelper{

...

public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

public void addItems(Item item) {
    ContentValues values = new ContentValues();
    SQLiteDatabase db = getWritableDatabase();

    // INSERT ITEMS
    values.put(COLUMN_ITEM, item.getName());
    ...

    db.close();
}

public String printDatabase() {
    String dbString = "";
    SQLiteDatabase db = getReadableDatabase();
    String printQuery = "SELECT * FROM " + TABLE_NAME + " WHERE 1";
    Cursor cursor = db.rawQuery(printQuery, null);
    cursor.moveToFirst();

    while (!cursor.isAfterLast()) {
        dbString += "Item: " + cursor.getString(cursor.getColumnIndex(COLUMN_ITEM)) +
                    " \t Amount: " + cursor.getString(cursor.getColumnIndex(COLUMN_AMOUNT)) +
                    " \t Price: " + cursor.getString(cursor.getColumnIndex(COLUMN_PRICE));
        cursor.moveToNext();
    }
    cursor.close();
    db.close();
    return dbString;
}

2 个答案:

答案 0 :(得分:0)

尽量让它变得静止!试着写 public static MyDBHandler dbHandler; 并像这样访问它 AddItemActivity.dbHandler

答案 1 :(得分:0)

您是否尝试在MyDBHandler课程中添加Application个实例?

public MyApplication extends Application
{
    private static MyApplication mInstance;
    private MyDBHandler mDatabase;

    ...

    public static synchronized MyApplication getInstance()
    {
        if ( mInstance == null )
        {
            mInstance = new MyApplication();
        }

        return mInstance;
    }

    public MyDBHandler getDatabase()
    {
        return mDatabase;
    }
}

在您的活动中,请按照以下方式使用:

public class AddItemActivity extends AppCompatActivity
{
    MyDBHandler dbHandler;

    ...

    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_add_item );

        // Get database from application instance
        dbHandler = MyApplication.getInstance().getDatabase();
    }
}

public class ListItemActivity extends AppCompatActivity
{
    private MyDBHandler mDb;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_data_base );

        // Get database from application instance
        mDb = MyApplication.getInstance().getDatabase();
    }

    public void printDatabase()
    {
        String dbString = ****.printDataBase();
        databaseTextView.setText(dbString);
    }
}