I am new to app development. I have two apps linked in firebase, one for sending image to Firebase Storage and image url to Firebase Realtime Database and one for retrieving all images with RecyclerView. The first app is running fine. But the second doesn't do anything. It only shows the layout arrivals.
Code of arrivals.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="gr.packagename.name.MainActivity"
android:background="#drawable/background">
<ImageView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:src="#drawable/logo"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/arrivals_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="144dp" />
</RelativeLayout>
Code of model.xml
<?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="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<ImageView
android:layout_width="100dp"
android:layout_height="200dp"
android:id="#+id/clothesimage"
android:src="#drawable/men"/>
<TextView
android:layout_width="74dp"
android:layout_height="wrap_content"
android:id="#+id/categoryimage"
android:text="category"
android:layout_alignBottom="#+id/clothesimage"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="18sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
Code of arrivals.java
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;
public class arrivals extends AppCompatActivity {
DatabaseReference mdatabase;
RecyclerView rv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.arrivals);
mdatabase = FirebaseDatabase.getInstance().getReference().child("Images");
rv = (RecyclerView) findViewById(R.id.arrivals_list);
rv.setHasFixedSize(true);
rv.setLayoutManager(new LinearLayoutManager(this));
}
#Override
protected void onStart(){
super.onStart();
FirebaseRecyclerAdapter<Images, ImageViewHolder> firebaseadapter = new FirebaseRecyclerAdapter<Images, ImageViewHolder>(
Images.class,
R.layout.list_content,
ImageViewHolder.class,
mdatabase
) {
#Override
protected void populateViewHolder(ImageViewHolder viewHolder, Images model, int position) {
viewHolder.setCategory(model.getCategory());
viewHolder.setImageurl(getApplicationContext(), model.getImageurl());
}
};
rv.setAdapter(firebaseadapter);
}
public static class ImageViewHolder extends RecyclerView.ViewHolder{
View mView;
public ImageViewHolder(View itemView){
super(itemView);
mView = itemView;
}
public void setCategory(String category){
TextView categoryimg = (TextView) mView.findViewById(R.id.categoryimage);
categoryimg.setText(category);
}
public void setImageurl(Context ctx, String imageurl){
ImageView img = (ImageView) mView.findViewById(R.id.clothesimage);
Picasso.with(ctx).load(imageurl).into(img);
}
}
}
Code of Images.java
public class Images {
private String imageurl;
private String category;
public Images() {
}
public String getImageurl() {
return imageurl;
}
public void setImageurl(String imageurl) {
this.imageurl = imageurl;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
}
Related
I am unable to display the image (only) in RecyclerView stored in Firebase Storage, while I'm displaying the other text data in the recyclerView.
I browse through a lot of questions but i didn't find the answer for me .
hint: all roles for Realtime Database , Firebase Storage is set either to [true or test mode]
hint : below is the link for out put image :
https://i.stack.imgur.com/dagbA.jpg
data model
import com.google.firebase.database.Exclude;
public class House {
#Exclude
private String key;
//owner name
private String name ;
//location
private String Location;
//image
private String imageUrl;
/*all start with [RealState] (realTimeDatabase/storage)*/
/*
* [RealStateData] - database
* [RealStatePictures] - storage
* */
public House() {
//required
}
public House(String name, String location, String imageUrl) {
if (name.trim().equals("") || name.isEmpty()){
name = "realState company";
}
this.name = name;
Location = location;
this.imageUrl = imageUrl;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return Location;
}
public void setLocation(String location) {
Location = location;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
#Exclude
public String getKey() {
return key;
}
#Exclude
public void setKey(String key) {
this.key = key;
}
}
Adapter
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.squareup.picasso.Picasso;
import com.techmarinar.royalhomes.data.House;
import java.util.List;
public class HouseAdapter extends RecyclerView.Adapter<HouseAdapter.HouseHolder> {
private static final String TAG = "HouseAdapter";
private Context mContext;
private List<House> houseList;
public HouseAdapter(Context mContext, List<House> houseList) {
this.mContext = mContext;
this.houseList = houseList;
}
public void setUploads(List<House> uploads) {
this.houseList = uploads;
this.notifyDataSetChanged();
}
#NonNull
#Override
public HouseHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v= LayoutInflater.from(mContext)
.inflate(R.layout.list_item,parent,false);
return new HouseHolder(v);
}
#Override
public void onBindViewHolder(#NonNull HouseHolder holder, int position) {
//house object
House house = houseList.get(position);
holder.name.setText(house.getName());
holder.location.setText(house.getLocation());
// holder.image.setImageURI(Uri .parse(house.getImageUrl());
String imageUrl = house.getImageUrl();
Glide.with(mContext)
.load(imageUrl)
.placeholder(R.drawable.ic_baseline_add_business_24)
.centerCrop()
.into(holder.image);
}
#Override
public int getItemCount() {
return houseList.size();
}
#Override
public long getItemId(int position) {
return position;
}
public class HouseHolder extends RecyclerView.ViewHolder {
private ImageView image;
private TextView name;
private TextView location ;
public HouseHolder(#NonNull View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.xHouseImage);
name = (TextView) itemView.findViewById(R.id.xHouseName);
location = (TextView) itemView.findViewById(R.id.xHouseLocation);
}
}
}
MainActicity -- recyclerclass
package com.techmarinar.royalhomes;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
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.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.techmarinar.royalhomes.data.House;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements MenuItem.OnMenuItemClickListener {
private static final String TAG = "MainActivity";
//widget
private RecyclerView mRecycler;
private ProgressBar mProgress;
//firebase Element
private StorageReference storageReference;
private DatabaseReference databaseReference;
//Adapters var
private List<House> houseList;
private HouseAdapter mAdapter;
private ValueEventListener getFirebaseData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//set Widget
setupUIWidget();
//init firebase
initiateFireBaseElement();
//set recyclerView
setupRecyclerView();
//get data from fire(storage/fireDataBase);
getHousesDataFromFireBase();
//setup Item Touch Helper
setupItemTouchHelper(mRecycler , mAdapter);
}
private void setupUIWidget() {
setTitle("home");
//widget
mProgress = (ProgressBar) findViewById(R.id.progress_circular);
mRecycler = (RecyclerView) findViewById(R.id.recycler);
FloatingActionButton mActionButton = (FloatingActionButton) findViewById(R.id.floatingBtn);
//ActionButton
mActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(MainActivity.this, "wow", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this , EditHouse.class));
}
});
}
private void initiateFireBaseElement() {
//fireBase reference
databaseReference = FirebaseDatabase.getInstance().getReference("RealStateData");
storageReference= FirebaseStorage.getInstance().getReference("RealStatePictures") ;
}
private void setupRecyclerView() {
Log.d(TAG, "setupRecyclerView: ");
houseList = new ArrayList<>();
mRecycler.setLayoutManager(new LinearLayoutManager(this));
mRecycler.setHasFixedSize(true);
//mRecycler.setHasStableIds(true);
//init adapter
mAdapter = new HouseAdapter(MainActivity.this,houseList);
//set adapter to the recyclerView
mRecycler.setAdapter(mAdapter);
}
private void getHousesDataFromFireBase() {
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
houseList.clear();
for (DataSnapshot data : snapshot.getChildren()) {
//init house object
House houseValues = data.getValue(House.class);
// houseValues.setKey(data.getKey());
//fill the list
houseList.add(houseValues);
}
//notify the adapter
mAdapter.setUploads(houseList);
//mAdapter.notifyDataSetChanged();
//hide the progressBar
mProgress.setVisibility(View.INVISIBLE);
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
Log.d(TAG, "onCancelled: " + error.getMessage());
Toast.makeText(MainActivity.this, "" + error.getMessage(), Toast.LENGTH_SHORT).show();
//hide progressbar
mProgress.setVisibility(View.INVISIBLE);
}
});
}
private void setupItemTouchHelper(RecyclerView mRecycler, HouseAdapter mAdapter) {
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
}
}).attachToRecyclerView(mRecycler);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//menu
MenuItem addNewHouse , deleteHouse , updateHouse;
addNewHouse= menu.add(0,1,1,"add house");
addNewHouse.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
addNewHouse.setIcon(R.drawable.ic_baseline_supervised_user_circle_24);
updateHouse= menu.add(0,2,2,"update house");
deleteHouse= menu.add(0,3,3,"delete house");
addNewHouse.setOnMenuItemClickListener(this);
updateHouse.setOnMenuItemClickListener(this);
deleteHouse.setOnMenuItemClickListener(this);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case 1:
Toast.makeText(this, "new house", Toast.LENGTH_SHORT).show();
return true;
case 2:
Toast.makeText(this, "update house data", Toast.LENGTH_SHORT).show();
return true;
case 3:
Toast.makeText(this, "delete house data", Toast.LENGTH_SHORT).show();
return true;
default:return false;
}
}
}
here is xml files
List item:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="2dp"
app:cardBackgroundColor="#color/purple_200"
app:cardElevation="12dp"
app:cardCornerRadius="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="8dp">
<!--this view is only for beautiful looks -->
<View
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#color/purple_200"/>
<!--this view is only for beautiful looks -->
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/white"/>
<ImageView
android:id="#+id/xHouseImage"
android:layout_width="match_parent"
android:layout_height="220dp"
android:adjustViewBounds="true" />
<!--this view is only for beautiful looks -->
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/white"/>
<TextView
android:id="#+id/xHouseLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="house Location"
android:layout_margin="4dp"
android:layout_marginStart="6dp"
android:fontFamily="sans-serif-light"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textStyle="bold"
android:textColor="#color/black"
android:textSize="33sp"/>
<TextView
android:id="#+id/xHouseName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="house owner name"
android:layout_margin="4dp"
android:layout_marginStart="8dp"
android:fontFamily="sans-serif-light"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textColor="#color/black"
android:textSize="16sp"
android:gravity="top|center_vertical" />
</LinearLayout>
</androidx.cardview.widget.CardView>
recycler
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:layout_margin="8dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
app:layout_goneMarginBottom="8dp">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="4dp"
android:layout_marginBottom="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.029" />
<ProgressBar
android:id="#+id/progress_circular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.446"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.406" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/floatingBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_baseline_home_work_24"
android:backgroundTint="#color/white"
android:foregroundGravity="bottom|right"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.905"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.945" />
</androidx.constraintlayout.widget.ConstraintLayout>
Try this applyDefaultRequestOptions Method for Center crop of the image
Glide.with(mContext)
.applyDefaultRequestOptions(RequestOptions().centerCrop())
.load(imageUrl)
.placeholder(R.drawable.ic_baseline_add_business_24)
.into(target!!)
the problem was in Manifest.xml security issuess.
solution :
adding
android:usesCleartextTraffic="true"
to the application section
I am using firebase ui to load data from firebase database to recycler view but the data is not showing up inside the recycler view.
This is my fragment
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.FirebaseDatabase;
import com.infinity.houseestimatorandmodeler.R;
import com.infinity.houseestimatorandmodeler.databinding.FragmentBuildersBinding;
public class BuilderFragment extends Fragment {
private BuilderAdapter adapter;
public BuilderFragment() {
// Required empty public constructor
}
#Override
public void onStart() {
super.onStart();
adapter.startListening();
}
#Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
FragmentBuildersBinding databinding= DataBindingUtil.inflate(inflater, R.layout.fragment_builders, container, false);
FirebaseRecyclerOptions<buildersModel> options =
new FirebaseRecyclerOptions.Builder<buildersModel>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("builders"), buildersModel.class)
.build();
adapter = new BuilderAdapter(options);
databinding.buildersRecyclerView.setAdapter(adapter);
return databinding.getRoot();
}
}`
this is my adapter class that is adapting the data for showing in the recycler view
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.infinity.houseestimatorandmodeler.R;
public class BuilderAdapter extends FirebaseRecyclerAdapter<buildersModel, BuilderAdapter.BuilderViewHolder> {
public BuilderAdapter(#NonNull FirebaseRecyclerOptions<buildersModel> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull BuilderViewHolder holder, int i, #NonNull buildersModel builder) {
holder.name.setText(builder.getName());
holder.builderClass.setText(builder.getBuilderClass());
holder.address.setText(builder.getAddress());
}
#NonNull
#Override
public BuilderViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.builders_card, parent, false);
return new BuilderViewHolder(view);
}
class BuilderViewHolder extends RecyclerView.ViewHolder{
TextView name, builderClass, address;
public BuilderViewHolder(#NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.nameofbuilder);
builderClass = itemView.findViewById(R.id.classOfBuilder);
address = itemView.findViewById(R.id.address);
}
}
}
this is my model class for data coming from firebase `
class buildersModel
{
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getBuilderClass() {
return builderClass;
}
public void setBuilderClass(String builderClass) {
this.builderClass = builderClass;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public buildersModel(String address, String builderClass, String name) {
this.address = address;
this.builderClass = builderClass;
this.name = name;
}
String address;
String builderClass;
String name;}
this is my firebase database
card view for individual element in recycler view
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#drawable/rounded_cards"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_margin="20dp"
android:src="#drawable/ic_builder" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/rounded_cards">
<TextView
android:id="#+id/nameofbuilder"
android:layout_marginTop="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/baloo"
android:textSize="20dp"
android:padding="2dp"
android:text="Abdul Shakoor"/>
<TextView
android:id="#+id/classOfBuilder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textSize="15sp"
android:textColor="#342C2C"
android:padding="2dp"
android:text="Location Islambad"/>
<TextView
android:id="#+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#342C2C"
android:padding="2dp"
android:text="Address : I10 Islamabad near Rahimia mosque"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
recycler view layout
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
>
<data>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:src="#drawable/ic_builders"
android:background="#75CDDC"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/buildersRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
</LinearLayout>
You didn't set the RecyclerView LayoutManger, you can either set it in you layout
<androidx.recyclerview.widget.RecyclerView
....
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
Or in the fragment
databinding.buildersRecyclerView.setLayoutManager(new LinearLayoutManager(requireActivity()));
I am getting the error E/RecyclerView: No adapter attached; skipping layout
here is my code.. The code runs fine except the intent method. Profile activity has no recycler view.
when I click to start the intent it shows the error in logcat
2020-06-14 09:53:06.887 3521-3521/com.technolgiya.nitcalling11 E/RecyclerView: No adapter attached; skipping layout
FirebaseRecyclerAdapter<Contacts,FindFriendsViewHolder> firebaseRecyclerAdapter
= new FirebaseRecyclerAdapter<Contacts, FindFriendsViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull final FindFriendsViewHolder holder, final int position, #NonNull final Contacts model)
{
holder.userNameTxt.setText(model.getName());
Picasso.get().load(model.getImage()).into(holder.profileImageView);
String visit_user_id = getRef(position).getKey();
Toast.makeText(FindPeopleActivity.this, "view on:" + visit_user_id+getRef(position), Toast.LENGTH_LONG).show();
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.getAdapterPosition();
String visit_user_id = getRef(position).getKey();
Toast.makeText(FindPeopleActivity.this, "clicked on:" + visit_user_id, Toast.LENGTH_LONG).show();
Intent intent = new Intent (FindPeopleActivity.this, ProfileActivity.class);
intent.putExtra("visit_user_id", visit_user_id);
intent.putExtra("profile_image", model.getImage());
intent.putExtra("profile_name", model.getName());
startActivity(intent);
}
});
}
here is my recycler view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FindPeopleActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/app_bar_layout_find_people"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar_contacts"
android:layout_width="match_parent"
android:layout_height="60dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/search_user_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="find freiend..."
android:textColorHint="#android:color/white"
android:textAlignment="center"
android:layout_marginRight="16dp"
android:drawableLeft="#drawable/search"
android:textColor="#android:color/white"
android:layout_centerVertical="true"
>
</EditText>
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/find_friends_list"
android:layout_below="#+id/app_bar_layout_find_people"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
here is my ProfileActivity.java
package com.technolgiya.nitcalling11;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;
public class ProfileActivity extends AppCompatActivity {
private String receiverUserID="";
private String receiverUserImage="";
private String receiverUserName="";
private ImageView background_profile_view;
private TextView name_profile;
private Button add_friend;
private Button decline_friend_request;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
background_profile_view.findViewById(R.id.background_profile_view);
name_profile = findViewById(R.id.name_profile);
add_friend = findViewById(R.id.add_friend);
decline_friend_request = findViewById(R.id.decline_friend_request);
receiverUserID = getIntent().getExtras().get("visit_user_id").toString();
receiverUserImage = getIntent().getExtras().get("profile_image").toString();
receiverUserName = getIntent().getExtras().get("profile_name").toString();
Toast.makeText(this, receiverUserID, Toast.LENGTH_SHORT).show();
Picasso.get().load(receiverUserImage).into(background_profile_view);
name_profile.setText(receiverUserName);
}
}
here is my activity_profile.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ProfileActivity">
<ImageView
android:id="#+id/background_profile_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/profile_image"
android:scaleType="centerCrop"
>
</ImageView>
<TextView
android:id="#+id/name_profile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="User name"
android:textColor="#color/colorPrimary"
android:textSize="30dp"
android:maxLines="2"
android:textStyle="bold"
android:gravity="center"
android:layout_centerInParent="true"
>
</TextView>
<Button
android:id="#+id/add_friend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:layout_below="#+id/name_profile"
android:layout_marginTop="35dp"
android:layout_marginLeft="35dp"
android:layout_marginRight="35dp"
android:layout_marginBottom="12dp"
android:background="#color/colorPrimary"
android:text="Add Friend"
android:padding="5dp"
>
</Button>
<Button
android:id="#+id/decline_friend_request"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:layout_below="#+id/add_friend"
android:layout_marginTop="1dp"
android:layout_marginLeft="35dp"
android:layout_marginRight="35dp"
android:layout_marginBottom="12dp"
android:background="#color/colorPrimary"
android:text="Cancel Friend Request"
android:padding="5dp"
android:visibility="gone"
>
</Button>
</RelativeLayout>
here is the whole code of FindPeople_Activity
package com.technolgiya.nitcalling11;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;
public class FindPeopleActivity extends AppCompatActivity {
private RecyclerView findFriendList;
private EditText searchET;
private String str = "";
private DatabaseReference usersRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_people);
usersRef = FirebaseDatabase.getInstance().getReference().child("Users");
FirebaseRecyclerOptions<Contacts> options = null;
searchET = findViewById(R.id.search_user_text);
findFriendList = findViewById(R.id.find_friends_list);
findFriendList.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
searchET.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charseq, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence charseq, int start, int before, int count) {
if (searchET.getText().toString().equals("")) {
Toast.makeText(FindPeopleActivity.this, "please write name to search", Toast.LENGTH_SHORT).show();
} else {
str = charseq.toString();
onStart();
}
}
#Override
public void afterTextChanged(Editable charseq) {
}
});
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<Contacts> options = null;
if(str.equals(""))
{
options =
new FirebaseRecyclerOptions.Builder<Contacts>()
.setQuery(usersRef, Contacts.class)
.build();
}
else
{
options =
new FirebaseRecyclerOptions.Builder<Contacts>()
.setQuery(usersRef.orderByChild("name")
.startAt(str)
.endAt(str + "\uf8ff")
, Contacts.class)
.build();
}
FirebaseRecyclerAdapter<Contacts,FindFriendsViewHolder> firebaseRecyclerAdapter
= new FirebaseRecyclerAdapter<Contacts, FindFriendsViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull final FindFriendsViewHolder holder, final int position, #NonNull final Contacts model)
{
holder.userNameTxt.setText(model.getName());
Picasso.get().load(model.getImage()).into(holder.profileImageView);
String visit_user_id = getRef(position).getKey();
Toast.makeText(FindPeopleActivity.this, "view on:" + visit_user_id+getRef(position), Toast.LENGTH_LONG).show();
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.getAdapterPosition();
String visit_user_id = getRef(position).getKey();
Toast.makeText(FindPeopleActivity.this, "clicked on:" + visit_user_id, Toast.LENGTH_LONG).show();
Intent intent = new Intent (FindPeopleActivity.this, ProfileActivity.class);
intent.putExtra("visit_user_id", visit_user_id);
intent.putExtra("profile_image", model.getImage());
intent.putExtra("profile_name", model.getName());
startActivity(intent);
}
});
}
#NonNull
#Override
public FindFriendsViewHolder onCreateViewHolder(#NonNull ViewGroup p, int viewType)
{
View view = LayoutInflater.from(p.getContext()).inflate(R.layout.contact_design,p,false);
FindFriendsViewHolder viewHolder = new FindFriendsViewHolder(view);
return viewHolder;
}
};
findFriendList.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.startListening();
}
public static class FindFriendsViewHolder extends RecyclerView.ViewHolder
{
TextView userNameTxt;
Button videoCallBtn;
ImageView profileImageView;
RelativeLayout cardView;
public FindFriendsViewHolder(#NonNull View itemView) {
super(itemView);
userNameTxt=itemView.findViewById(R.id.name_contact);
videoCallBtn=itemView.findViewById(R.id.call_btn);
profileImageView=itemView.findViewById(R.id.image_contact);
cardView=itemView.findViewById(R.id.card_view1);
videoCallBtn.setVisibility(View.GONE);
}
}
}
here is my contacts.java
package com.technolgiya.nitcalling11;
public class Contacts
{
String name,image,status,uid;
public Contacts() {
}
public Contacts(String name, String image, String status, String uid) {
this.name = name;
this.image = image;
this.status = status;
this.uid = uid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
}
here is the activity_contacts.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/app_bar_layout_contacts"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar_contacts"
android:layout_width="match_parent"
android:layout_height="50dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contacts"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#android:color/white"
android:layout_centerVertical="true"
>
</TextView>
<ImageView
android:id="#+id/find_people_btn"
android:layout_width="38dp"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:layout_marginEnd="12dp"
android:src="#drawable/find_people"
android:tint="#android:color/white"
>
</ImageView>
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/contact_list"
android:layout_below="#+id/app_bar_layout_contacts"
android:orientation="vertical"
android:layout_above="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
android:layout_alignParentBottom="true"
app:menu="#menu/bottom_nav_menu" />
</RelativeLayout>
UPDATE:
I got:
Unable to start activity ComponentInfo{com.technolgiya.nitcalling11/com.technolgiya.nitcalling11.ProfileActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.ImageView.findViewById(int)' on a null object reference
Please Try to start listening for Firebase data before setting the adapter, so
replace
findFriendList.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.startListening();
with
firebaseRecyclerAdapter.startListening();
findFriendList.setAdapter(firebaseRecyclerAdapter);
in your onStart() method.
UPDATE
Unable to start activity ComponentInfo{com.technolgiya.nitcalling11/com.technolgiya.nitcalling11.ProfileActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.ImageView.findViewById(int)' on a null object reference
In ProfileActivity Replace
background_profile_view.findViewById(R.id.background_profile_view);
With
background_profile_view = findViewById(R.id.background_profile_view);
I am slightly new to Firebase implementation, and would appreciate any help in the display of my Firebase data in my Android application. There are no specifics in "what data" should be displayed, I am simply attempting to populate the entire database into the application. There are also NOT specific user auth's, the database is shared by all users for the sake of this application.
Here is my Cart.java (Where I would like the data to populate)
package edu.phoenix.mbl402.week2apptt2163;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class sunglass1 extends AppCompatActivity {
private FirebaseAuth mAuth;
private String TAG;
int minteger = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sunglass2);
mAuth = FirebaseAuth.getInstance();
Button button = (Button) findViewById(R.id.addcart);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("Octagon Glasses");
myRef.setValue("$64");
Toast.makeText(getBaseContext(), "Added to Cart", Toast.LENGTH_SHORT).show();
}
});
Button button2 = (Button) findViewById(R.id.viewcart);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openCart();
}
});
}
public void openCart() {
Intent intent = new Intent(this, cart.class);
startActivity(intent);
}
#Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
}}
Here is the cart.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:showIn="#layout/activity_main"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</RelativeLayout>
Here is my cartadapter.java:
package edu.phoenix.mbl402.week2apptt2163;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class cartadapter extends RecyclerView.Adapter<cartadapter.MyViewHolder> {
private List<cartitems> cartList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, price;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
price = (TextView) view.findViewById(R.id.price);
}
}
public cartadapter(List<cartitems> moviesList) {
this.cartList = moviesList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cart_list_row, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
cartitems items = cartList.get(position);
holder.title.setText(items.getTitle());
holder.price.setText(items.getPrice());
}
#Override
public int getItemCount() {
return cartList.size();
}
}
And of course, cartitems.java:
package edu.phoenix.mbl402.week2apptt2163;
public class cartitems {
private String title, price;
public cartitems() {
}
public cartitems(String title, String price) {
this.title = title;
this.price = price;
}
public String getTitle() {
return title;
}
public void setTitle(String name) {
this.title = name;
}
public String getPrice() {
return price;
}
public void setPrice(String year) {
this.price = price;
}
}
Here is the cart_list_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:paddingBottom="#dimen/row_padding_vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/row_padding_vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textColor="#color/title"
android:textSize="40dp"
android:text="Your Cart"
android:gravity="center"
android:textStyle="bold" />
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textColor="#color/title"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:id="#+id/price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/title" />
</RelativeLayout>
Thank you for absolutely any help,
if you don't use authentication and no error(or Exception) comes then i think you must set auth == null in firebase console database rule part.and if you also done it then check variable name of the class cartitems should same as firebase variable.
i'm trying to create a a resturant apk which it has a menu inside this menu you chose what kinda food you want lets say if you want a pizza and when you press pizza it will open another menu with different kind of pizza and when press the pizza that i want it should take you to another activity and show you the details of the pizza but its not showing me and i don't know what is the problem
this is my food list activity
package com.example.median1.demo;
import android.content.Intent;
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 android.view.View;
import android.widget.Toast;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;
public class FoodLIst extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
FirebaseDatabase database;
DatabaseReference foodList;
String categoryId;
FirebaseRecyclerAdapter<Food,FoodViewHolder>adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_list);
//firebase
database=FirebaseDatabase.getInstance();
foodList= database.getReference("Foods");
recyclerView=(RecyclerView)findViewById(R.id.recycler_food);
recyclerView.setHasFixedSize(true);
layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
//get food list intent
if(getIntent()!=null){
categoryId=getIntent().getStringExtra("CategoryId");
if(!categoryId.isEmpty()&& categoryId!=null){
loadListFood(categoryId);
}
}
}
private void loadListFood(String categoryId) {
adapter=new FirebaseRecyclerAdapter<Food, FoodViewHolder>(Food.class,
R.layout.food_item,
FoodViewHolder.class,
foodList.orderByChild("MenuId").equalTo(categoryId)) {
#Override
protected void populateViewHolder(FoodViewHolder viewHolder, Food model, int position) {
viewHolder.food_name.setText(model.getName());
Picasso.with(getBaseContext()).load(model.getImage())
.into(viewHolder.food_image);
final Food local=model;
viewHolder.setItemClicklistener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
// start new activity for the food destails
Intent foodDetails=new Intent(FoodLIst.this,FoodDetail.class);
foodDetails.putExtra("FoodId",adapter.getRef(position).getKey()); //send foood id to new activity
startActivity(foodDetails);
}
});
}
};
//set adapter
Log.d("TAG",""+adapter.getItemCount());
recyclerView.setAdapter(adapter);
}
}
and this is my food details activity
package com.example.median1.demo;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import com.cepheuen.elegantnumberbutton.view.ElegantNumberButton;
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.squareup.picasso.Picasso;
import java.io.File;
public class FoodDetail extends AppCompatActivity {
TextView food_name,food_price,food_description;
ImageView food_image;
CollapsingToolbarLayout collapsingToolbarLayout;
FloatingActionButton btnCart;
ElegantNumberButton numberButton;
String foodId="";
FirebaseDatabase database;
DatabaseReference foods;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_detail);
//firebase
database=FirebaseDatabase.getInstance();
foods=database.getReference("Foods");
//int View
numberButton=(ElegantNumberButton)findViewById(R.id.number_button);
btnCart=(FloatingActionButton)findViewById(R.id.btnCart);
food_description=(TextView)findViewById(R.id.food_description2);
food_name=(TextView)findViewById(R.id.food_name);
food_price=(TextView)findViewById(R.id.food_price);
food_image=(ImageView)findViewById(R.id.img_food);
collapsingToolbarLayout=(CollapsingToolbarLayout)findViewById(R.id.collapsing);
collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExpandedAppbar);
collapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.CollapsedAppbar);
//get food id from the intent
if(getIntent()!=null)
foodId=getIntent().getStringExtra("FoodId");
if(foodId.isEmpty()){
getDetailFood(foodId);
}
}
private void getDetailFood(String foodId) {
foods.child(foodId).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Food food=dataSnapshot.getValue(Food.class);
//set image
Picasso.with(getBaseContext()).load(food.getImage()).into(food_image);
collapsingToolbarLayout.setTitle(food.getName());
food_price.setText(food.getPrice());
food_name.setText(food.getName());
food_description.setText(food.getDescription());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
and this is the food class for the food details where i retrieve all the childs from the firebase
package com.example.median1.demo;
/**
* Created by median1 on 11/5/2017.
*/
public class Food {
private String Name,Image,Description,Price,Discount,MenuId;
public Food() {
}
public Food(String name, String image, String description, String price, String discount, String menuId) {
Name = name;
Image = image;
Description = description;
Price = price;
Discount = discount;
MenuId = menuId;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public String getPrice() {
return Price;
}
public void setPrice(String price) {
Price = price;
}
public String getDiscount() {
return Discount;
}
public void setDiscount(String discount) {
Discount = discount;
}
public String getMenuId() {
return MenuId;
}
public void setMenuId(String menuId) {
MenuId = menuId;
}
}
and this is the xml of the food details
i dont know if its important
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.median1.demo.FoodDetail">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true"
>
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing"
android:layout_width="match_parent"
android:layout_height="350dp"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="#0e0d0e"
app:expandedTitleTextAppearance="#android:color/transparent">
<ImageView
android:id="#+id/img_food"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#null"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"
/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:title="food name"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="parallax"
/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/btnCart"
android:src="#drawable/ic_shopping_cart_black_24dp"
android:backgroundTint="#android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="6dp"
app:pressedTranslationZ="12dp"
app:layout_anchor="#id/app_bar_layout"
app:layout_anchorGravity="bottom|right|end"
app:useCompatPadding="true"
/>
<android.support.v4.widget.NestedScrollView
android:id="#+id/nestedScrollView"
android:clipToPadding="false"
android:layout_width="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/food_name"
android:layout_marginTop="8dp"
android:padding="12dp"
android:text="Food name "
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/layout_price"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/ic_attach_money_black_24dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/food_price"
android:text="1,000 "
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#color/colorPrimary"
android:layout_width="0dp"
android:layout_weight="9"
android:layout_height="wrap_content" />
</LinearLayout>
<com.cepheuen.elegantnumberbutton.view.ElegantNumberButton
android:layout_width="100dp"
android:layout_height="30dp"
android:id="#+id/number_button"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginBottom="18dp"
app:textSize="8sp"
app:backGroundColor="#color/colorAccent"
app:initialNumber="1"
app:finalNumber="20"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/food_description2"
android:layout_marginTop="12dp"
android:lineSpacingMultiplier="1.5"
android:padding="12dp"
android:text="description"
android:textColor="#android:color/black"
android:textSize="14sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
Firebase database
can't you see the error on logcat or run? i think the error is here:
//get food id from the intent
if(getIntent()!=null)
foodId=getIntent().getStringExtra("FoodId");
if(foodId.isEmpty()){
getDetailFood(foodId);
}
}
or here
//get food list intent
if(getIntent()!=null){
categoryId=getIntent().getStringExtra("CategoryId");
if(!categoryId.isEmpty()&& categoryId!=null){
loadListFood(categoryId);
}
}
}
maybe check run when you click and the problem happens to see where is the problem.. you may get a null string
EDITED: i made the same app got the same problem with you and on my run its saying the problem is here if(!categoryId.isEmpty()&& categoryId!=null