RecyclerView Adapter null after restoring Fragment

392 views Asked by At

I have a RecyclerView inside a fragment that is in a ViewPager. The problem is, when the view is restored, the RecyclerView is null and its adapter, which I need to update the contents, is also null.

How do I save the adapter state so that it isn't null when restoring the Fragment?

Code

//Inside fragment:
public override void OnResume()
{
        base.OnResume();

        (Activity as MainActivity).mLocation.ChangeFragments(this);
}
//Change Fragments calls this method:  
 private async void HomeLocation()
 {     
       JsonValue json;
        json = await GetZmanimAsync();

        (mFragment as HomeFragment).ParseAndDisplay(json);


            foreach (var time in (mFragment as HomeFragment).times)
            {
                time.GotTime = true;
            }

}

 public void ParseAndDisplay(JsonValue json)
    {

        if (times == null)
        {
            times = new List<TimeItem>();

            times.Add(new TimeItem() { TimeType = "עלות השחר", ImageResourceId = Resource.Drawable.alotIcon });
            times.Add(new TimeItem() { TimeType = "זמן טו\"ת", ImageResourceId = Resource.Drawable.tutIcon });

            times.Add(new TimeItem() { TimeType = "נץ החמה", ImageResourceId = Resource.Drawable.netzIcon });
            times.Add(new TimeItem() { TimeType = "סו\"ז ק\"ש", ImageResourceId = Resource.Drawable.shmaIcon });

            times.Add(new TimeItem() { TimeType = "סו\"ז תפילה", ImageResourceId = Resource.Drawable.DavenAnimation, isAnimation = true });
            times.Add(new TimeItem() { TimeType = "זמן מנחה", ImageResourceId = Resource.Drawable.DavenAnimation, isAnimation = true });

            times.Add(new TimeItem() { TimeType = "שקיעה", ImageResourceId = Resource.Drawable.shkiaaIcon });
            times.Add(new TimeItem() { TimeType = "צאת הכוכבים", ImageResourceId = Resource.Drawable.TzetIcon });
        }

        zman = DateTime.Parse((json[0] as JsonObject).Values.ToList()[1]);//"עלות השחר 72 ד"]);
        times[0].Time = zman.ToString("H:mm");
        zman = DateTime.Parse((json[2] as JsonObject).Values.ToList()[1]);//DateTime.Parse(json["זמן טלית ותפילין"]);
        times[1].Time = zman.ToString("H:mm");
        zman = DateTime.Parse((json[3] as JsonObject).Values.ToList()[1]);//DateTime.Parse(json["זריחה"]);
        times[2].Time = zman.ToString("H:mm");
        zman = DateTime.Parse((json[5] as JsonObject).Values.ToList()[1]);//DateTime.Parse(json["סוף זמן ק\"ש (גר\"א)"]);
        times[3].Time = zman.ToString("H:mm");
        zman = DateTime.Parse((json[7] as JsonObject).Values.ToList()[1]);//DateTime.Parse(json["סוף זמן תפילה (גר\"א"]);
        times[4].Time = zman.ToString("H:mm");
        zman = DateTime.Parse((json[9] as JsonObject).Values.ToList()[1]);//DateTime.Parse(json["מנחה גדולה"]);
        times[5].Time = zman.ToString("H:mm");
        zman = DateTime.Parse((json[12] as JsonObject).Values.ToList()[1]);//DateTime.Parse(json["שקיעה"]);
        times[6].Time = zman.ToString("H:mm");
        zman = DateTime.Parse((json[13] as JsonObject).Values.ToList()[1]);//DateTime.Parse(json["צאת הכוכבים"]);
        times[7].Time = zman.ToString("H:mm");


        mAdapter.NotifyDataSetChanged();


    }
0

There are 0 answers