diff --git a/src/com/onirica/carrousel/Configuration.java b/src/com/onirica/carrousel/Configuration.java index 0e432a1..6600e3b 100644 --- a/src/com/onirica/carrousel/Configuration.java +++ b/src/com/onirica/carrousel/Configuration.java @@ -44,6 +44,11 @@ public class Configuration extends ListActivity { @Override public void onServiceConnected(ComponentName className, IBinder service) { mResults = ((Results.LocalBinder)service).getService(); + subscribedMatches = mResults.getSubscriptions(); + if (subscribedMatches == null) { + subscribedMatches = new HashSet(); + } else + mSubscribeButton.setEnabled(true); populateMatches(); progressDialog.dismiss(); @@ -106,12 +111,13 @@ public class Configuration extends ListActivity { private CheckBox mCb; private CompoundButton.OnCheckedChangeListener mListener = null; - public MatchView(Context context, String text) { + public MatchView(Context context, String text, boolean isChecked) { super(context); this.setOrientation(HORIZONTAL); mTv = new TextView(context); mTv.setText(text); mCb = new CheckBox(context); + setChecked(isChecked); mCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (mListener != null) { @@ -127,6 +133,10 @@ public class Configuration extends ListActivity { mTv.setText(text); } + public void setChecked(boolean isChecked) { + mCb.setChecked(isChecked); + } + public void setOnMatchCheckedChanged(CompoundButton.OnCheckedChangeListener listener) { mListener = listener; } @@ -152,14 +162,25 @@ public class Configuration extends ListActivity { public long getItemId(int pos) { return pos; } + @Override public View getView(int pos, View convertView, ViewGroup parent) { MatchView v; + Match match = mMatches[pos]; + boolean isSubscribed = subscribedMatches.contains(match.getId()); + if (convertView == null) { - v = new MatchView(parent.getContext(), mMatches[pos].toString()); +; v = new MatchView(parent.getContext(), match.toString(), isSubscribed); v.setOnMatchCheckedChanged(new OnMatchCheckedListener(pos)); } else { v = (MatchView)convertView; - v.setText(mMatches[pos].toString()); + v.setText(match.toString()); + // This is tricky: We are reusing the view for a different match. + // We need to update the view checkbox state and we use setChecked, + // but this would trigger the previous event listener, clearing + // the model of the previous item shown with this view. + v.setOnMatchCheckedChanged(null); + v.setChecked(isSubscribed); + v.setOnMatchCheckedChanged(new OnMatchCheckedListener(pos)); } return v; } @@ -173,9 +194,10 @@ public class Configuration extends ListActivity { Match m = (Match)getItem(mPosition); if (m != null) { if (isChecked) - subscribedMatches.remove(mMatches[mPosition].getId()); - else subscribedMatches.add(mMatches[mPosition].getId()); + else + subscribedMatches.remove(mMatches[mPosition].getId()); + mSubscribeButton.setEnabled(!subscribedMatches.isEmpty()); } } diff --git a/src/com/onirica/carrousel/Results.java b/src/com/onirica/carrousel/Results.java index 210b01d..607c3c2 100644 --- a/src/com/onirica/carrousel/Results.java +++ b/src/com/onirica/carrousel/Results.java @@ -23,6 +23,11 @@ public class Results extends Service { public void updateSubscriptions(HashSet subscriptions) { mSubscriptions = subscriptions; } + + public HashSet getSubscriptions() { + return mSubscriptions; + } + public class LocalBinder extends Binder { Results getService() {