How to retrieve data from firebase database in android - android

Retrieving images from Firebase does not work? Hi I am working on upload and retrieve images from Firebase. I have successfully done the uploading part, but while doing for the retrieving one. I have came across a problem where i am unable to fetch image from Firebase. I am using Picasso version: 2.5.2
QuoteList.java
public class QuoteList extends AppCompatActivity {
private StorageReference storageRef;
private RecyclerView mRecyclerView;
private DatabaseReference mReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quote_list);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView = findViewById(R.id.quotes_list);
mRecyclerView.setLayoutManager(layoutManager);
mReference = FirebaseDatabase.getInstance().getReference().child("Quotes");
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Quote, QuoteViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Quote, QuoteViewHolder>(
Quote.class,
R.layout.quote_row,
QuoteViewHolder.class,
mReference
) {
#Override
protected void populateViewHolder(QuoteViewHolder viewHolder , Quote model , int position) {
viewHolder.setQuotes(model.getQuotes());
viewHolder.setQuoteAuthor(model.getQuote_Author());
viewHolder.setImage(getApplicationContext(),model.getImages());
}
};
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
}
public class QuoteViewHolder extends RecyclerView.ViewHolder{
View mView;
public QuoteViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setQuotes(String Quote){
TextView post_quote = (TextView) mView.findViewById(R.id.post_quote);
post_quote.setText(Quote);
}
public void setQuoteAuthor(String QuoteAuthor){
TextView post_quoteAuthor = (TextView) mView.findViewById(R.id.post_author);
post_quoteAuthor.setText(QuoteAuthor);
}
public void setImage(Context ctx, String image){
ImageView post_image = (ImageView) mView.findViewById(R.id.post_image);
Picasso.with(ctx).load(image).into(post_image);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
}
For reference i have used a Youtube Link.

Related

Retrieving data from firebase to recyclerview does not work

As mentioned in the title, my recyclerview does not work. No idea why, because I do not get any errors or anything in the logcat or while building the app. Is there a way I could debug this or could you point out the mistake that I did?
I've implemented another recyclerview with the adapter, but I used a simple adapter. I tried using the firebaseUI in this case, and it seems that the adapter somehow cannot retrieve the data from the firebase firestore.
https://imgur.com/a/JSLXuou The screenshot of my database structure.
My adapter:
public class ManageAdapter extends FirestoreRecyclerAdapter<Manage, ManageAdapter.ManageHolder> {
public ManageAdapter(#NonNull FirestoreRecyclerOptions<Manage> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull ManageHolder holder, int position, #NonNull Manage model) {
holder.textviewName.setText(model.getName());
holder.textviewGrade.setText(model.getGrade());
//holder.textviewAge.setText(String.valueOf(model.getAge()));
}
#NonNull
#Override
public ManageHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.manage_item,
parent,false);
return new ManageHolder(v);
}
class ManageHolder extends RecyclerView.ViewHolder{
TextView textviewName;
TextView textviewAge;
TextView textviewGrade;
public ManageHolder(#NonNull View itemView) {
super(itemView);
textviewAge = itemView.findViewById(R.id.manage_item_title_age);
textviewGrade = itemView.findViewById(R.id.manage_item_grade);
textviewName = itemView.findViewById(R.id.manage_item_name);
}
}
}
Main class:
private FirebaseFirestore firebaseFirestore;
private Toolbar mToolbar;
private Button delete_event;
private RecyclerView participants;
private TextView manage_events_title;
private TextView participans_text;
private ManageAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manage_events);
final String ID = getIntent().getExtras().getString("ID");
firebaseFirestore = FirebaseFirestore.getInstance();
delete_event = (Button) findViewById(R.id.delete_event);
manage_events_title = (TextView) findViewById(R.id.manage_events_title);
participans_text = (TextView) findViewById(R.id.participants_text);
participants = (RecyclerView) findViewById(R.id.recyclerview_manage);
setUpRecyclerView();
}
private void setUpRecyclerView(){
final String ID = getIntent().getExtras().getString("ID");
Query query = firebaseFirestore.collection("Events").document(ID).collection("Users");
FirestoreRecyclerOptions<Manage> options = new FirestoreRecyclerOptions.Builder<Manage>()
.setQuery(query, Manage.class).build();
adapter = new ManageAdapter(options);
participants.setHasFixedSize(true);
participants.setLayoutManager(new LinearLayoutManager(this));
participants.setAdapter(adapter);
}
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}

