簡體   English   中英

更新 SQLite Xamarin Android 上的數據后,刷新來自活動的片段

[英]Refresh Fragment From Activity After Updating Data on SQLite Xamarin Android

更新Fragment_profile上的用戶數據時,我的 Android 程序出現問題。 Fragment_Profile 加載用戶配置文件數據並包含一個更新按鈕,其中該按鈕導航到名為EditProfileActivity的活動。 用戶在技術上可以更新數據,但保存更新后的數據后,數據仍然無法更新。 舊數據仍然出現。 我嘗試使用 OnResume() 刷新片段,並在片段和活動中的 EditProfileActivity.cs 和 OnPause 上添加 OnRestart,但仍然沒有。 我正在使用 Xamarin Android 並使用 C# 開發它。 有關更多信息,您可以在 GIF我的問題中查看發生了什么

到目前為止,我已經嘗試過編碼,這是我的代碼。 Fragment_profile.cs

public class Fragment_Profile : Android.Support.V4.App.Fragment
    {
        public HomePageActivity m_currentActivity;

        public TextView m_tv_loginname, m_tv_username, m_tv_fullname , m_tv_dob;

        public Boolean isRefreshing = false;

        public override void OnCreate(Bundle aSavedInstanceState)
        {
            base.OnCreate(aSavedInstanceState);
        }

        //public static Fragment_Profile NewInstance(Model.User aCurrentUser)
        public static Fragment_Profile NewInstance()
        {
            var _frag4 = new Fragment_Profile { Arguments = new Bundle() };
            return _frag4;
        }

        public override View OnCreateView(LayoutInflater aInflater, ViewGroup aContainer, Bundle aSavedInstanceState)
        {
            var _ignored = base.OnCreateView(aInflater, aContainer, aSavedInstanceState);
            var view= aInflater.Inflate(Resource.Layout.FragmentProfile, null);

            m_tv_loginname = view.FindViewById<TextView>(Resource.Id.tv_loginname);
            m_tv_username = view.FindViewById<TextView>(Resource.Id.tv_userEmail);
            m_tv_fullname = view.FindViewById<TextView>(Resource.Id.tv_fullname);
            m_tv_dob = view.FindViewById<TextView>(Resource.Id.tv_dob);


            Button _updateProfile = view.FindViewById<Button>(Resource.Id.btnUpdateProfile);

            _updateProfile.Click += _updateProfile_Click;

            m_currentActivity = (HomePageActivity)this.Activity;

            if (m_currentActivity.CurrentUser != null)
            {
                //string  = "Welcome, " + m_currentActivity.CurrentUser.UserName;
                m_tv_loginname.Text = m_currentActivity.CurrentUser.LoginName;
                m_tv_fullname.Text = m_currentActivity.CurrentUser.UserName;
                m_tv_username.Text = m_currentActivity.CurrentUser.UserEmail;
                m_tv_dob.Text = m_currentActivity.CurrentUser.DateOfBirth;
            }

            else
            {
                Toast.MakeText(Activity, "The data is not found!", ToastLength.Short).Show();
                Intent i = new Intent(Context, typeof(MainActivity));
                StartActivity(i);
                this.Activity.Finish();
            }


            return view;
        }

        private void _updateProfile_Click(object sender, EventArgs e)
        {
            Intent i = new Intent(Context, typeof(EditProfileActivity));
            i.PutExtra("loginname", m_currentActivity.CurrentUser.LoginName);
            i.PutExtra("fullname", m_currentActivity.CurrentUser.UserName);
            i.PutExtra("useremail", m_currentActivity.CurrentUser.UserEmail);
            i.PutExtra("dob", m_currentActivity.CurrentUser.DateOfBirth);
            StartActivity(i);
        }

        public override void OnResume()
        {
            base.OnResume();
            if (isRefreshing)
            {
                Fragment fragment = new Fragment_Profile();
                Android.Support.V4.App.FragmentManager fragmentMg = Activity.SupportFragmentManager;
                FragmentTransaction fragmentTrans = fragmentMg.BeginTransaction();
                fragmentTrans.Replace(Resource.Id.content_frame, fragment);
                fragmentTrans.Detach(fragment);
                fragmentTrans.Attach(fragment);
                fragmentTrans.Commit();

                //adapter.notifyDataSetChanged();
            }
        }

        public override void OnPause()
        {
            base.OnPause();
                isRefreshing = true;
        }
    }

