store selected groups by id rather than position, fixing problems like

clearing the selection after moving a group
This commit is contained in:
Eric House 2013-10-22 07:02:49 -07:00
parent 8f8fd22c7f
commit 8c5cd9ebe3
3 changed files with 46 additions and 32 deletions

View file

@ -161,9 +161,9 @@ public class GameListAdapter implements ExpandableListAdapter {
deselectRows( rowids ); deselectRows( rowids );
} }
public void clearSelectedGroups( HashSet<Integer> groups ) public void clearSelectedGroups( HashSet<Long> groupIDs )
{ {
deselectGroups( groups ); deselectGroups( groupIDs );
} }
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
@ -241,8 +241,10 @@ public class GameListAdapter implements ExpandableListAdapter {
// if ( null != convertView ) { // if ( null != convertView ) {
// DbgUtils.logf( "getGroupView gave non-null convertView" ); // DbgUtils.logf( "getGroupView gave non-null convertView" );
// } // }
long groupID = getGroupIDFor( groupPosition );
GameListGroup view = GameListGroup view =
GameListGroup.makeForPosition( m_context, groupPosition, m_cb ); GameListGroup.makeForPosition( m_context, groupPosition, groupID,
m_cb );
if ( !isExpanded ) { if ( !isExpanded ) {
GameGroupInfo ggi = getInfoForGroup( groupPosition ); GameGroupInfo ggi = getInfoForGroup( groupPosition );
@ -388,12 +390,13 @@ public class GameListAdapter implements ExpandableListAdapter {
} }
} }
private void deselectGroups( HashSet<Integer> groups ) private void deselectGroups( HashSet<Long> groupids )
{ {
groups = (HashSet<Integer>)groups.clone(); groupids = (HashSet<Long>)groupids.clone();
for ( Iterator<Integer>iter = groups.iterator(); for ( Iterator<Long>iter = groupids.iterator();
iter.hasNext(); ) { iter.hasNext(); ) {
GameListGroup group = getGroupItemFor( iter.next() ); int pos = getGroupPosition( iter.next() );
GameListGroup group = getGroupItemFor( pos );
group.setSelected( false ); group.setSelected( false );
} }
} }

View file

