Thisi是代碼:
public List<DeliveryNoteInfo> _lstNoteInfo = new List<DeliveryNoteInfo>();
public ObjectToSerialize objectToSerialize;
public Serializer serializer;
private ListView objListItem;
private MonoBaseAdapter2 myAdapter = null;
static AlertDialog levelDialog;
static string[] items;
static Context context2;
protected override void OnCreate (Bundle SavedInstanceState)
{
base.OnCreate (SavedInstanceState);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
// Get our UI controls from the loaded layout
Button addButton = FindViewById<Button>(Resource.Id.AddButton);
addButton.Click += delegate {
StartActivity (typeof(AddActivity));
};
Button sortButton = FindViewById<Button>(Resource.Id.SortButton);
sortButton.Click += delegate {
ShowSortDialog(_lstNoteInfo);
};
Button exitButton = FindViewById<Button>(Resource.Id.ExitButton);
exitButton.Click += delegate {
Finish();
};
context2 = this;
// Loading the already saved notes from external file
objectToSerialize = new ObjectToSerialize();
serializer = new Serializer();
var path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
if (System.IO.File.Exists (path + "outputFile.txt")) {
deserialize();
}
//Displaying the Notes in a custom made ListView
objListItem = FindViewById<ListView> (Resource.Id.listView1);
myAdapter = new MonoBaseAdapter2 (this, _lstNoteInfo);
objListItem.Adapter = myAdapter;
objListItem.FastScrollEnabled = true;
objListItem.ChoiceMode = (ChoiceMode)AbsListViewChoiceMode.MultipleModal;
objListItem.SetMultiChoiceModeListener (new CabMenu(this,this,objListItem));
objListItem.ItemClick += delegate(object sender, AdapterView.ItemClickEventArgs e) {
var itemClicked = new Intent(this,typeof(InfoActivity));
itemClicked.PutExtra("File name",_lstNoteInfo[e.Position].ImageID);
itemClicked.PutExtra("Date",_lstNoteInfo[e.Position].Date);
itemClicked.PutExtra("Type",_lstNoteInfo[e.Position].Type);
itemClicked.PutExtra("Name",_lstNoteInfo[e.Position].Name);
itemClicked.PutExtra("Supplier",_lstNoteInfo[e.Position].Supplier);
itemClicked.PutExtra("Amount",_lstNoteInfo[e.Position].Amount);
itemClicked.PutExtra("Info",_lstNoteInfo[e.Position].AddInfo);
StartActivity (itemClicked);
};
}
protected override void OnResume()
{
base.OnResume();
var lstNoteInfo = new List<DeliveryNoteInfo>();
var path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
if (System.IO.File.Exists (path + "outputFile.txt")) {
objectToSerialize = serializer.DeSerializeObject ("outputFile.txt");
lstNoteInfo = objectToSerialize.DeliveryNoteInfo;
}
_lstNoteInfo.Clear();
_lstNoteInfo.AddRange (lstNoteInfo);
((MonoBaseAdapter2)objListItem.Adapter).NotifyDataSetChanged();
objListItem.Invalidate();
((MonoBaseAdapter2)objListItem.Adapter).NotifyDataSetInvalidated();
}
public void makeNote (String fileName, String finalDate, String type,String noteName, String amount, String supplier, String addInfo)
{
_lstNoteInfo.Add (new DeliveryNoteInfo(){ ImageID = fileName, Date = finalDate,Type = type, Name = noteName, Amount = amount, Supplier = supplier, AddInfo = addInfo});
objectToSerialize.DeliveryNoteInfo = _lstNoteInfo;
serializer.SerializeObject("outputFile.txt", objectToSerialize);
Console.WriteLine ("Name: " + noteName);
Console.WriteLine ("Amount: " + amount);
}
public void RefreshAfterDelete(List<DeliveryNoteInfo> updatedList)
{
_lstNoteInfo.Clear();
_lstNoteInfo.AddRange (updatedList);
objectToSerialize.DeliveryNoteInfo = _lstNoteInfo;
serializer.SerializeObject("outputFile.txt", objectToSerialize);
UpdateList();
}
public void PrintCollection<T>(IEnumerable<T> col)
{
foreach(var item in col)
System.Console.WriteLine("ITEM");
}
void deserialize()
{
objectToSerialize = serializer.DeSerializeObject ("outputFile.txt");
_lstNoteInfo = objectToSerialize.DeliveryNoteInfo;
}
void deserialize(List<DeliveryNoteInfo> list)
{
objectToSerialize = serializer.DeSerializeObject ("outputFile.txt");
list = objectToSerialize.DeliveryNoteInfo;
}
void serialize (List<DeliveryNoteInfo> list)
{
objectToSerialize.DeliveryNoteInfo = list;
serializer.SerializeObject("outputFile.txt", objectToSerialize);
}
private void ShowSortDialog(List<DeliveryNoteInfo> updatedList)
{
// Strings to Show In Dialog with Radio Buttons
items = new string[]{" Date "," Name "," Type of delivery "," Supplier "};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.SetTitle("Sort by ..");
builder.SetSingleChoiceItems(items, -1, onClick);
levelDialog = builder.Create();
levelDialog.Show();
}
public void onClick(object sender, DialogClickEventArgs e) {
var item = (int) e.Which;
List<DeliveryNoteInfo> list = new List<DeliveryNoteInfo>();
switch(item)
{
case 0:
// Your code when first option seletced
Console.WriteLine ("Sorting by date..");
break;
case 1:
// Sorting by name
objectToSerialize = serializer.DeSerializeObject ("outputFile.txt");
list = objectToSerialize.DeliveryNoteInfo;
List<DeliveryNoteInfo> SortedList = list.OrderBy (o => o.Name).ToList();
RefreshAfterDelete (SortedList);
Console.WriteLine ("Sorting by name..");
break;
case 2:
// Your code when 3rd option seletced
Console.WriteLine ("Sorting by type of delivery..");
break;
case 3:
// Your code when 4th option seletced
Console.WriteLine ("Sorting by supplier ..");
break;
}
levelDialog.Dismiss();
}
private void ListClicked (object sender, DialogClickEventArgs e)
{
var builder = new AlertDialog.Builder (this);
builder.SetMessage (string.Format ("You selected: {0} , {1}", (int)e.Which, items [(int)e.Which]));
builder.Show();
}
private void UpdateList()
{
myAdapter.NotifyDataSetChanged();
}
}
'NotifyDataSetChanged'最肯定的運作'OnCreate' /'OnResume'覆蓋之外。 – Johan 2014-09-30 12:50:08
是的,它的工作原理,但我已經在onCreate方法中初始化我的適配器..我不知道如何在onCreate()方法外使用它。 – 2014-09-30 12:51:24
看到我的答案,這是一個小例子,但會起作用。 – Johan 2014-09-30 13:00:01