locked
Xamarin forms Firebase Database to listview RRS feed

  • Question

  • User385011 posted

    Hello,

    I'm trying to get data from Firebase and i got the data and passed it to Listview, but only the count appear on the listview no data. The listview has blank rows.

    The Firebase Android that gets the data:

      public void OnDataChange(DataSnapshot snapshot)
            {
    
                Gson gson = new GsonBuilder().Create();
    
                var children = snapshot.Children?.ToEnumerable<DataSnapshot>();
    
                HashMap map = snapshot.Value.JavaCast<HashMap>();
    
                string vehiclesstring = gson.ToJson(map);
    
                MessagingCenter.Send<object, string>(this, "vehicles", vehiclesstring);
            }
    

    The Data Reciever:

       private void ToCollection(string data)
        {
            ObservableCollection<Vehicle> Vehicles = new ObservableCollection<Vehicle>();
            Vehicle vehicle = new Vehicle();
    
            Dictionary<string, object> Data = new Dictionary<string, object>();
    
            Vehicles.Clear();
    
            try
            {
    
                Data = JsonConvert.DeserializeObject <Dictionary<string, object>> (data);
    
                foreach (KeyValuePair<string, object> item in Data.ToList())
                {
                    vehicle = item.Value as Vehicle;
                    Vehicles.Add(vehicle);
                }
    
                vehicleslist.ItemsSource = Vehicles;
    
            }
            catch (Exception ex)
            {
                DisplayAlert("Error", "Error getting vehicles" + ex.Message, "OK");
            }
    
        }
    

    The model:

       public partial class Vehicle
      {
        public Vehicle()
        {
    
        }
    
    
        public Vehicle(string key, string imageUri, string model, string plate, string type, string createdAt)
        {
            Key = key;
            ImageUri = imageUri;
            Model = model;
            Plate = plate;
            Type = type;
            CreatedAt = createdAt;
        }
    
        public string Key { get; set; }
        public string ImageUri { get; set; }
        public string Model { get; set; }
        public string Plate { get; set; }
        public string Type { get; set; }
        public string CreatedAt { get; set; }
    
    }
    

    The Xaml Listview:

      <ListView
                    x:Name="vehicleslist"
                    ItemSelected="Vehicleslist_ItemSelected"
                    RowHeight="80">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell Height="50">
                            <StackLayout Margin="10" Padding="0">
                                <Label Text="{Binding Type}" />
                                <Label Text="{Binding Model}" />
                                <Label Text="{Binding Plate}" />
                            </StackLayout>
    
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>`
    
    Monday, August 5, 2019 9:42 AM

All replies

  • User379860 posted

    First of all, make sure you have set a realtime database and have data in this database like the following screenshot.

    Then, check the rule of of the database. And add the android Internet persmission

    I set the demo of the firebase database, I can get the result.

    Here is running screenshot.

    I got the all data in the firebase database like the following code.

       public class FirebaseHelper
    {
        FirebaseClient firebase = new FirebaseClient("https://xamarinfirebase-dfa10.firebaseio.com/");
    
        public async Task<List<Vehicle>> GetAllVehicles()
        {
    
            return (await firebase
              .Child("Vehicle")
              .OnceAsync<Vehicle>()).Select(item => new Vehicle
              {
                  Key = item.Object.Key,
                  ImageUri = item.Object.ImageUri
              }).ToList();
        }
    
        public async Task AddVehicle(string myKey, string myImageUri)
        {
    
            await firebase
              .Child("Vehicle")
              .PostAsync(new Vehicle() { Key = myKey, ImageUri = myImageUri });
        }
    }
    

    I post my demo. And there is a helpful article about it. https://www.c-sharpcorner.com/article/xamarin-forms-working-with-firebase-realtime-database-crud-operations/

    Monday, August 5, 2019 12:55 PM
  • User385011 posted

    @LeonLu said: First of all, make sure you have set a realtime database and have data in this database like the following screenshot.

    Then, check the rule of of the database. And add the android Internet persmission

    I set the demo of the firebase database, I can get the result.

    Here is running screenshot.

    I got the all data in the firebase database like the following code.

       public class FirebaseHelper
    {
        FirebaseClient firebase = new FirebaseClient("https://xamarinfirebase-dfa10.firebaseio.com/");
    
        public async Task<List<Vehicle>> GetAllVehicles()
        {
    
            return (await firebase
              .Child("Vehicle")
              .OnceAsync<Vehicle>()).Select(item => new Vehicle
              {
                  Key = item.Object.Key,
                  ImageUri = item.Object.ImageUri
              }).ToList();
        }
    
        public async Task AddVehicle(string myKey, string myImageUri)
        {
    
            await firebase
              .Child("Vehicle")
              .PostAsync(new Vehicle() { Key = myKey, ImageUri = myImageUri });
        }
    }
    

    I post my demo. And there is a helpful article about it. https://www.c-sharpcorner.com/article/xamarin-forms-working-with-firebase-realtime-database-crud-operations/

    I'm using different Package, is it possible to that in https://www.nuget.org/packages/Xamarin.Firebase.Database/

    Monday, August 5, 2019 1:44 PM
  • User379860 posted

    @mohaaosman This package is used for the xamarin android, not xamarin forms.

    Monday, August 5, 2019 2:16 PM
  • User385011 posted

    @LeonLu I'm not using it in Xamarin form, i used a Dependency Service, and i'm Getting the data, just didn't know how to Convert it properly to list, normally i can get a single record, But when it comes to list i have a problem because of how the data is structured

    Monday, August 5, 2019 3:13 PM
  • User379860 posted

    @mohaaosman why you want to use Dependency Service to achieve it? you get the data, then send the data by messageCenter to the PCL? If you have massive data, you still use messageCenter, it will cost lots of resource.

    Tuesday, August 6, 2019 7:09 AM
  • User385011 posted

    @LeonLu I'm using DS because it was the only solution i found for Platform specific Packages, if you have another solution or game that will help to send data from native to Shared do tell me please, and i wanted to know to get list of data, already have a solution for the single record

    Thursday, August 8, 2019 5:29 AM
  • User379860 posted

    @mohaaosman Which kind of method did your send the data in the messagecenter? string, List<object> or ObservableCollection<object> ?You can refer to this link about sending message by ObservableCollection<object> https://montemagno.com/how-to-use-xamarinforms-messaging-center/

    Monday, August 12, 2019 12:43 PM