still getting weirdness with multichoiceitems

This commit is contained in:
mgroves 2011-01-03 23:10:54 -05:00
parent 78f76cbc49
commit 795f1ec3f8

View file

@ -15,9 +15,7 @@ namespace MonoStockPortfolio.Activites
public class ConfigActivity : PreferenceActivity
{
[IoC] private IConfigRepository _repo;
private bool[] _allitemschecked;
private StockDataItem[] _allStockItems;
private StockItemPreference[] _stockItemsConfig;
protected override void OnCreate(Bundle bundle)
{
@ -25,9 +23,7 @@ namespace MonoStockPortfolio.Activites
AddPreferencesFromResource(Resource.layout.config);
_allStockItems = StockDataItem.Change.GetValues<StockDataItem>().ToArray();
var checkedItems = _repo.GetStockItems();
_allitemschecked = _allStockItems.Select(i => checkedItems.Contains(i)).ToArray();
_stockItemsConfig = StockItemPreference.BuildList(_repo.GetStockItems()).ToArray();
var customPref = FindPreference("customStockItems");
customPref.PreferenceClick += customPref_PreferenceClick;
@ -35,35 +31,40 @@ namespace MonoStockPortfolio.Activites
bool customPref_PreferenceClick(Preference preference)
{
var stockItemsDisplay = _allStockItems.Select(i => i.GetStringValue()).Cast<IEnumerable<char>>().ToArray();
var dialog = new AlertDialog.Builder(this)
.SetTitle("Select columns")
.SetMultiChoiceItems(stockItemsDisplay, _allitemschecked, clickCallback)
.SetPositiveButton("Save", okCallback)
.Create();
dialog.Show();
IEnumerable<char>[] stockItemsDisplay = _stockItemsConfig.OrderBy(i => i.StockDataItem).Select(i => i.StockDataItem.GetStringValue()).ToArray();
bool[] allitemschecked = _stockItemsConfig.OrderBy(i => i.StockDataItem).Select(i => i.IsChecked).ToArray();
var dialog = new AlertDialog.Builder(this);
dialog.SetMultiChoiceItems(stockItemsDisplay, allitemschecked, clickCallback);
dialog.SetTitle("Select columns");
dialog.SetPositiveButton("Save", okCallback);
dialog.Create().Show();
return true;
}
private void okCallback(object sender, DialogClickEventArgs e)
{
var list = new List<StockDataItem>();
for (int i = 0; i < _allitemschecked.Length; i++)
{
if (_allitemschecked[i])
{
list.Add(_allStockItems[i]);
}
}
var list = _stockItemsConfig.Where(i => i.IsChecked).Select(i => i.StockDataItem).ToList();
_repo.UpdateStockItems(list);
}
private void clickCallback(object sender, DialogMultiChoiceClickEventArgs e)
{
_allitemschecked[e.Which] = e.IsChecked;
_stockItemsConfig[e.Which].IsChecked = e.IsChecked;
}
public static string ClassName { get { return "monostockportfolio.activites.ConfigActivity"; } }
private class StockItemPreference
{
public static IEnumerable<StockItemPreference> BuildList(IEnumerable<StockDataItem> checkedItems)
{
var allitems = StockDataItem.Change.GetValues<StockDataItem>();
return allitems.Select(item => new StockItemPreference {StockDataItem = item, IsChecked = checkedItems.Contains(item)});
}
public StockDataItem StockDataItem { get; private set; }
public bool IsChecked { get; set; }
}
}
}