當用戶單擊_updateProfile button時,它將引用下一個活動,即 EditProfileActivity。 這是我的EditProfileActivity.cs

 public class EditProfileActivity : Activity, IOnDateSetListener
    {
        public EditText m_editFullName, m_editUsername, m_dob;
        public TextView m_tvEmail;
        public Button m_btnUpdate;
        public Boolean isRefreshing = false;



        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.EditProfile);

            m_editFullName = FindViewById<EditText>(Resource.Id.et_editfullName);
             m_editUsername = FindViewById<EditText>(Resource.Id.et_editUserName);
            m_tvEmail = FindViewById<TextView>(Resource.Id.tv_Email);
             m_dob = FindViewById<EditText>(Resource.Id.et_editDob);
            m_btnUpdate = FindViewById<Button>(Resource.Id.btn_updateprofile);

            m_btnUpdate.Click += _btnUpdate_Click;

            m_dob.Click += _dob_Click;

            Bundle extras = Intent.Extras;
            m_editFullName.Text = extras.GetString("loginname");
            m_editUsername.Text = extras.GetString("fullname");
            m_tvEmail.Text = extras.GetString("useremail");
            m_dob.Text = extras.GetString("dob");


        }

        private void _btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                Model.User _currentUser = Model.User.CheckEmailUser(m_tvEmail.Text);
                _currentUser.UserName = m_editUsername.Text;
                _currentUser.LoginName = m_editFullName.Text;
                _currentUser.UserEmail = m_tvEmail.Text;
                _currentUser.DateOfBirth = m_dob.Text;

               var _updated =  DBManager.Instance.Update(_currentUser);

                if (_updated > 0)
                {

                    Toast.MakeText(this, "Your account has been succesfully updated!", ToastLength.Long).Show();
                    Finish();
                    OnResume();
                }

                else
                {
                       Toast.MakeText(this, "Failed to update!", ToastLength.Long).Show();
                }

            }

            catch(Exception ex)
            {
                Toast.MakeText(this, ex.ToString(), ToastLength.Short).Show();
            }
        }

        private void _dob_Click(object sender, EventArgs e)
        {
            var _dateTimeNow = DateTime.Now;
            DatePickerDialog _datepicker = new DatePickerDialog(this, this
                , _dateTimeNow.Year, _dateTimeNow.Month, _dateTimeNow.Day);
            _datepicker.Show();
        }

        public void OnDateSet(DatePicker view, int year, int month, int dayOfMonth)
        {
            m_dob.Text = new DateTime(year, month + 1, dayOfMonth).ToShortDateString();
        }

        protected override void OnRestart()
        {
            base.OnRestart();
            if (isRefreshing)
            {
                isRefreshing = false;
                Finish();
            }
        }

        protected override void OnPause()
        {
            base.OnPause();
            if (!isRefreshing)
                isRefreshing = true;
        }


    }

我也讀過一些與我有同樣問題的文章,但是,它仍然讓我感到困惑並且仍然一樣。 我知道這是一件簡單的事情,但是,我花了幾天的時間,因為我是新手,但我仍然沒有得到解決方案。 你們有什么想法嗎? 你願意幫助我嗎? 如果您不介意,請檢查我的源代碼,我知道我缺少什么。 提前感謝您的幫助!

在您的 Fragment 中,您可以覆蓋OnResume方法。 如果你回到 Acitivty 的片段。 您應該從 DB 中查詢新數據,然后將新值設置為 Fragment 中的控件。

 public override void OnResume()
        {
            base.OnResume();
            PHName.Text = photoDAO.GetFirstPhotos(Id).PhotoName;
        }

這里正在運行 GIF。

在此處輸入圖像描述

你可以看到這個關於 Fragment 的生命周期。 每次顯示 Fragment 時,都會執行OnResume方法。 您從 DB 獲取最新數據,然后將其設置到Fragment頁面。 在此處輸入圖像描述

這是我的演示。

https://drive.google.com/file/d/11dROKS7TtqAaVYkG8w6ZKpqnQJRBD87E/view

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM