I am facing a problem with a tutorial. I have been working with hardcoded data (working fine) and now I am trying to work with live data from firebase.
I already have some data in my database and I am trying to do a fetch request (get request) to update my RecyclerView
. Currently, my RecyclerView
is filled with the hardcoded data. What I want to achieve is to add the data in my firebase to update my RecyclerView
. basically.
Once the app is deployed on the phone and I try to save data, the function to save data works fine. I am able to see the data saved in real time in my firebase. But an Exception is thrown when it gets to the updateGameDataList function in my GameActivity.cs.
The exception points to this line:
recycle.SetAdapter(adpater);
and throws a System.NullReferenceException
.
What could I be missing or doing so wrong? Thank you
Games.cs
public class Games
{
private string GameName;
private int GameImage;
public string Name
{
get { return GameName; }
set { GameName = value; }
}
public int Image
{
get { return GameImage; }
set { GameImage = value; }
}
}
AllGames
// this is where I hardcoded my data for practice.
public class AllGames
{
public static JavaList<Games> GetAllGames()
{
JavaList<Games> getGames = new JavaList<Games>();
Games adventureGames = new Games();
adventureGames.Name = "Heroes";
adventureGames.Image = Resource.Drawable.game1;
getGames.Add(adventureGames);
}
}
GameFragment
public class GameFragment : SupportFragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// GameDataList contains recycleView to display all games
// inflated into List_AllGames
RecyclerView recyclerView = inflater.Inflate(Resource.Layout.GameDataList, container, false) as RecyclerView;
GameRecyclerView(recyclerView);
return recyclerView;
}
private void GameRecyclerView(RecyclerView recyclerView)
{
var bindGames = AllGames.GetAllGames();
recyclerView.SetLayoutManager(new LinearLayoutManager(recyclerView.Context));
var Adapter = new GameAdapter(AllGames.GetAllGames());
recyclerView.SetAdapter(Adapter);
Adapter.NotifyDataSetChanged();
recyclerView.SetItemClickListener((rv, position, view) =>
{
Context context = view.Context;
Intent intent = new Intent(context, typeof(GamesAttributesActivity));
context.StartActivity(intent);
});
}
// Game Adapter here - this is the same adapter I used
// to display the hard coded data
public class GameAdapter : RecyclerView.Adapter
{
private JavaList<Games> myGames;
public EventAdapter(JavaList<Games> myGames)
{
this.myGames = myGames;
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
GameHolder adventure = holder as GameHolder;
adventure.GameName.Text = myGames[position].Name;
adventure.GameImage.SetImageResource(peaceEvents[position].Image);
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
View games = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.List_AllGames, parent, false);
GameHolder holder = new GameHolder(games);
return holder;
}
public override int ItemCount
{
get { return myGames.Size(); }
}
}
//Same holder used to hold my hardcoded data
public class GameHolder : RecyclerView.ViewHolder
{
public TextView GameName;
public ImageView GameImage;
public GameHolder(View itemView) : base(itemView)
{
//binding to List_AllGames inflated in GameFragment
GameImage = itemView.FindViewById<ImageView>(Resource.Id.displayGameImage);
GameName = itemView.FindViewById<TextView>(Resource.Id.displayGameText);
}
}
GameActivity
private RecyclerView recycle;
JavaList<Games> NewGames = new JavaList<Games>();
private async void CreateNewGames()
{
//code to add games
var firebase = new FirebaseClient(FirebaseUrl);
//add item
var item = await firebase.Child("Games").PostAsync<PeaceEvents>(addGames);
await UpdateGameDataList();
}
//where recycleView must update new games added to the list
private async Task UpdateGameDataList()
{
var firebase = new FirebaseClient(FirebaseUrl);
var items = await firebase.Child("Games").OnceAsync<PeaceEvents>();
NewGames.Clear();
GameAdapter adapter;
foreach(var item in items)
{
Games fetchGames = new Games();
fetchGames.Name = item.Object.Name;
NewGames.Add(fetchGames);
}
//fetching the id of the GameDataList recycleView
recycle = FindViewById<RecyclerView>(Resource.Id.recyclerview);
adapter = new GameAdapter(NewGames);
recycle.SetAdapter(adapter);
recycle.SetLayoutManager(new LinearLayoutManager(this));
adapter.NotifyDataSetChanged();
}