“RecyclerView de Firebase Un seul enfant” Réponses codées

RecyclerView de Firebase Un seul enfant

public static class MenuHomeAdapter extends RecyclerView.Adapter<MenuViewHolder> {
    ArrayList<Menu> items = new ArrayList<>();

    public MenuHomeAdapter(Firebase ref) {
        ref.addValueEventListener(new ValueEventListener() {
            public void onDataChange(DataSnapshot snapshot) {
                items.clear();
                for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                    Menu menu = postSnapshot.getValue(Menu.class);
                    items.add(menu);
                }
                notifyDataSetChanged();
            }

            public void onCancelled(FirebaseError firebaseError) {
                System.out.println("The read failed: " + firebaseError.getMessage());
            }
        });
    }

    public MenuViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
        return new MenuViewHolder(view);
    }

    public void onBindViewHolder(MenuViewHolder holder, int position) {
        Menu item = items.get(position);
        holder.getTitleView().setText(item.getTitle());
    }

    public int getItemCount() {
        return items.size();
    }
};
CB20 138 Najmi

RecyclerView de Firebase Un seul enfant

package com.example.recyclerview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
  
public class MainActivity extends AppCompatActivity {
  
    private RecyclerView recyclerView;
    personAdapter
        adapter; // Create Object of the Adapter class
    DatabaseReference mbase; // Create object of the
                             // Firebase Realtime Database
  
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // Create a instance of the database and get
        // its reference
        mbase
            = FirebaseDatabase.getInstance().getReference();
  
        recyclerView = findViewById(R.id.recycler1);
  
        // To display the Recycler view linearly
        recyclerView.setLayoutManager(
            new LinearLayoutManager(this));
  
        // It is a class provide by the FirebaseUI to make a
        // query in the database to fetch appropriate data
        FirebaseRecyclerOptions<person> options
            = new FirebaseRecyclerOptions.Builder<person>()
                  .setQuery(mbase, person.class)
                  .build();
        // Connecting object of required Adapter class to
        // the Adapter class itself
        adapter = new personAdapter(options);
        // Connecting Adapter class with the Recycler view*/
        recyclerView.setAdapter(adapter);
    }
  
    // Function to tell the app to start getting
    // data from database on starting of the activity
    @Override protected void onStart()
    {
        super.onStart();
        adapter.startListening();
    }
  
    // Function to tell the app to stop getting
    // data from database on stopping of the activity
    @Override protected void onStop()
    {
        super.onStop();
        adapter.stopListening();
    }
}
CB20 138 Najmi

RecyclerView de Firebase Un seul enfant

package com.example.recyclerview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
  
public class MainActivity extends AppCompatActivity {
  
    private RecyclerView recyclerView;
    personAdapter
        adapter; // Create Object of the Adapter class
    DatabaseReference mbase; // Create object of the
                             // Firebase Realtime Database
  
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // Create a instance of the database and get
        // its reference
        mbase
            = FirebaseDatabase.getInstance().getReference();
  
        recyclerView = findViewById(R.id.recycler1);
  
        // To display the Recycler view linearly
        recyclerView.setLayoutManager(
            new LinearLayoutManager(this));
  
        // It is a class provide by the FirebaseUI to make a
        // query in the database to fetch appropriate data
        FirebaseRecyclerOptions<person> options
            = new FirebaseRecyclerOptions.Builder<person>()
                  .setQuery(mbase, person.class)
                  .build();
        // Connecting object of required Adapter class to
        // the Adapter class itself
        adapter = new personAdapter(options);
        // Connecting Adapter class with the Recycler view*/
        recyclerView.setAdapter(adapter);
    }
  
    // Function to tell the app to start getting
    // data from database on starting of the activity
    @Override protected void onStart()
    {
        super.onStart();
        adapter.startListening();
    }
  
    // Function to tell the app to stop getting
    // data from database on stopping of the activity
    @Override protected void onStop()
    {
        super.onStop();
        adapter.stopListening();
    }
}
CB20 138 Najmi

RecyclerView de Firebase Un seul enfant

package com.example.recyclerview;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
  
