Home » Android » android – How to retrieve data from fire-base into a new activity On-click RecyclerView-Exceptionshub

android – How to retrieve data from fire-base into a new activity On-click RecyclerView-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

It’s an app that retrieves book data from firebase, I want to know how to retrieve specific data into a new activity. Like, retrieve specific book data on click RecyclerView. I am able to get the onclick position on the RecyclerView but I don’t know how to use that with firebase

Here is my code:

public class SearchPage extends AppCompatActivity {
    EditText searchbar;

    RecyclerView recyclerView;
    DatabaseReference reference;

    ArrayList <String> BookNameList;
    ArrayList <String> AuthorNameList;
    ArrayList <String> PicList;
    ArrayList <String> PublisherList;
    ArrayList <String> Shelfnols;
    ArrayList <String> Desc;
    SearchAdapter searchAdapter;

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

        searchbar = (EditText) findViewById(R.id.searchbar);
        reference = FirebaseDatabase.getInstance().getReference();
        reference.keepSynced(true);
        recyclerView = (RecyclerView) findViewById(R.id.rv);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        BookNameList = new ArrayList <> ();
        PublisherList = new ArrayList <> ();
        AuthorNameList = new ArrayList <> ();
        Shelfnols = new ArrayList <> ();
        PicList = new ArrayList <> ();
        Desc = new ArrayList <> ();
        final ArrayList <String> uidlist = new ArrayList <String> ();




        searchbar.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!s.toString().isEmpty()) {
                    setAdapter(s.toString());

                } else {
                    BookNameList.clear();
                    AuthorNameList.clear();
                    PicList.clear();
                    PublisherList.clear();
                    Shelfnols.clear();
                    Desc.clear();
                    uidlist.clear();

                }
            }

            private void setAdapter(final String searchedString) {
                reference.child("books").addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                        BookNameList.clear();
                        AuthorNameList.clear();
                        PicList.clear();
                        PublisherList.clear();
                        Shelfnols.clear();
                        Desc.clear();
                        uidlist.clear();


                        int counter = 0;
                        for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
                            String uid = snapshot.getKey();

                            String desc = snapshot.child("Desc").getValue(String.class);
                            String bookname = snapshot.child("bookname").getValue(String.class);
                            String author = snapshot.child("author").getValue(String.class);
                            String image = snapshot.child("image").getValue(String.class);
                            String publisher = snapshot.child("Publisher").getValue(String.class);
                            String shelfno = snapshot.child("Shelf_no").getValue(String.class);
                            try {


                                if (bookname.toLowerCase().contains(searchedString.toLowerCase())) {
                                    BookNameList.add(bookname);
                                    AuthorNameList.add(author);
                                    PublisherList.add(publisher);
                                    Shelfnols.add(shelfno);
                                    PicList.add(image);
                                    Desc.add(desc);

                                    counter++;
                                } else if (author.toLowerCase().contains(searchedString.toLowerCase())) {
                                    BookNameList.add(bookname);
                                    AuthorNameList.add(author);
                                    PublisherList.add(publisher);
                                    Shelfnols.add(shelfno);
                                    PicList.add(image);
                                    Desc.add(desc);
                                    counter++;
                                }
                            } catch (Exception e) {
                                Toast.makeText(getApplicationContext(), "not found", Toast.LENGTH_LONG).show();
                            }


                            if (counter == 15) {
                                break;
                            }
                            SearchAdapter searchAdapter = new SearchAdapter(SearchPage.this, BookNameList, AuthorNameList, PicList, PublisherList, Shelfnols);
                            recyclerView.setAdapter(searchAdapter);
                        }
                    }

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

                    }
                });
            }
        });

    }

This is my adapter class

public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.SearchViewHolder> {
public static final String id="key";
    Context context;
    ArrayList<String> BookNameList;
    ArrayList<String> AuthorNameList;
ArrayList<String> PicList;
ArrayList<String> PublisherList;
ArrayList<String> Shelfnols;
LinearLayout booklayout;




class SearchViewHolder extends RecyclerView.ViewHolder{
    ImageView bookimage;
    TextView bookname, authorname,publisher,shelfno;

    public SearchViewHolder(@NonNull View itemView) {
        super(itemView);
        bookimage =  itemView.findViewById(R.id.Bookimg);
        bookname =  itemView.findViewById(R.id.BookName);
        authorname =  itemView.findViewById(R.id.AuthorName);
        publisher =  itemView.findViewById(R.id.Publications);
        shelfno =  itemView.findViewById(R.id.Shelfno);
        booklayout=itemView.findViewById(R.id.LinLayout);
    }
}

public SearchAdapter(Context context, ArrayList<String> bookNameList, ArrayList<String> authorNameList, ArrayList<String> picList,ArrayList<String> publisherList,ArrayList<String> shelfnols) {
    this.context = context;
    BookNameList = bookNameList;
    AuthorNameList = authorNameList;
    PicList = picList;
    PublisherList=publisherList;
    Shelfnols=shelfnols;
}


@NonNull
@Override
public SearchAdapter.SearchViewHolder  onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.activity_search_layout,parent,false);
     return new SearchAdapter.SearchViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull SearchViewHolder holder, final int position) {
    holder.bookname.setText(BookNameList.get(position));
    holder.authorname.setText(AuthorNameList.get(position));
    holder.publisher.setText(PublisherList.get(position));
    holder.shelfno.setText(Shelfnols.get(position));
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                String t=String.valueOf(position);
            Toast.makeText(v.getContext(), "Item is clicked" + t, Toast.LENGTH_SHORT).show();

        }
    });

   booklayout.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {

       }

   });

this the new activity where i want the book details to be shown

public class Bookdetailslayout extends SearchPage {

DatabaseReference reference;
TextView bookname,author,publisher,desc,location;
ImageView image;




@Override
protected void onCreate(Bundle savedInstanceState) {



    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bookdetailslayout);

    bookname = (TextView)findViewById(R.id.bkname);
    author = (TextView) findViewById(R.id.aname);
    publisher = (TextView) findViewById(R.id.pname);
    desc = (TextView) findViewById(R.id.bkdescription);
    location = (TextView) findViewById(R.id.bklocation);
    String s = getIntent().getStringExtra("key");


    DatabaseReference databaseReference=FirebaseDatabase.getInstance().getReference().child("books");
    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            Bookdeets bookdeets=dataSnapshot.getValue(Bookdeets.class);


            bookname.setText(bookdeets.getBookname());



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

        }
    });



}



    Glide.with(context).asBitmap().load(PicList.get(position)).placeholder(R.mipmap.ic_launcher_round).into(holder.bookimage);

}

@Override
public int getItemCount() {
    return BookNameList.size();
}
How to&Answers:
      holder.itemView.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           Intent i = new Intent(context,BookdetailsActivity.class);
           i.putExtra("bookname",bookname);
           context.startActivity(i);
       }
   });

above code start new activity for showing details on new activity at the same time pass unique key for retrieving data from firebase.

inside BookDetailsActivity
get key value by using

String name = getIntent().getStringExtra(“bookname”);

then do your stuff for getting data from firebase.