@ -31,17 +31,20 @@ public class GameListGroup extends ExpiringTextView
implements GameListAdapter.ClickHandler implements GameListAdapter.ClickHandler
{ {
private int m_groupPosition; private int m_groupPosition;
private long m_groupID;
private boolean m_expanded; private boolean m_expanded;
private GameListAdapter.LoadItemCB m_cb; private GameListAdapter.LoadItemCB m_cb;
public static GameListGroup makeForPosition( Context context, public static GameListGroup makeForPosition( Context context,
int groupPosition, int groupPosition,
long groupID,
GameListAdapter.LoadItemCB cb ) GameListAdapter.LoadItemCB cb )
{ {
GameListGroup result = GameListGroup result =
(GameListGroup)Utils.inflate( context, R.layout.game_list_group ); (GameListGroup)Utils.inflate( context, R.layout.game_list_group );
result.m_cb = cb; result.m_cb = cb;
result.m_groupPosition = groupPosition; result.m_groupPosition = groupPosition;
result.m_groupID = groupID;
return result; return result;
} }
@ -60,6 +63,11 @@ public class GameListGroup extends ExpiringTextView
return m_groupPosition; return m_groupPosition;
} }
public long getGroupID()
{
return m_groupID;
}
public void setSelected( boolean selected ) public void setSelected( boolean selected )
{ {
// If new value and state not in sync, force change in state // If new value and state not in sync, force change in state

View file

@ -87,8 +87,8 @@ public class GamesList extends XWExpandableListActivity
RESET_GAME, RESET_GAME,
SYNC_MENU, SYNC_MENU,
NEW_FROM, NEW_FROM,
DELETE_GROUPS,
DELETE_GAMES, DELETE_GAMES,
DELETE_GROUPS,
OPEN_GAME OPEN_GAME
}; };
private static final int[] DEBUGITEMS = { private static final int[] DEBUGITEMS = {
@ -131,7 +131,7 @@ public class GamesList extends XWExpandableListActivity
private boolean m_gameLaunched = false; private boolean m_gameLaunched = false;
private boolean m_menuPrepared; private boolean m_menuPrepared;
private HashSet<Long> m_selectedGames; private HashSet<Long> m_selectedGames;
private HashSet<Integer> m_selectedGroups; private HashSet<Long> m_selectedGroupIDs;
@Override @Override
protected Dialog onCreateDialog( int id ) protected Dialog onCreateDialog( int id )
@ -361,7 +361,7 @@ public class GamesList extends XWExpandableListActivity
getBundledData( savedInstanceState ); getBundledData( savedInstanceState );
m_selectedGames = new HashSet<Long>(); m_selectedGames = new HashSet<Long>();
m_selectedGroups = new HashSet<Integer>(); m_selectedGroupIDs = new HashSet<Long>();
setContentView(R.layout.game_list); setContentView(R.layout.game_list);
ExpandableListView listview = getExpandableListView(); ExpandableListView listview = getExpandableListView();
@ -506,12 +506,12 @@ public class GamesList extends XWExpandableListActivity
m_selectedGames.remove( rowid ); m_selectedGames.remove( rowid );
} }
} else if ( toggled instanceof GameListGroup ) { } else if ( toggled instanceof GameListGroup ) {
int position = ((GameListGroup)toggled).getGroupPosition(); long id = ((GameListGroup)toggled).getGroupID();
if ( selected ) { if ( selected ) {
m_selectedGroups.add( position ); m_selectedGroupIDs.add( id );
clearSelectedGames(); clearSelectedGames();
} else { } else {
m_selectedGroups.remove( position ); m_selectedGroupIDs.remove( id );
} }
} }
Utils.invalidateOptionsMenuIf( this ); Utils.invalidateOptionsMenuIf( this );
@ -619,7 +619,7 @@ public class GamesList extends XWExpandableListActivity
public boolean onPrepareOptionsMenu( Menu menu ) public boolean onPrepareOptionsMenu( Menu menu )
{ {
int nGamesSelected = m_selectedGames.size(); int nGamesSelected = m_selectedGames.size();
int nGroupsSelected = m_selectedGroups.size(); int nGroupsSelected = m_selectedGroupIDs.size();
m_menuPrepared = 0 == nGamesSelected || 0 == nGroupsSelected; m_menuPrepared = 0 == nGamesSelected || 0 == nGroupsSelected;
if ( m_menuPrepared ) { if ( m_menuPrepared ) {
boolean nothingSelected = 0 == (nGroupsSelected + nGamesSelected); boolean nothingSelected = 0 == (nGroupsSelected + nGamesSelected);
@ -645,7 +645,8 @@ public class GamesList extends XWExpandableListActivity
int selGroupPos = -1; int selGroupPos = -1;
if ( 1 == nGroupsSelected ) { if ( 1 == nGroupsSelected ) {
selGroupPos = m_selectedGroups.iterator().next(); long id = m_selectedGroupIDs.iterator().next();
selGroupPos = m_adapter.getGroupPosition( id );
} }
// You can't delete the default group, nor make it the default // You can't delete the default group, nor make it the default
@ -690,6 +691,7 @@ public class GamesList extends XWExpandableListActivity
Assert.assertTrue( m_menuPrepared ); Assert.assertTrue( m_menuPrepared );
boolean handled = true; boolean handled = true;
boolean changeContent = false;
int groupPos = getSelGroupPos(); int groupPos = getSelGroupPos();
long groupID = -1; long groupID = -1;
if ( 0 <= groupPos ) { if ( 0 <= groupPos ) {
@ -750,7 +752,7 @@ public class GamesList extends XWExpandableListActivity
case R.id.gamel_menu_loaddb: case R.id.gamel_menu_loaddb:
DBUtils.loadDB( this ); DBUtils.loadDB( this );
onContentChanged(); changeContent = true;
break; break;
case R.id.gamel_menu_storedb: case R.id.gamel_menu_storedb:
DBUtils.saveDB( this ); DBUtils.saveDB( this );
@ -800,6 +802,7 @@ public class GamesList extends XWExpandableListActivity
showOKOnlyDialog( R.string.cannot_delete_default_group ); showOKOnlyDialog( R.string.cannot_delete_default_group );
} else { } else {
long[] groupIDs = getSelGroupIDs(); long[] groupIDs = getSelGroupIDs();
Assert.assertTrue( 0 < groupIDs.length );
msg = getString( 1 == groupIDs.length ? R.string.group_confirm_del msg = getString( 1 == groupIDs.length ? R.string.group_confirm_del
: R.string.groups_confirm_del ); : R.string.groups_confirm_del );
int nGames = 0; int nGames = 0;
@ -823,14 +826,10 @@ public class GamesList extends XWExpandableListActivity
showDialog( RENAME_GROUP ); showDialog( RENAME_GROUP );
break; break;
case R.id.list_group_moveup: case R.id.list_group_moveup:
if ( m_adapter.moveGroup( groupID, -1 ) ) { changeContent = m_adapter.moveGroup( groupID, -1 );
onContentChanged();
}
break; break;
case R.id.list_group_movedown: case R.id.list_group_movedown:
if ( m_adapter.moveGroup( groupID, 1 ) ) { changeContent = m_adapter.moveGroup( groupID, 1 );
onContentChanged();
}
break; break;
default: default:
@ -841,6 +840,9 @@ public class GamesList extends XWExpandableListActivity
if ( handled ) { if ( handled ) {
clearSelections(); clearSelections();
} }
if ( changeContent ) {
onContentChanged();
}
return handled; return handled;
} }
@ -1121,7 +1123,7 @@ public class GamesList extends XWExpandableListActivity
if ( clearSelectedGroups() ) { if ( clearSelectedGroups() ) {
inval = true; inval = true;
} }
if ( !inval ) { if ( inval ) {
Utils.invalidateOptionsMenuIf( this ); Utils.invalidateOptionsMenuIf( this );
} }
} }
@ -1141,10 +1143,10 @@ public class GamesList extends XWExpandableListActivity
private boolean clearSelectedGroups() private boolean clearSelectedGroups()
{ {
// clear any selection // clear any selection
boolean needsClear = 0 < m_selectedGroups.size(); boolean needsClear = 0 < m_selectedGroupIDs.size();
if ( needsClear ) { if ( needsClear ) {
m_adapter.clearSelectedGroups( m_selectedGroups ); m_adapter.clearSelectedGroups( m_selectedGroupIDs );
m_selectedGroups.clear(); m_selectedGroupIDs.clear();
} }
return needsClear; return needsClear;
} }
@ -1227,19 +1229,20 @@ public class GamesList extends XWExpandableListActivity
private int getSelGroupPos() private int getSelGroupPos()
{ {
int result = -1; int result = -1;
if ( 1 == m_selectedGroups.size() ) { if ( 1 == m_selectedGroupIDs.size() ) {
result = m_selectedGroups.iterator().next(); long id = m_selectedGroupIDs.iterator().next();
result = m_adapter.getGroupPosition( id );
} }
return result; return result;
} }
private long[] getSelGroupIDs() private long[] getSelGroupIDs()
{ {
long[] result = new long[m_selectedGroups.size()]; long[] result = new long[m_selectedGroupIDs.size()];
int ii = 0; int ii = 0;
for ( Iterator<Integer> iter = m_selectedGroups.iterator(); for ( Iterator<Long> iter = m_selectedGroupIDs.iterator();
iter.hasNext(); ) { iter.hasNext(); ) {
result[ii++] = m_adapter.getGroupIDFor( iter.next() ); result[ii++] = iter.next();
} }
return result; return result;
} }