Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
6002CEM_8918189/app/src/main/java/com/example/booksharing21/LibraryFragment.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
242 lines (141 sloc)
5.82 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.booksharing21; | |
import android.content.Context; | |
import android.os.Bundle; | |
import androidx.annotation.NonNull; | |
import androidx.fragment.app.Fragment; | |
import androidx.recyclerview.widget.LinearLayoutManager; | |
import androidx.recyclerview.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import com.google.firebase.auth.FirebaseAuth; | |
import com.google.firebase.auth.FirebaseUser; | |
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 com.google.firebase.firestore.FirebaseFirestore; | |
import com.squareup.picasso.Picasso; | |
import java.util.ArrayList; | |
/** | |
* A simple {@link Fragment} subclass. | |
* Use the {@link LibraryFragment#newInstance} factory method to | |
* create an instance of this fragment. | |
*/ | |
public class LibraryFragment extends Fragment { | |
// TODO: Rename parameter arguments, choose names that match | |
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER | |
private static final String ARG_PARAM1 = "param1"; | |
private static final String ARG_PARAM2 = "param2"; | |
// TODO: Rename and change types of parameters | |
private String mParam1; | |
private String mParam2; | |
public LibraryFragment() { | |
// Required empty public constructor | |
} | |
/** | |
* Use this factory method to create a new instance of | |
* this fragment using the provided parameters. | |
* | |
* @param param1 Parameter 1. | |
* @param param2 Parameter 2. | |
* @return A new instance of fragment LibraryFragment. | |
*/ | |
// TODO: Rename and change types and number of parameters | |
public static LibraryFragment newInstance(String param1, String param2) { | |
LibraryFragment fragment = new LibraryFragment(); | |
Bundle args = new Bundle(); | |
args.putString(ARG_PARAM1, param1); | |
args.putString(ARG_PARAM2, param2); | |
fragment.setArguments(args); | |
return fragment; | |
} | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
if (getArguments() != null) { | |
mParam1 = getArguments().getString(ARG_PARAM1); | |
mParam2 = getArguments().getString(ARG_PARAM2); | |
} | |
onStart(); | |
} | |
public ArrayList<info> infoList ; | |
public HelperAdapter helperAdapter; | |
public RecyclerView librec; | |
public FirebaseDatabase mdb; | |
public DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference(); | |
public String userid = FirebaseAuth.getInstance().getCurrentUser().getUid(); | |
public DatabaseReference db = FirebaseDatabase.getInstance().getReference("user-posts").child(userid); | |
public Context mContext; | |
public String key = mDatabase.child("user-posts/").push().getKey(); | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
// Inflate the layout for this fragment | |
View view = inflater.inflate(R.layout.fragment_library, container, false); | |
// Layout | |
librec = view.findViewById(R.id.lib_re_view); | |
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); | |
librec.setLayoutManager(layoutManager); | |
librec.setHasFixedSize(true); | |
mDatabase = FirebaseDatabase.getInstance().getReference("user-posts").child(userid); | |
// Array List | |
infoList = new ArrayList<>(); | |
// Clear List | |
ClearAll(); | |
// Getting Data from FDB | |
GetDataFromFirebase(); | |
return view; | |
} | |
private void ClearAll() { | |
if (infoList!=null){ | |
infoList.clear(); | |
if (helperAdapter != null){ | |
helperAdapter.notifyDataSetChanged(); | |
} | |
} | |
infoList = new ArrayList<>(); | |
} | |
ValueEventListener listener; | |
// selecting books from current user | |
private void GetDataFromFirebase() { | |
listener = new ValueEventListener() { | |
@Override | |
public void onDataChange(@NonNull DataSnapshot snapshot) { | |
DatabaseReference db = FirebaseDatabase.getInstance().getReference("user-posts").child(userid); | |
ClearAll(); | |
for (DataSnapshot dataSnapshot : snapshot.getChildren()) { | |
info info = new info(); | |
info.setPostid(dataSnapshot.child("postid").getValue().toString()); | |
info.setUrl(dataSnapshot.child("url").getValue().toString()); | |
info.setBookname(dataSnapshot.child("book name").getValue().toString()); | |
info.setIsbn(dataSnapshot.child("isbn").getValue().toString()); | |
info.setAuthor(dataSnapshot.child("author").getValue().toString()); | |
infoList.add(info); | |
helperAdapter = new HelperAdapter(getContext(), infoList); | |
librec.setAdapter(helperAdapter); | |
helperAdapter.notifyDataSetChanged(); | |
} ; | |
} | |
@Override | |
public void onCancelled (@NonNull DatabaseError error){ | |
Toast.makeText(getActivity(), "No books in your library", Toast.LENGTH_LONG).show(); | |
} | |
}; | |
db.addListenerForSingleValueEvent(listener); | |
}; | |
@Override | |
public void onStart() | |
{ | |
super.onStart(); | |
GetDataFromFirebase(); //onStart Called | |
} | |
public void onStop() { | |
super.onStop(); | |
mDatabase.removeEventListener(listener); | |
} | |
} | |