I'm using a SQLite DB in my android application to store football players and maintain which rosters they belong to. The activity which manages these rosters has the ability to open 2 dialog windows, 1 to save the current roster to the db and 1 to load an existing roster from the db.
When a roster is saved and then the dialog to load a roster is opened, the newly saved roster is not found in my query of the existing rosters. If I exit the activity and restart it the new roster is listed and can be loaded just fine.
Here is the code which queries the DB for existing roster names
public String[] fetchRosterNames() {
String query = "SELECT "+ _ID + ", " + ROSTER_NAME + ", COUNT(" + ROSTER_NAME + ") As `NUM_PLAYERS`" +
" FROM " + TABLE_NAME +
" GROUP BY " + ROSTER_NAME;
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery(query, null);
ArrayList<String> rosterNames = new ArrayList<String>();
if(cursor.getCount() > 0){
while(!cursor.isLast()){
cursor.moveToNext();
rosterNames.add(cursor.getString(1));
}
}
cursor.close();
db.close();
return rosterNames.toArray(new String[rosterNames.size()]);
}
here is the code which inserts a roster into the DB public void insertRoster(String roster_name, LeagueRoster roster){
SQLiteDatabase db = getWritableDatabase();
Iterator<Player> players = roster.getFullRoster();
while(players.hasNext()){
Player player = players.next();
ContentValues values = new ContentValues();
Log.e("MySQLiteHelper: insertRoster()", player.getName());
values.put(PlayerStats.NAME.name(), player.getName());
values.put(PlayerStats.POSITION.name(), player.getPosition().name());
values.put(PlayerStats.TEAM.name(), player.getTeam().name());
values.put(ROSTER_NAME, roster_name);
db.insertOrThrow(TABLE_NAME, null, values);
}
db.close();
}
And finally here is the code within my activity that creates the 2 different dialogs
protected Dialog onCreateDialog(int id){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
switch(id) {
case 1:
final MySQLiteHelper rosterData = new MySQLiteHelper(this);
final String[] items = rosterData.fetchRosterNames();
builder.setTitle("Select the roster you would like to load");
builder.setItems(items, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
Log.e("Roster Names Dialog", items[which]);
LeagueRoster loadedRoster = rosterData.fetchRoster(items[which]);
if(loadedRoster != null){
roster = loadedRoster;
db.setRoster(roster);
redrawTables();
}
}
});
dialog = builder.create();
break;
case 2:
default:
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View saveRosterView = inflater.inflate(R.layout.saveroster_dialog, null);
builder.setView(saveRosterView);
final EditText nameField = (EditText)saveRosterView.findViewById(R.id.saveroster_dialog_editText);
Button saveButton = (Button)saveRosterView.findViewById(R.id.saveroster_dialog_button);
saveButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
MySQLiteHelper rosterData = new MySQLiteHelper(RosterActivity.this);
String rosterName = nameField.getText().toString();
rosterData.insertRoster(rosterName, roster);
dialog.dismiss();
}
});
dialog = builder.create();
break;
}
return dialog;
}
I would very much appreciate some help with this issue if anyone has any ideas...
Weird, but try a db.commit() after each insert?
0 comments:
Post a Comment