How to swipe to delete on RecyclerView?

What I have here a History page that displays the requests the user have accepted or posted. I was able to successfully put a button for the user to delete all of the history the user has. Now I want the user to be able to delete a history one at a time by swiping it.
HistoryFragment.java
RecyclerView rView;
Button clear;
DatabaseReference dRef;
LinearLayoutManager llm;
String id;
View myView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.history, container, false);
FirebaseUser fu = FirebaseAuth.getInstance().getCurrentUser();
id = fu.getUid();
dRef = FirebaseDatabase.getInstance().getReference().child("History"+id);
clear = (Button) myView.findViewById(R.id.clear);
rView = (RecyclerView) myView.findViewById(R.id.history_list);
rView.setHasFixedSize(true);
llm = new LinearLayoutManager(getActivity());
llm.setReverseLayout(true);
llm.setStackFromEnd(true);
rView.setLayoutManager(llm);
clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dRef.removeValue();
}
});
return myView;
}
#Override
public void onStart() {
super.onStart();
FirebaseRecyclerAdapter<HistoryData, HistoryFragment.RequestViewHolder> fra = new FirebaseRecyclerAdapter<HistoryData, HistoryFragment.RequestViewHolder>(
HistoryData.class,
R.layout.historylistview,
HistoryFragment.RequestViewHolder.class,
dRef
) {
#Override
protected void populateViewHolder(final HistoryFragment.RequestViewHolder viewHolder, HistoryData model, final int position) {
viewHolder.setTrans(model.getTrans());
viewHolder.setRequestDeadline(model.getRequestDeadline());
viewHolder.setRequestDescription(model.getRequestDescription());
viewHolder.setDateTimeAccepted(model.getDateTimeAccepted());
viewHolder.setRequestLocation(model.getRequestLocation());
viewHolder.setProfile(getApplicationContext() ,model.getProfile());
}
};
rView.setAdapter(fra);
}
public static class RequestViewHolder extends RecyclerView.ViewHolder{
View myView;
public RequestViewHolder(View itemView) {
super(itemView);
myView = itemView;
}
public void setTrans(String profile){
TextView trans = (TextView) myView.findViewById(R.id.transaction);
trans.setText(profile);
}
public void setRequestDeadline(String dead){
TextView rdead = (TextView) myView.findViewById(R.id.rDead);
rdead.setText(dead);
}
public void setRequestDescription(String desc){
TextView rdesc = (TextView) myView.findViewById(R.id.rDesc);
rdesc.setText(desc);
}
public void setDateTimeAccepted(String date){
TextView dta = (TextView) myView.findViewById(R.id.rAccept);
dta.setText(date);
}
public void setRequestLocation(String loc){
TextView rloc = (TextView) myView.findViewById(R.id.rLoc);
rloc.setText(loc);
}
public void setProfile(Context c, String profile){
ImageView rprofile = (ImageView) myView.findViewById(R.id.HPicture);
Picasso.with(c).load(profile).into(rprofile);
}
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
I haven't really tried anything because the other questions I've seen here so far doesn't seem to be similar to my codes.
You will have to use an existing library / widget unless you want to create the functionality from scratch.
For instance:
https://android-arsenal.com/details/1/4897
You have full instructions there but basically:
mRecyclerView = new CdsRecyclerView(this);
mRecyclerView = (CdsRecyclerView) findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
// Adapter:
mRecyclerViewAdapter = new RecyclerViewAdapter(this, getData());
mRecyclerView.setAdapter(mRecyclerViewAdapter);
// Enable swipe:
mRecyclerView.enableItemSwipe();
// Listener:
mRecyclerView.setItemSwipeCompleteListener(new CdsItemTouchCallback.ItemSwipeCompleteListener() {
#Override
public void onItemSwipeComplete(int position) {
Toast.makeText(MainActivity.this, "Item was swiped:" + position,
Toast.LENGTH_SHORT).show();
}
});

Android Using View Holder

I try to show in my app using firebase but not return image or the other data
please help me I use it after no any error but now I do not use this method
public class PaylasAnaSayfa extends AppCompatActivity {
private RecyclerView mBlogList;
public DatabaseReference mDatabase;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paylas_ana_sayfa);
mDatabase= FirebaseDatabase.getInstance().getReference().child("Resimler");
mBlogList= (RecyclerView) findViewById(R.id.resim_list);
mBlogList.setHasFixedSize(true);
mBlogList.setLayoutManager(new LinearLayoutManager(this));
if (mDatabase==null){
Toast.makeText(this, "veri tabanı hatası", Toast.LENGTH_SHORT).show();
}else {
FirebaseRecyclerAdapter<PaylasModel, PaylasHolder> adapter = new FirebaseRecyclerAdapter<PaylasModel, PaylasHolder>(
PaylasModel.class,
R.layout.resim_row,
PaylasHolder.class,
mDatabase
) {
#Override
protected void populateViewHolder(PaylasHolder viewHolder, PaylasModel model, int position) {
viewHolder.setImage(getApplicationContext(), model.getImgImage());
viewHolder.setBaslik(model.getImgBaslik());
viewHolder.setAciklama(model.getImgAciklama());
viewHolder.setKullanici(model.getImgKullaniciAdi());
viewHolder.setDers(model.getImgDers());
}
};
mBlogList.setAdapter(adapter);
}
}
private static class PaylasHolder extends RecyclerView.ViewHolder {
View mView;
public PaylasHolder(View itemView) {
super(itemView);
mView=itemView;
}
public void setBaslik(String baslik){
TextView tBaslik= (TextView) mView.findViewById(R.id.row_img_baslik);
tBaslik.setText(baslik);
}
public void setAciklama(String aciklama){
TextView tAciklama= (TextView) mView.findViewById(R.id.row_img_aciklama);
tAciklama.setText(aciklama);
}
public void setKullanici(String kullanici){
TextView tKullanici= (TextView) mView.findViewById(R.id.row_img_kullanici);
tKullanici.setText(kullanici);
}
public void setDers(String ders){
TextView tDers= (TextView) mView.findViewById(R.id.row_img_ders);
tDers.setText(ders);
}
public void setImage(final Context context, final String image){
final ImageView tImage= (ImageView) mView.findViewById(R.id.row_img_image);
Picasso.with(context).load(image).networkPolicy(NetworkPolicy.OFFLINE).into(tImage, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
Picasso.with(context).load(image).into(tImage);
}
});
}
}
}
using view holder do not return data
The word in square brackets is the alt text, which gets displayed if the browser can't show the image. Be sure to include meaningful alt text for screen-reading software.
First you don't need to initialize views everytime in the methods of your ViewHolder.RecyclerView reuses the view from the view pool.
private static class PaylasHolder extends RecyclerView.ViewHolder {
View mView;
TextView tBaslik;
public PaylasHolder(View itemView) {
super(itemView);
mView=itemView;
tBaslik = mView.findViewById(R.id.row_img_baslik);
//intialize all you view here once and for all.
}
public void setBaslik(String baslik){
tBaslik.setText(baslik);
}
public void setAciklama(String aciklama){
tAciklama.setText(aciklama);
}
public void setKullanici(String kullanici){
tKullanici.setText(kullanici);
}
public void setDers(String ders){
tDers.setText(ders);
}
public void setImage(final Context context, final String image){
Picasso.with(context).load(image).networkPolicy(NetworkPolicy.OFFLINE).into(tImage, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
Picasso.with(context).load(image).into(tImage);
}
});
}
}
If you are very much sure about the size of the recyclerview then only use setHasFixedSize(true) otherwise it will not request layout upon the addition of data to the adapter.

