mercredi 11 février 2015

Accessing data in sqlite after passing activity.

At first I was displaying all rows from db in MainActivity, later I needed to add detailed view so I passed activity and I can't access database. What I'm doing wrong? How can I get data like I did it in MainActivity?



public class MainActivity extends Activity {

public final static String ID_EXTRA="com.example.bazadanych._ID";

DBAdapter myDB;


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



openDB();

populateListViewDB ();

onclickcallback();

}

@Override
protected void onDestroy() {
super.onDestroy();
closeDB();
}

private void openDB() {
myDB = new DBAdapter(this);
myDB.open();

}

private void closeDB() {
myDB.close();

}
private void populateListViewDB() {
Cursor cursor = myDB.getAllRows();

// aktywnosc bedzie zarzadzala cyklem kursora

startManagingCursor(cursor);

//mapowanie dla kursowra dla wyswietlania pol

String[] fromFieldNames = new String[]
{DBAdapter.KEY_NAME, DBAdapter.KEY_COUNTRY, DBAdapter.KEY_REGION, DBAdapter.KEY_PHONE};
int [] toViewIDs = new int []
{R.id.list_item_name, R.id.list_item_country, R.id.list_item_region, R.id.list_icon_item};


// stworzenie adaptera dla kolumn
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(
this, //context
R.layout.main_list_item, // layout wiersza
cursor, //cursor - zestaw rekordow z DB
fromFieldNames, // wartosci w DB
toViewIDs // wyswietla ID, aby insertowac informacje
);


// ustawianie adaptera do listview
ListView myList = (ListView) findViewById(R.id.listViewDB);
myList.setAdapter(myCursorAdapter);

}

private void onclickcallback() {
ListView myList = (ListView) findViewById(R.id.listViewDB);
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {


@Override
public void onItemClick(AdapterView<?> parent, View viewClicked,
int position, long id) {

Intent intent = new Intent(MainActivity.this, Details.class);
intent.putExtra(ID_EXTRA, String.valueOf(id));
startActivity(intent);

}

});
}
}


ACtivity is passed to Details.class. Eclipse want's me to create method "getWritableDatabase", but it's already in the DBAdapter, what's wrong here?



Details extends Activity {

DBAdapter myDB;
String passedV=null;

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

passedV=getIntent().getStringExtra(MainActivity.ID_EXTRA);
SQLiteDatabase db = (new DBAdapter(this)).getWritableDatabase();

openDB();

populateListViewDB ();



}

@Override
protected void onDestroy() {
super.onDestroy();
closeDB();
}



private void openDB() {
myDB = new DBAdapter(this);
myDB.open();

}

private void closeDB() {
myDB.close();

}

private void populateListViewDB() {
Cursor cursor = myDB.getRow(0);

// aktywnosc bedzie zarzadzala cyklem kursora

startManagingCursor(cursor);

//mapowanie dla kursowra dla wyswietlania pol

String[] fromFieldNames = new String[]
{DBAdapter.KEY_NAME, DBAdapter.KEY_COUNTRY, DBAdapter.KEY_REGION, DBAdapter.KEY_VC, DBAdapter.KEY_ADRESS, DBAdapter.KEY_PHONENUM, DBAdapter.KEY_PHONE};
int [] toViewIDs = new int []
{R.id.item_name, R.id.item_country, R.id.item_region, R.id.item_vc, R.id.item_adress, R.id.item_phonenum, R.id.list_icon_item};


// stworzenie adaptera dla kolumn
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(
this, //context
R.layout.item_layout, // layout wiersza
cursor, //cursor - zestaw rekordow z DB
fromFieldNames, // wartosci w DB
toViewIDs // wyswietla ID, aby insertowac informacje
);


// ustawianie adaptera do listview
ListView myList = (ListView) findViewById(R.id.listViewDB);
myList.setAdapter(myCursorAdapter);


}
}


This is Database Adapter



