我在Android Studio和Firebase上还很陌生,我想做的是在填写表单后单击按钮的同时在我的应用程序和firebase上填充一个片段。我不是在谈论弱化。我的应用崩溃了。
这是表格所在的片段
public class Entreprisesfrag extends android.support.v4.app.Fragment {
RecyclerView recyclerView;
DatabaseReference dbref;
FirebaseDatabase firebaseDatabase;
public Entreprisesfrag() {
// Required empty public constructor
}
@Override
public void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_entreprises, container, false);
recyclerView= view.findViewById(R.id.recv);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager (new LinearLayoutManager(getContext()));
firebaseDatabase= FirebaseDatabase.getInstance();
dbref= firebaseDatabase.getReference("Entreprises");
return view;
}
@Override
public void onStart() {
super.onStart();
FirebaseRecyclerAdapter<entreprises_class,ViewHolder>firebaseRecyclerAdapter= new FirebaseRecyclerAdapter<entreprises_class, ViewHolder>
(entreprises_class.class,R.layout.entreprises_cardview, ViewHolder.class,dbref) {
@Override
protected void populateViewHolder(ViewHolder viewHolder, entreprises_class model, int position) {
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.recherche_activity_menu_item, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
//Do search code here
return true;
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()==R.id.recherche)
{
}
else if (item.getItemId()==R.id.filtrer)
{
Toast.makeText(getContext(), "Erreur de connexion ! Vérifiez l'adresse email utilisée et le mot de passe", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
}
字母和二传手课程
public class entreprises_class {
String nom, secteur, ville, image;
public entreprises_class(){}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getSecteur() {
return secteur;
}
public void setSecteur(String secteur) {
this.secteur = secteur;
}
public String getVille() {
return ville;
}
public void setVille(String ville) {
this.ville = ville;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
Firebase类
public class Firebase_class {
DatabaseReference dbref;
Boolean aBoolean=null;
ArrayList<entreprises_class> entreprises= new ArrayList<>();
public Firebase_class(DatabaseReference dbref) {
this.dbref=dbref;
}
public Boolean save(entreprises_class ent_class)
{
if(ent_class==null){
aBoolean=false;
}
else
{
try
{
dbref.child("Entreprises").push().setValue(ent_class);
aBoolean=true;
}
catch (DatabaseException databaseException)
{
databaseException.printStackTrace();
aBoolean=false;
}
}
return aBoolean;
}
public void fetchdata(DataSnapshot dataSnapshot)
{
entreprises.clear();
for (DataSnapshot ds: dataSnapshot.getChildren())
{
entreprises_class ent_class= ds.getValue(entreprises_class.class);
entreprises.add(ent_class);
}
}
public ArrayList<entreprises_class> retrieve()
{
dbref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot,
@Nullable String s) {
fetchdata(dataSnapshot);
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot,
@Nullable String s) {
fetchdata(dataSnapshot);
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot,
@Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return entreprises;
}
}
ViewHolder类
public class ViewHolder extends RecyclerView.ViewHolder {
TextView textView, textView1, textView2;
ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
textView= itemView.findViewById(R.id.entreprise_nom);
textView1= itemView.findViewById(R.id.entreprise_secteur);
textView2= itemView.findViewById(R.id.entreprise_sous_secteur);
imageView=itemView.findViewById(R.id.entreprise_image);
}
}
适配器类
public class Adapter extends RecyclerView.Adapter<ViewHolder>{
Context cntx;
ArrayList<entreprises_class> entreprises;
public Adapter(Context cntx, ArrayList<entreprises_class> entreprises) {
this.cntx = cntx;
this.entreprises = entreprises;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(cntx).inflate
(R.layout.entreprises_cardview, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.textView.setText(entreprises.get(position).getNom());
holder.textView1.setText(entreprises.get(position).getSecteur());
holder.textView2.setText(entreprises.get(position).getVille());
Picasso.get().load(entreprises.get(position)
.getImage()).into(holder.imageView);
}
@Override
public int getItemCount() {
return entreprises.size();
}
}