skip to Main Content

I cant find a way to read data from firebase databse in c# android application.
I want to read value from child data/test. Question appers to be very simple, couldnt find a aimple way which i would understand online

DATABASE picture:
data
Problem with reading screens
before
after

Working connection:

        var options = new Firebase.FirebaseOptions.Builder()
            .SetApplicationId("1:191485419818:android:c3bef44f30f167e537be53")
            .SetApiKey("AIzaSyBwLcDZ3wGyzkwP6SobH8KDDsl5Vq227Vg")
            .Build();

        if (fireApp == null)
            fireApp = FirebaseApp.InitializeApp(this, options);

        auth = FirebaseAuth.GetInstance(fireApp);

        string FirebaseURL = "https://plsbrothers-default-rtdb.firebaseio.com/";
        FirebaseClient rootNode = new FirebaseClient(FirebaseURL);

        dataNode = rootNode.Child("data");
        userNode = rootNode.Child("users");

//Trying to access the saved data and display it. Dont know it it is an issue with converting data to string and displaying it or i dont even access it properly (it shows Firebase.Database.Query.childQuery and not the data i want to display)

string testDisplay = dataNode.Child("test").ToString();
      
          TextView currentCharacterName = FindViewById<TextView>(Resource.Id.test);
          currentCharacterName.Text = testDisplay;

XML CODE:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:layout_height="match_parent">


<Button
         android:layout_width="match_parent"
         android:layout_height="100dp"
         android:text="save"
         android:textSize="25dp"
         android:textColor="#000"
         android:id="@+id/Btn"
         android:layout_marginTop="280dp"
/>

<TextView
    android:text="starting value"
    android:textSize="30dp"
    android:textColor="#000"
    android:layout_marginTop="100dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/test" />

2

Answers


  1. Chosen as BEST ANSWER

    Connection:

    FirebaseClient firebaseClient = new FirebaseClient("Your Firebase URL");
    

    Data Class, in these example it will be GalleryModel

    public class GalleryModel
    {
        public string Id { get; set; }
    
        public string Name{ get; set; }
    
    }
    

    Generete new file(optionally) for Tasks. In these case file name is GalleryRepo

     public async Task<List<GalleryModel>>GetAll()
        {
            return (await firebaseClient.Child(nameof(GalleryModel)).OnceAsync<GalleryModel>()).Select(item => new GalleryModel
            {
                
                Name = item.Object.Name,
                Id = item.Key
    
            }).ToList();
        }
    

    Create new xml file.

    <StackLayout>
            <ListView x:Name="galleryListView" ItemsSource="{Binding gallerylist}" HasUnevenRows="True">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
    
                            <StackLayout Orientation="Horizontal" Padding="5">
    
                                <StackLayout HorizontalOptions="StartAndExpand">
    
                                    <Label Text="{Binding Name}" FontSize="Medium"/>
                                    <Label Text="{Binding Id}" FontSize="Medium"/>                 
                                   
                                </StackLayout>
                            </StackLayout>
    
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    

    Than in xml cs file

    GalleryRepo galrepo = new GalleryRepo();
    
    protected override async void OnAppearing()
        {
    
            var gallerylist = await GetAll();
            datesListView.ItemsSource = gallerylist;
    
    
        }
    

  2. //string testDisplay = dataNode.Child("test").ToString();
    DatabaseReference testNode = dataNode.child("test");
    string email = testNode.Object.Email.ToString()
    string name = testNode.Object.Name.ToString()
    
    TextView currentCharacterName = FindViewById<TextView>(Resource.Id.test);
    currentCharacterName.Text = email;
    currentCharacterName.Invalidate();
    

    //Please add the Invalidate() method.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search