public class DBAdapter {


private static final String TAG = "DBAdapter";

// DB Fields
public static final String KEY_ROWID = "_id";
public static final int COL_ROWID = 0;

public static final String KEY_NAME = "Name";
public static final String KEY_COUNTRY = "Country";
public static final String KEY_REGION = "Region";
public static final String KEY_VC = "VisitorsCenter";
public static final String KEY_ADRESS = "Adress";
public static final String KEY_PHONE = "Phone";
public static final String KEY_PHONENUM = "PhoneNumber";





// TODO: Setup your field numbers here (0 = KEY_ROWID, 1=...)
public static final int COL_NAME = 1;
public static final int COL_COUNTRY = 2;
public static final int COL_REGION = 3;
public static final int COL_VC = 4;
public static final int COL_ADRESS = 5;
public static final int COL_PHONE = 6;
public static final int COL_PHONENUM = 7;


public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_NAME, KEY_COUNTRY, KEY_REGION, KEY_VC, KEY_ADRESS, KEY_PHONE, KEY_PHONENUM };

public static final String DATABASE_NAME = "MyDb";
public static final String DATABASE_TABLE = "mainTable";
// Track DB version if a new version of your app changes the format.
public static final int DATABASE_VERSION = 18;

private static final String DATABASE_CREATE_SQL =
"create table " + DATABASE_TABLE
+ " (" + KEY_ROWID + " integer primary key autoincrement, "

+ KEY_NAME + " text not null, "
+ KEY_COUNTRY + " string not null, "
+ KEY_REGION + " string not null, "
+ KEY_VC + " string not null, "
+ KEY_ADRESS + " String not null, "
+ KEY_PHONE + " int not null, "
+ KEY_PHONENUM + " int not null "


+ ");";

// Context of application who uses us.
private final Context context;

private DatabaseHelper myDBHelper;
private SQLiteDatabase db;

public DBAdapter(Context ctx) {
this.context = ctx;
myDBHelper = new DatabaseHelper(context);
}

// Open the database connection.
public DBAdapter open() {
db = myDBHelper.getWritableDatabase();
return this;
}

// Close the database connection.
public void close() {
myDBHelper.close();
}

// Add a new set of values to the database !!!
public long insertRow(String Name, String Country, String Region, String VisitorsCenter, String Adress, int Phone, int PhoneNumber) {

ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, Name);
initialValues.put(KEY_COUNTRY, Country);
initialValues.put(KEY_REGION, Region);
initialValues.put(KEY_VC, VisitorsCenter);
initialValues.put(KEY_ADRESS, Adress);
initialValues.put(KEY_PHONE, Phone);
initialValues.put(KEY_PHONENUM, PhoneNumber);

// Insert it into the database.
return db.insert(DATABASE_TABLE, null, initialValues);
}

// Delete a row from the database, by rowId (primary key)
public boolean deleteRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
return db.delete(DATABASE_TABLE, where, null) != 0;
}

public void deleteAll() {
Cursor c = getAllRows();
long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
if (c.moveToFirst()) {
do {
deleteRow(c.getLong((int) rowId));
} while (c.moveToNext());
}
c.close();
}

// Return all data in the database.
public Cursor getAllRows() {
String where = null;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}

// Get a specific row (by rowId)
public Cursor getRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}

// Change an existing row to be equal to new data.
public boolean updateRow(long rowId, String Name, String Country, String Region, String VisitorsCenter, String Adress, int Phone, int PhoneNumber) {
String where = KEY_ROWID + "=" + rowId;

ContentValues newValues = new ContentValues();
newValues.put(KEY_NAME, Name);
newValues.put(KEY_COUNTRY, Country);
newValues.put(KEY_REGION, Region);
newValues.put(KEY_VC, VisitorsCenter);
newValues.put(KEY_ADRESS, Adress);
newValues.put(KEY_PHONE, Phone);
newValues.put(KEY_PHONENUM, PhoneNumber);

// Insert it into the database.
return db.update(DATABASE_TABLE, newValues, where, null) != 0;
}




private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase _db) {
_db.execSQL(DATABASE_CREATE_SQL);
}

@Override
public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading application's database from version " + oldVersion
+ " to " + newVersion + ", which will destroy all old data!");

// Destroy old database:
_db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);

// Recreate new database:
onCreate(_db);
}
}
}


Thanks in advance for all your help.


Aucun commentaire:

Enregistrer un commentaire