2017-04-21 75 views
0

我想列出从另一个类创建的数据库中的项目。 但我不知道如何在不创建对象的情况下访问它。下面的代码:访问从其他类创建的数据库(Android/JAVA)

AddItemActivity:

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

MyDBHandler:

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

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

public class ListItemActivity extends AppCompatActivity { 

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

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

回答

0

试着让它静止!尝试写 public static MyDBHandler dbHandler; 和访问它像这样 AddItemActivity.dbHandler

+0

THX!现在作品:D – Aye

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