// FirebaseRecyclerAdapter is a class provided by
// FirebaseUI. it provides functions to bind, adapt and show
// database contents in a Recycler View
public class personAdapter extends FirebaseRecyclerAdapter<
    person, personAdapter.personsViewholder> {
  
    public personAdapter(
        @NonNull FirebaseRecyclerOptions<person> options)
    {
        super(options);
    }
  
    // Function to bind the view in Card view(here
    // "person.xml") iwth data in
    // model class(here "person.class")
    @Override
    protected void
    onBindViewHolder(@NonNull personsViewholder holder,
                     int position, @NonNull person model)
    {
  
        // Add firstname from model class (here
        // "person.class")to appropriate view in Card
        // view (here "person.xml")
        holder.firstname.setText(model.getFirstname());
  
        // Add lastname from model class (here
        // "person.class")to appropriate view in Card
        // view (here "person.xml")
        holder.lastname.setText(model.getLastname());
  
        // Add age from model class (here
        // "person.class")to appropriate view in Card
        // view (here "person.xml")
        holder.age.setText(model.getAge());
    }
  
    // Function to tell the class about the Card view (here
    // "person.xml")in
    // which the data will be shown
    @NonNull
    @Override
    public personsViewholder
    onCreateViewHolder(@NonNull ViewGroup parent,
                       int viewType)
    {
        View view
            = LayoutInflater.from(parent.getContext())
                  .inflate(R.layout.person, parent, false);
        return new personAdapter.personsViewholder(view);
    }
  
    // Sub Class to create references of the views in Crad
    // view (here "person.xml")
    class personsViewholder
        extends RecyclerView.ViewHolder {
        TextView firstname, lastname, age;
        public personsViewholder(@NonNull View itemView)
        {
            super(itemView);
  
            firstname
                = itemView.findViewById(R.id.firstname);
            lastname = itemView.findViewById(R.id.lastname);
            age = itemView.findViewById(R.id.age);
        }
    }
}
CB20 138 Najmi

RecyclerView de Firebase Un seul enfant

package com.example.recyclerview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
  
public class MainActivity extends AppCompatActivity {
  
    private RecyclerView recyclerView;
    personAdapter
        adapter; // Create Object of the Adapter class
    DatabaseReference mbase; // Create object of the
                             // Firebase Realtime Database
  
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // Create a instance of the database and get
        // its reference
        mbase
            = FirebaseDatabase.getInstance().getReference();
  
        recyclerView = findViewById(R.id.recycler1);
  
        // To display the Recycler view linearly
        recyclerView.setLayoutManager(
            new LinearLayoutManager(this));
  
        // It is a class provide by the FirebaseUI to make a
        // query in the database to fetch appropriate data
        FirebaseRecyclerOptions<person> options
            = new FirebaseRecyclerOptions.Builder<person>()
                  .setQuery(mbase, person.class)
                  .build();
        // Connecting object of required Adapter class to
        // the Adapter class itself
        adapter = new personAdapter(options);
        // Connecting Adapter class with the Recycler view*/
        recyclerView.setAdapter(adapter);
    }
  
    // Function to tell the app to start getting
    // data from database on starting of the activity
    @Override protected void onStart()
    {
        super.onStart();
        adapter.startListening();
    }
  
    // Function to tell the app to stop getting
    // data from database on stopping of the activity
    @Override protected void onStop()
    {
        super.onStop();
        adapter.stopListening();
    }
}
CB20 138 Najmi

RecyclerView de Firebase Un seul enfant

package com.example.recyclerview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
  
public class MainActivity extends AppCompatActivity {
  
    private RecyclerView recyclerView;
    personAdapter
        adapter; // Create Object of the Adapter class
    DatabaseReference mbase; // Create object of the
                             // Firebase Realtime Database
  
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // Create a instance of the database and get
        // its reference
        mbase
            = FirebaseDatabase.getInstance().getReference();
  
        recyclerView = findViewById(R.id.recycler1);
  
        // To display the Recycler view linearly
        recyclerView.setLayoutManager(
            new LinearLayoutManager(this));
  
        // It is a class provide by the FirebaseUI to make a
        // query in the database to fetch appropriate data
        FirebaseRecyclerOptions<person> options
            = new FirebaseRecyclerOptions.Builder<person>()
                  .setQuery(mbase, person.class)
                  .build();
        // Connecting object of required Adapter class to
        // the Adapter class itself
        adapter = new personAdapter(options);
        // Connecting Adapter class with the Recycler view*/
        recyclerView.setAdapter(adapter);
    }
  
    // Function to tell the app to start getting
    // data from database on starting of the activity
    @Override protected void onStart()
    {
        super.onStart();
        adapter.startListening();
    }
  
    // Function to tell the app to stop getting
    // data from database on stopping of the activity
    @Override protected void onStop()
    {
        super.onStop();
        adapter.stopListening();
    }
}
CB20 138 Najmi

Réponses similaires à “RecyclerView de Firebase Un seul enfant”

Questions similaires à “RecyclerView de Firebase Un seul enfant”

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code