我非常難以理解如何刷新ListAdapter我有一個AlertDialog活動,我從同一活動調用。刷新對話框中的ListAdapter
這裏的活動代碼:
private static ArrayAdapter<CarProfile> mainListAdapter;
public class CarProfiles : ListActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
mainListAdapter = new ArrayAdapter<CarProfile>(this, Android.Resource.Layout.SimpleListItem1, carProfiles);
// This targets a ListView in my axml with id list.
ListAdapter = mainListAdapter;
ShowCarProfileFormDialog(parameters blah, blah, blah);
}
}
這是我的AlertDialog:
public class CarProfileDialogFragment : DialogFragment
{
public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
LayoutInflater inflater = Activity.LayoutInflater;
View view = inflater.Inflate(Resource.Layout.CarProfileForm, null);
// component init (removed)
var builder = new AlertDialog.Builder(Activity)
.SetView(view)
.SetPositiveButton(GetString(Resource.String.lblCarProfileDialogOK), (sender, args) =>
{
// The datasouce source update works
datasource.UpdateCarProfile(id, txtName.Text, txtPlateNumber.Text, spnCategoryColor.SelectedItem.ToString(), spnCategoryNumber.SelectedItem.ToString());
// But this doesn't
mainListAdapter.NotifyDataSetChanged();
})
.SetNegativeButton(GetString(Resource.String.lblCarProfileDialogCancel), (sender, args) =>
{
Dialog.Dismiss();
})
.SetTitle(GetString(Resource.String.lblCarProfileDialogTitle));
return builder.Create();
}
}
顯示在前面AlertDialog的代碼,我datasouce得到沒有問題的更新,當我打電話了NotifyDataSetChanged
方法沒有任何反應。
是否曾經編譯?這個文件中有幾個java synxtax錯誤。 – Snicolas
這是monodroid。我不介意Java解決方案。 – Ron
我不能說MonoDroid,但在普通Java中,您應該使用基於事件的機制來允許您的片段與您的活動進行通信,然後再刷新列表。 http://stackoverflow.com/questions/10867425/communication-between-fragments-dialogs-in-android – Snicolas