Mohamed Dahab
Mohamed Dahab

Reputation: 21

image (only) is not appears in RecyclerView stored in FireBase Storage , while other items is appears

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.sstatic.net/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>

Upvotes: 0

Views: 57

Answers (2)

Mohamed Dahab
Mohamed Dahab

Reputation: 21

the problem was in Manifest.xml security issuess. solution : adding android:usesCleartextTraffic="true" to the application section

Upvotes: 0

Spiker
Spiker

Reputation: 575

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!!)

Upvotes: 1

Related Questions