How can I get selecting value in FirebaseRecyclerAdapter?

I want to obtain determined value from the Realtime DB using FirebaseRecyclerAdapter. This is my class:
public class ProductAvtivity extends AppCompatActivity {
private final String TAG = "ProductActivity";
private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
private FirebaseDatabase database;
private DatabaseReference mProductReference;
private Query mMachineReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_avtivity);
database = FirebaseDatabase.getInstance();
mProductReference = database.getReference().child("products");
String idmachine = getIntent().getStringExtra("idmachine");
mMachineReference = database.getReference().child("machines").child("_id").equalTo(idmachine);
Log.d(TAG + "idmachine",idmachine);
mRecyclerView = (RecyclerView) findViewById(R.id.product_recyclerview);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
FirebaseRecyclerAdapter<Product,ProductViewHolder> adapter =
new FirebaseRecyclerAdapter<Product, ProductViewHolder>(Product.class,R.layout.view_product,ProductViewHolder.class,mProductReference) {
#Override
protected void populateViewHolder(ProductViewHolder viewHolder, Product model, int position) {
viewHolder.productName.setText(model.name);
}
};
mRecyclerView.setAdapter(adapter);
}
public static class ProductViewHolder extends RecyclerView.ViewHolder{
TextView productName;
public ProductViewHolder(View itemView) {
super(itemView);
productName = (TextView) itemView.findViewById(R.id.product_name);
}
}
}
I tried it with this solution but it's not working because the activity crashes:
public class ProductAvtivity extends AppCompatActivity {
private final String TAG = "ProductActivity";
private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
private FirebaseDatabase database;
private DatabaseReference mProductReference;
private Query mMachineReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_avtivity);
database = FirebaseDatabase.getInstance();
mProductReference = database.getReference().child("products");
String idmachine = getIntent().getStringExtra("idmachine");
mMachineReference = database.getReference().child("machines").child("_id").equalTo(idmachine);
Log.d(TAG + "idmachine",idmachine);
mRecyclerView = (RecyclerView) findViewById(R.id.product_recyclerview);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
FirebaseRecyclerAdapter<Product,ProductViewHolder> adapter =
new FirebaseRecyclerAdapter<Product, ProductViewHolder>(Product.class,R.layout.view_product,ProductViewHolder.class,mProductReference) {
#Override
protected void populateViewHolder(final ProductViewHolder viewHolder, final Product model, int position) {
mMachineReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Machine machine = dataSnapshot.getValue(Machine.class);
for (Integer i : machine.id_prod){
if (model._id == i){
viewHolder.productName.setText(model.name);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
};
mRecyclerView.setAdapter(adapter);
}
public static class ProductViewHolder extends RecyclerView.ViewHolder{
TextView productName;
public ProductViewHolder(View itemView) {
super(itemView);
productName = (TextView) itemView.findViewById(R.id.product_name);
}
}
}
I have a list of product for all machine IDs and then I pass this from another activity where the user selects the machine. By this selection, I have to list in the recyclerview the product connected to it.

Unable to load more than one row using FirebaseRecyclerAdapter

I am trying to fetch the data from firebase, using firebaseRecyclerAdapter. But I am unable to populate all the data from the firebase database to the RecyclerView.
Database is as follows:
The output I am getting is only from the "qqq" row
rvlist Class:
public class rvlist {
String name, propellant;
public rvlist() {
}
public rvlist(String name, String propellant) {
this.name = name;
this.propellant = propellant;
}
public String getName() {
return name;
}
public String getPropellant() {
return propellant;
}
}
MyViewHolder:
public class MyViewHolder extends RecyclerView.ViewHolder {
View mView;
public MyViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setName(String name) {
TextView field = (TextView) mView.findViewById(R.id.a);
field.setText(name);
}
public void setPropellant(String text) {
TextView field = (TextView) mView.findViewById(R.id.b);
field.setText(text);
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
DatabaseReference mRef;
RecyclerView rv;
ProgressBar pb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
pb = (ProgressBar)findViewById(R.id.pb);
pb.setVisibility(View.VISIBLE);
rv = (RecyclerView) findViewById(R.id.rvv);
rv.setLayoutManager(new LinearLayoutManager(this));
mRef = FirebaseDatabase.getInstance().getReference().child("table");
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<rvlist, MyViewHolder> recyclerAdapter = new FirebaseRecyclerAdapter<rvlist, MyViewHolder>
(rvlist.class, R.layout.model, MyViewHolder.class, mRef) {
#Override
public void populateViewHolder(MyViewHolder itemsViewHolder, rvlist items, int position) {
pb.setVisibility(View.GONE);
itemsViewHolder.setName(items.getName());
itemsViewHolder.setPropellant(items.getPropellant());
}
};
rv.setAdapter(recyclerAdapter);
}
}
Make sure your RecyclerView and adapter layout_height is set to wrap_content not to match_parent
<android.support.v7.widget.RecyclerView
android:id="#+id/recv_location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="LinearLayoutManager"
...>
</android.support.v7.widget.RecyclerView>

Categories

Resources