fix stupid listadapters, and preserve selection for remotedicts list too

This commit is contained in:
Eric House 2014-06-11 19:07:05 -07:00
parent 6de515e75e
commit ac2c960ef5
2 changed files with 84 additions and 100 deletions

View file

@ -29,9 +29,6 @@ import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Resources;
import android.database.DataSetObserver;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.text.TextUtils; import android.text.TextUtils;
@ -42,11 +39,9 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.Button; import android.widget.Button;
import android.widget.ExpandableListAdapter;
import android.widget.ListView; import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
@ -56,8 +51,6 @@ import junit.framework.Assert;
import org.eehouse.android.xw4.DlgDelegate.Action; import org.eehouse.android.xw4.DlgDelegate.Action;
import org.eehouse.android.xw4.DictUtils.DictAndLoc; import org.eehouse.android.xw4.DictUtils.DictAndLoc;
import org.eehouse.android.xw4.jni.XwJNI;
import org.eehouse.android.xw4.jni.JNIUtilsImpl;
import org.eehouse.android.xw4.jni.GameSummary; import org.eehouse.android.xw4.jni.GameSummary;
import org.eehouse.android.xw4.DictUtils.DictLoc; import org.eehouse.android.xw4.DictUtils.DictLoc;
@ -86,8 +79,7 @@ public class DictsDelegate extends ListDelegateBase
private class DictListAdapter extends XWListAdapter { private class DictListAdapter extends XWListAdapter {
private Context m_context; private Context m_context;
private Integer m_count = null; private Object[] m_listInfo;
// private XWListItem[][] m_cache;
public DictListAdapter( Context context ) public DictListAdapter( Context context )
{ {
@ -98,10 +90,12 @@ public class DictsDelegate extends ListDelegateBase
@Override @Override
public int getCount() public int getCount()
{ {
if ( null == m_count ) { if ( null == m_listInfo ) {
ArrayList<Object> alist = new ArrayList<Object>();
int nLangs = m_langs.length; int nLangs = m_langs.length;
int result = nLangs;
for ( int ii = 0; ii < nLangs; ++ii ) { for ( int ii = 0; ii < nLangs; ++ii ) {
alist.add( new Integer(ii) );
String langName = m_langs[ii]; String langName = m_langs[ii];
if ( m_closedLangs.contains( langName ) ) { if ( m_closedLangs.contains( langName ) ) {
continue; continue;
@ -109,12 +103,14 @@ public class DictsDelegate extends ListDelegateBase
int lang = DictLangCache.getLangLangCode( m_context, langName ); int lang = DictLangCache.getLangLangCode( m_context, langName );
DictAndLoc[] dals = DictLangCache.getDALsHaveLang( m_context, lang ); DictAndLoc[] dals = DictLangCache.getDALsHaveLang( m_context, lang );
if ( null != dals ) { if ( null != dals ) {
result += dals.length; for ( DictAndLoc dal : dals ) {
alist.add( dal );
}
} }
} }
m_count = new Integer( result ); m_listInfo = alist.toArray( new Object[alist.size()] );
} }
return m_count; return m_listInfo.length;
} }
@Override @Override
@ -124,52 +120,41 @@ public class DictsDelegate extends ListDelegateBase
public View getView( final int position, View convertView, ViewGroup parent ) public View getView( final int position, View convertView, ViewGroup parent )
{ {
View result = null; View result = null;
int indx = position;
for ( int ii = 0; ii < m_langs.length; ++ii ) { Object obj = m_listInfo[position];
String langName = m_langs[ii]; if ( obj instanceof Integer ) {
int groupPos = (Integer)obj;
String langName = m_langs[groupPos];
int langCode = DictLangCache.getLangLangCode( m_context, int langCode = DictLangCache.getLangLangCode( m_context,
langName ); langName );
boolean expanded = ! m_closedLangs.contains( langName ); boolean expanded = ! m_closedLangs.contains( langName );
if ( indx == 0 ) { result = ListGroup.make( m_context, DictsDelegate.this, groupPos,
result = ListGroup.make( m_context, DictsDelegate.this, ii, langName, expanded );
langName, expanded ); } else if ( obj instanceof DictAndLoc ) {
break; DictAndLoc dal = (DictAndLoc)obj;
} else { XWListItem item =
DictAndLoc[] dals = XWListItem.inflate( m_activity, DictsDelegate.this );
DictLangCache.getDALsHaveLang( m_context, langCode ); result = item;
int count = expanded ? dals.length : 0;
if ( indx <= count ) {
XWListItem item =
XWListItem.inflate( m_activity, DictsDelegate.this );
result = item;
DictAndLoc dal = dals[indx - 1]; String name = dal.name;
String name = dal.name; item.setText( name );
item.setText( name );
DictLoc loc = dal.loc; DictLoc loc = dal.loc;
item.setComment( m_locNames[loc.ordinal()] ); item.setComment( m_locNames[loc.ordinal()] );
item.cache( loc ); item.cache( loc );
item.setOnClickListener( DictsDelegate.this ); item.setOnClickListener( DictsDelegate.this );
// Replace sel entry if present // Replace sel entry if present
if ( m_selDicts.containsKey( name ) ) { if ( m_selDicts.containsKey( name ) ) {
m_selDicts.put( name, item ); m_selDicts.put( name, item );
item.setSelected( true ); item.setSelected( true );
}
break;
}
indx -= 1 + count;
} }
} else {
Assert.fail();
} }
return result; return result;
} }
// public boolean areAllItemsEnabled() { return false; }
} }
protected DictsDelegate( ListActivity activity, Bundle savedInstanceState ) protected DictsDelegate( ListActivity activity, Bundle savedInstanceState )

View file

@ -32,7 +32,6 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.json.JSONArray; import org.json.JSONArray;
@ -55,14 +54,15 @@ public class RemoteDictsDelegate extends ListDelegateBase
private static class DictInfo implements Comparable { private static class DictInfo implements Comparable {
public String m_name; public String m_name;
public DictState m_state; public DictState m_state;
public DictInfo( String name ) { m_name = name; } public String m_lang;
public DictInfo( String name, String lang ) { m_name = name; m_lang = lang; }
public int compareTo( Object obj ) { public int compareTo( Object obj ) {
DictInfo other = (DictInfo)obj; DictInfo other = (DictInfo)obj;
return m_name.compareTo( other.m_name ); return m_name.compareTo( other.m_name );
} }
} }
private HashMap<String, DictInfo[]> m_langInfo; private HashMap<String, DictInfo[]> m_langInfo;
private HashSet<XWListItem> m_selDicts = new HashSet<XWListItem>(); private HashMap<String, XWListItem> m_selDicts = new HashMap<String, XWListItem>();
private String m_origTitle; private String m_origTitle;
protected RemoteDictsDelegate( ListActivity activity, Bundle savedInstanceState ) protected RemoteDictsDelegate( ListActivity activity, Bundle savedInstanceState )
@ -104,7 +104,7 @@ public class RemoteDictsDelegate extends ListDelegateBase
case R.id.remote_dicts_download: case R.id.remote_dicts_download:
String[] urls = new String[m_selDicts.size()]; String[] urls = new String[m_selDicts.size()];
int count = 0; int count = 0;
for ( Iterator<XWListItem> iter = m_selDicts.iterator(); for ( Iterator<XWListItem> iter = m_selDicts.values().iterator();
iter.hasNext(); ) { iter.hasNext(); ) {
XWListItem litm = iter.next(); XWListItem litm = iter.next();
String langName = (String)litm.getCached(); String langName = (String)litm.getCached();
@ -158,10 +158,11 @@ public class RemoteDictsDelegate extends ListDelegateBase
public void itemToggled( LongClickHandler toggled, boolean selected ) public void itemToggled( LongClickHandler toggled, boolean selected )
{ {
XWListItem item = (XWListItem)toggled; XWListItem item = (XWListItem)toggled;
String name = item.getText();
if ( selected ) { if ( selected ) {
m_selDicts.add( item ); m_selDicts.put( name, item );
} else { } else {
m_selDicts.remove( item ); m_selDicts.remove( name );
} }
invalidateOptionsMenuIf(); invalidateOptionsMenuIf();
setTitleBar(); setTitleBar();
@ -170,7 +171,7 @@ public class RemoteDictsDelegate extends ListDelegateBase
public boolean getSelected( LongClickHandler obj ) public boolean getSelected( LongClickHandler obj )
{ {
XWListItem item = (XWListItem)obj; XWListItem item = (XWListItem)obj;
return m_selDicts.contains( item ); return m_selDicts.containsKey( item.getText() );
} }
////////////////////////////////////////////////// //////////////////////////////////////////////////
@ -224,7 +225,7 @@ public class RemoteDictsDelegate extends ListDelegateBase
JSONObject dict = dicts.getJSONObject( jj ); JSONObject dict = dicts.getJSONObject( jj );
String name = dict.getString( "xwd" ); String name = dict.getString( "xwd" );
name = DictUtils.removeDictExtn( name ); name = DictUtils.removeDictExtn( name );
DictInfo info = new DictInfo( name ); DictInfo info = new DictInfo( name, langName );
if ( DictLangCache.haveDict( m_activity, langName, name ) ) { if ( DictLangCache.haveDict( m_activity, langName, name ) ) {
boolean matches = true; boolean matches = true;
String curSum = DictLangCache.getDictMD5Sum( m_activity, name ); String curSum = DictLangCache.getDictMD5Sum( m_activity, name );
@ -303,9 +304,6 @@ public class RemoteDictsDelegate extends ListDelegateBase
if ( null != post ) { if ( null != post ) {
String json = null; String json = null;
json = UpdateCheckReceiver.runPost( post, m_params ); json = UpdateCheckReceiver.runPost( post, m_params );
// if ( null == json ) {
// json = s_fakeData;
// }
success = digestData( json ); success = digestData( json );
} }
return new Boolean( success ); return new Boolean( success );
@ -324,9 +322,9 @@ public class RemoteDictsDelegate extends ListDelegateBase
} }
private class RDListAdapter extends XWListAdapter { private class RDListAdapter extends XWListAdapter {
private Integer m_count = null;
private String m_installed; private String m_installed;
private String m_needsUpdate; private String m_needsUpdate;
private Object[] m_listInfo;
public RDListAdapter() public RDListAdapter()
{ {
@ -338,18 +336,21 @@ public class RemoteDictsDelegate extends ListDelegateBase
@Override @Override
public int getCount() public int getCount()
{ {
if ( null == m_count ) { if ( null == m_listInfo ) {
ArrayList<Object> alist = new ArrayList<Object>();
int nLangs = m_langNames.length; int nLangs = m_langNames.length;
int count = nLangs;
for ( int ii = 0; ii < nLangs; ++ii ) { for ( int ii = 0; ii < nLangs; ++ii ) {
alist.add( new Integer(ii) );
if ( m_expanded[ii] ) { if ( m_expanded[ii] ) {
count += m_langInfo.get( m_langNames[ii] ).length; for ( DictInfo di : m_langInfo.get( m_langNames[ii] ) ) {
alist.add( di );
}
} }
} }
m_count = new Integer( count ); m_listInfo = alist.toArray( new Object[alist.size()] );
} }
// DbgUtils.logf( "RemoteDictsDelegate.getCount() => %d", m_count ); // DbgUtils.logf( "RemoteDictsDelegate.getCount() => %d", m_listInfo.length );
return m_count; return m_listInfo.length;
} }
@Override @Override
@ -363,45 +364,43 @@ public class RemoteDictsDelegate extends ListDelegateBase
position, convertView ); position, convertView );
} }
View result = null; View result = null;
int indx = position;
for ( int ii = 0; ii < m_langNames.length; ++ii ) { Object obj = m_listInfo[position];
if ( obj instanceof Integer ) {
int ii = (Integer)obj;
String langName = m_langNames[ii]; String langName = m_langNames[ii];
boolean expanded = m_expanded[ii]; boolean expanded = m_expanded[ii];
if ( indx == 0 ) { result = ListGroup.make( m_activity, RemoteDictsDelegate.this,
result = ListGroup.make( m_activity, RemoteDictsDelegate.this, ii, langName, expanded );
ii, langName, expanded ); } else if ( obj instanceof DictInfo ) {
break; DictInfo info = (DictInfo)obj;
} else { XWListItem item =
DictInfo[] dicts = m_langInfo.get( langName ); XWListItem.inflate( m_activity, RemoteDictsDelegate.this );
int count = expanded ? dicts.length : 0; result = item;
if ( indx <= count ) {
XWListItem item =
XWListItem.inflate( m_activity, RemoteDictsDelegate.this );
result = item;
DictInfo info = dicts[indx-1]; String name = info.m_name;
item.setText( info.m_name ); item.setText( name );
if ( null != info.m_state ) { if ( null != info.m_state ) {
String comment = null; String comment = null;
switch( info.m_state ) { switch( info.m_state ) {
case INSTALLED: case INSTALLED:
comment = m_installed; comment = m_installed;
break; break;
case NEEDS_UPDATE: case NEEDS_UPDATE:
comment = m_needsUpdate; comment = m_needsUpdate;
break;
}
item.setComment( comment );
}
item.cache( langName );
//item.setOnClickListener( RemoteDictsDelegate.this );
break; break;
} }
indx -= 1 + count; item.setComment( comment );
} }
item.cache( info.m_lang );
if ( m_selDicts.containsKey( name ) ) {
m_selDicts.put( name, item );
item.setSelected( true );
}
} else {
Assert.fail();
} }
Assert.assertNotNull( result ); Assert.assertNotNull( result );