从我的应用程序中的Firebase检索数据,并手动上传数据

时间:2019-01-10 18:34:50

标签: android firebase-realtime-database

我想将Firebase实时数据库上的可用数据检索到Recycler View,在该视图中我已手动上传了数据。 我的数据库包含其中存在书名的“标题”列和其中存在书pdf的url且“书” pdf可在Firebase Storage上获得的“书”列。 应用程序类似于,用户可以使用搜索栏或手动搜索来访问数据库中可用的书。我没有为搜索栏编写代码。

Adapter Code:-
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
ArrayList<Model> models = new ArrayList<>();
Context c;

public MyAdapter(Context c , ArrayList<Model> models){
    this.c = c;
    this.models = models;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
    View view = LayoutInflater.
from(parent.getContext()).inflate(R.layout.book_list,parent,false);
    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(MyViewHolder holder , int position){
    holder.nameText.setText(models.get(position).getTitle());
    holder.imageView.setImageResource(R.drawable.fileimage);
}

@Override
public int getItemCount(){
    return models.size();
}
}






Two Layouts:-
1.main_activity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">

<SearchView
    android:layout_width="match_parent"
    android:layout_height="45dp">
</SearchView>


<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_marginTop="45dp"
    android:scrollbars="vertical"
    />

</LinearLayout>






2.book_list

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">


    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        />

        <TextView
            android:id="@+id/text_view"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:text="Name:"
            android:textSize="20dp"
            android:textColor="@android:color/black"/>

</android.support.v7.widget.CardView>







Main_Activity:-
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

private FirebaseDatabase firebaseDatabase;
private DatabaseReference databaseReference;
private ArrayList<Model> model;
private RecyclerView recyclerView;
private LinearLayoutManager layoutManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    firebaseDatabase = FirebaseDatabase.getInstance();
    databaseReference = firebaseDatabase.getReference();
    recyclerView = (RecyclerView)findViewById(R.id.recycler);
    layoutManager = new LinearLayoutManager(this);

    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(layoutManager);


   databaseReference.child("Data").addValueEventListener(new                     
       ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            model=new ArrayList<>();

            for(DataSnapshot ds : dataSnapshot.getChildren()){

                Log.v("TAG","For Loop Entered");

                Model m = ds.getValue(Model.class);
                model.add(m);
            }

            Log.v("TAG","For Loop Closed");

            MyAdapter myAdapter = new MyAdapter(MainActivity.this,model);
            recyclerView.setAdapter(myAdapter);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

            Log.v("TAG","Read Error",databaseError.toException());

        }
    });
    }
}


MOdel Class:-
public class Model {

private String Title;
private String Book;

public Model(){

}

public Model(String Title , String Book){
    this.Title = Title;
    this.Book = Book;
}

public String getTitle() {
    return Title;
}

public void setTitle(String title) {
    this.Title = title;
}

public String getBook() {
    return Book;
}

public void setBookName(String Book) {
    this.Book = Book;
}


}


Database Structure image:-


[1]: https://i.stack.imgur.com/b6tQH.png




https://book-shelf-364b7.firebaseio.com/
 book-shelf-364b7
   Data
     Book:"https://firebasestorage.googleapis.com/v0/b/boo..."
     Title:"CPP"

0 个答案:

没有答案